From b1fd38ccabf37ea21cb885740422e30559116378 Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Thu, 16 Nov 2023 14:23:07 +0200 Subject: [PATCH] fix: don't remove modules directory when registry configuration changes (#7322) --- .changeset/mighty-pots-return.md | 6 ++++ pkg-manager/core/test/lockfile.ts | 46 ---------------------------- pkg-manager/get-context/src/index.ts | 9 ------ 3 files changed, 6 insertions(+), 55 deletions(-) create mode 100644 .changeset/mighty-pots-return.md diff --git a/.changeset/mighty-pots-return.md b/.changeset/mighty-pots-return.md new file mode 100644 index 0000000000..ae6e90e8f4 --- /dev/null +++ b/.changeset/mighty-pots-return.md @@ -0,0 +1,6 @@ +--- +"@pnpm/get-context": patch +"pnpm": patch +--- + +The modules directory should not be removed if the registry configuration has changed. diff --git a/pkg-manager/core/test/lockfile.ts b/pkg-manager/core/test/lockfile.ts index 2a623dc267..8113972102 100644 --- a/pkg-manager/core/test/lockfile.ts +++ b/pkg-manager/core/test/lockfile.ts @@ -1051,52 +1051,6 @@ test('existing dependencies are preserved when updating a lockfile to a newer fo expect(initialLockfile.packages).toStrictEqual(updatedLockfile.packages) }) -test('lockfile is not getting broken if the used registry changes', async () => { - const project = prepareEmpty() - - const manifest = await addDependenciesToPackage({}, ['is-positive@1'], await testDefaults()) - - const newOpts = await testDefaults({ registries: { default: 'https://registry.npmjs.org/' } }) - let err!: PnpmError - try { - await addDependenciesToPackage(manifest, ['is-negative@1'], newOpts) - } catch (_err: any) { // eslint-disable-line - err = _err - } - expect(err.code).toBe('ERR_PNPM_REGISTRIES_MISMATCH') - - await mutateModulesInSingleProject({ - manifest, - mutation: 'install', - rootDir: process.cwd(), - }, { - ...newOpts, - confirmModulesPurge: false, - }) - await addDependenciesToPackage(manifest, ['is-negative@1'], newOpts) - - expect(Object.keys((await project.readLockfile()).packages)).toStrictEqual([ - '/is-negative@1.0.1', - '/is-positive@1.0.0', - ]) -}) - -test('when using a different registry, add -g to the error report according to options.global', async () => { - prepareEmpty() - - const manifest = await addDependenciesToPackage({}, ['is-positive@1'], await testDefaults()) - - const newOpts = await testDefaults({ registries: { default: 'https://registry.npmjs.org/' }, global: true }) - let err!: PnpmError - try { - await addDependenciesToPackage(manifest, ['is-negative@1'], newOpts) - } catch (_err: any) { // eslint-disable-line - err = _err - } - expect(err.code).toBe('ERR_PNPM_REGISTRIES_MISMATCH') - expect(err.hint).toContain('pnpm install -g') -}) - test('broken lockfile is fixed even if it seems like up to date at first. Unless frozenLockfile option is set to true', async () => { const project = prepareEmpty() await addDistTag({ package: '@pnpm.e2e/pkg-with-1-dep', version: '100.0.0', distTag: 'latest' }) diff --git a/pkg-manager/get-context/src/index.ts b/pkg-manager/get-context/src/index.ts index 6ed47ce320..4c4b0893e2 100644 --- a/pkg-manager/get-context/src/index.ts +++ b/pkg-manager/get-context/src/index.ts @@ -286,15 +286,6 @@ async function validateModules ( purged = true } })) - if ((modules.registries != null) && !equals(opts.registries, modules.registries)) { - if (opts.forceNewModules) { - await purgeModulesDirsOfImporters(opts, projects) - return { purged: true } - } - throw new PnpmError('REGISTRIES_MISMATCH', `This modules directory was created using the following registries configuration: ${JSON.stringify(modules.registries)}. The current configuration is ${JSON.stringify(opts.registries)}.`, { - hint: `To recreate the modules directory using the new settings, run "pnpm install${opts.global ? ' -g' : ''}".`, - }) - } if (purged && (rootProject == null)) { await purgeModulesDirsOfImporter(opts, { modulesDir: path.join(opts.lockfileDir, opts.modulesDir),