diff --git a/.changeset/spicy-eggs-collect.md b/.changeset/spicy-eggs-collect.md new file mode 100644 index 0000000000..bc97d7f058 --- /dev/null +++ b/.changeset/spicy-eggs-collect.md @@ -0,0 +1,5 @@ +--- +"@pnpm/plugin-commands-installation": patch +--- + +Fix: align pnpm save-prefix behavior when a range is not specified explicitly. diff --git a/packages/plugin-commands-installation/src/getPinnedVersion.ts b/packages/plugin-commands-installation/src/getPinnedVersion.ts index 2fe20512e3..fa58a66311 100644 --- a/packages/plugin-commands-installation/src/getPinnedVersion.ts +++ b/packages/plugin-commands-installation/src/getPinnedVersion.ts @@ -1,4 +1,4 @@ export default function getPinnedVersion (opts: { saveExact?: boolean, savePrefix?: string }) { - if (opts.saveExact === true) return 'patch' + if (opts.saveExact === true || opts.savePrefix === '') return 'patch' return opts.savePrefix === '~' ? 'minor' : 'major' } diff --git a/packages/plugin-commands-installation/test/add.ts b/packages/plugin-commands-installation/test/add.ts index c30e4a7feb..a22eea6f94 100644 --- a/packages/plugin-commands-installation/test/add.ts +++ b/packages/plugin-commands-installation/test/add.ts @@ -243,3 +243,45 @@ test('pnpm add --save-peer', async () => { ) } }) + +test('pnpm add - with save-prefix set to empty string should save package version without prefix', async () => { + prepare() + await add.handler({ + ...DEFAULT_OPTIONS, + dir: process.cwd(), + linkWorkspacePackages: false, + savePrefix: '', + }, ['is-positive@1.0.0']) + + { + const manifest = await loadJsonFile(path.resolve('package.json')) + + expect( + manifest + ).toStrictEqual( + { + name: 'project', + version: '0.0.0', + dependencies: { 'is-positive': '1.0.0' }, + } + ) + } +}) + +test('pnpm add - should add prefix when set in .npmrc when a range is not specified explicitly', async () => { + prepare() + await add.handler({ + ...DEFAULT_OPTIONS, + dir: process.cwd(), + linkWorkspacePackages: false, + savePrefix: '~', + }, ['is-positive']) + + { + const manifest = (await import(path.resolve('package.json'))) + + expect( + manifest.dependencies['is-positive'] + ).toMatch(/~([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$/) + } +})