From d5c40b556cd48ada5bf7c7c7a93b474431ae8fdb Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Tue, 9 May 2023 15:34:02 +0300 Subject: [PATCH] fix(link): don't update version specs in package.json (#6513) close #4341 --- .changeset/angry-bobcats-develop.md | 6 ++++++ pkg-manager/plugin-commands-installation/src/link.ts | 4 +++- pkg-manager/plugin-commands-installation/test/link.ts | 9 +++++++-- 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 .changeset/angry-bobcats-develop.md diff --git a/.changeset/angry-bobcats-develop.md b/.changeset/angry-bobcats-develop.md new file mode 100644 index 00000000000..a1de02d9fe9 --- /dev/null +++ b/.changeset/angry-bobcats-develop.md @@ -0,0 +1,6 @@ +--- +"@pnpm/plugin-commands-installation": patch +"pnpm": patch +--- + +`pnpm link -g ` should not modify the `package.json` file [#4341](https://github.com/pnpm/pnpm/issues/4341). diff --git a/pkg-manager/plugin-commands-installation/src/link.ts b/pkg-manager/plugin-commands-installation/src/link.ts index 872f732f9b1..e58f93d0df6 100644 --- a/pkg-manager/plugin-commands-installation/src/link.ts +++ b/pkg-manager/plugin-commands-installation/src/link.ts @@ -200,7 +200,9 @@ export async function handler ( storeDir: storeL.dir, manifest, } as LinkFunctionOptions) - await writeProjectManifest(newManifest) + if (!opts.cliOptions?.global) { + await writeProjectManifest(newManifest) + } await Promise.all( Array.from(storeControllerCache.values()) diff --git a/pkg-manager/plugin-commands-installation/test/link.ts b/pkg-manager/plugin-commands-installation/test/link.ts index 39629f5e8f8..656d76dcdd5 100644 --- a/pkg-manager/plugin-commands-installation/test/link.ts +++ b/pkg-manager/plugin-commands-installation/test/link.ts @@ -5,6 +5,7 @@ import { install, link } from '@pnpm/plugin-commands-installation' import { prepare, preparePackages } from '@pnpm/prepare' import { assertProject, isExecutable } from '@pnpm/assert-project' import { fixtures } from '@pnpm/test-fixtures' +import { sync as loadJsonFile } from 'load-json-file' import PATH from 'path-name' import writePkg from 'write-pkg' import { DEFAULT_OPTS } from './utils' @@ -107,7 +108,7 @@ test('link to global bin from the specified directory', async function () { }) test('link a global package to the specified directory', async function () { - const project = prepare() + const project = prepare({ dependencies: { 'global-package-with-bin': '0.0.0' } }) process.chdir('..') const globalDir = path.resolve('global') @@ -132,20 +133,24 @@ test('link a global package to the specified directory', async function () { }) process.chdir('..') + const projectDir = path.resolve('./project') // link from global await link.handler({ ...DEFAULT_OPTS, cliOptions: { global: true, - dir: path.resolve('./project'), + dir: projectDir, }, bin: globalBin, dir: globalDir, + saveProd: true, // @pnpm/config sets this setting to true when global is true. This should probably be changed. }, ['global-package-with-bin']) process.env[PATH] = oldPath + const manifest = loadJsonFile(path.join(projectDir, 'package.json')) // eslint-disable-line @typescript-eslint/no-explicit-any + expect(manifest.dependencies).toStrictEqual({ 'global-package-with-bin': '0.0.0' }) await project.has('global-package-with-bin') })