From 9daa9d0da8d533673ee4f64c7e461f76ff2d67e7 Mon Sep 17 00:00:00 2001 From: Fotis Papadogeorgopoulos Date: Thu, 19 Dec 2024 02:39:21 +0200 Subject: [PATCH] fix(remove): ensure that link-workspace-packages=false is respected in single project runs in a workspace (#8881) --- .changeset/hip-rats-love.md | 6 + .../src/remove.ts | 1 + .../test/remove/workspace.ts | 165 ++++++++++++++++++ 3 files changed, 172 insertions(+) create mode 100644 .changeset/hip-rats-love.md create mode 100644 pkg-manager/plugin-commands-installation/test/remove/workspace.ts diff --git a/.changeset/hip-rats-love.md b/.changeset/hip-rats-love.md new file mode 100644 index 0000000000..9368114ab0 --- /dev/null +++ b/.changeset/hip-rats-love.md @@ -0,0 +1,6 @@ +--- +"@pnpm/plugin-commands-installation": patch +"pnpm": patch +--- + +`pnpm remove` should not link dependencies from the workspace, when `link-workspace-packages` is set to `false` [#7674](https://github.com/pnpm/pnpm/issues/7674). diff --git a/pkg-manager/plugin-commands-installation/src/remove.ts b/pkg-manager/plugin-commands-installation/src/remove.ts index dcf4dfd455..4e79292b93 100644 --- a/pkg-manager/plugin-commands-installation/src/remove.ts +++ b/pkg-manager/plugin-commands-installation/src/remove.ts @@ -174,6 +174,7 @@ export async function handler ( const store = await createOrConnectStoreController(opts) const removeOpts = Object.assign(opts, { ...getOptionsFromRootManifest(opts.rootProjectManifestDir, opts.rootProjectManifest ?? {}), + linkWorkspacePackagesDepth: opts.linkWorkspacePackages === 'deep' ? Infinity : opts.linkWorkspacePackages ? 0 : -1, storeController: store.ctrl, storeDir: store.dir, include, diff --git a/pkg-manager/plugin-commands-installation/test/remove/workspace.ts b/pkg-manager/plugin-commands-installation/test/remove/workspace.ts new file mode 100644 index 0000000000..8021ecd19e --- /dev/null +++ b/pkg-manager/plugin-commands-installation/test/remove/workspace.ts @@ -0,0 +1,165 @@ +import path from 'path' +import { filterPackagesFromDir } from '@pnpm/workspace.filter-packages-from-dir' +import { type LockfileFile } from '@pnpm/lockfile.types' +import { install, remove } from '@pnpm/plugin-commands-installation' +import { preparePackages } from '@pnpm/prepare' +import { sync as readYamlFile } from 'read-yaml-file' +import { DEFAULT_OPTS } from '../utils' + +test('remove --filter only changes the specified dependency, when run with link-workspace-packages=false', async () => { + const projects = preparePackages([ + { + name: 'project-1', + version: '1.0.0', + + dependencies: { + 'is-negative': '1.0.0', + }, + }, + { + name: 'project-2', + version: '1.0.0', + + dependencies: { + 'project-1': '1.0.0', + 'is-negative': '1.0.0', + }, + }, + ]) + + const sharedOpts = { + dir: process.cwd(), + recursive: true, + workspaceDir: process.cwd(), + lockfileDir: process.cwd(), + sharedWorkspaceLockfile: true, + linkWorkspacePackages: false, + } + + await install.handler({ + ...DEFAULT_OPTS, + ...await filterPackagesFromDir(process.cwd(), []), + ...sharedOpts, + }) + + await remove.handler({ + ...DEFAULT_OPTS, + // Only remove is-negative from project-2 + ...await filterPackagesFromDir(process.cwd(), [{ namePattern: 'project-2' }]), + ...sharedOpts, + }, ['is-negative']) + + // project-1 should be unchanged + { + const pkg = await import(path.resolve('project-1/package.json')) + expect(pkg?.dependencies).toStrictEqual({ + 'is-negative': '1.0.0', + }) + } + + // project-2 has the is-negative dependency removed + { + const pkg = await import(path.resolve('project-2/package.json')) + expect(pkg?.dependencies).toStrictEqual({ + 'project-1': '1.0.0', + }) + } + + // Anything left can still be resolved + projects['project-1'].has('is-negative') + projects['project-2'].has('project-1') + projects['project-2'].hasNot('is-negative') + + // The lockfile agrees with the above + const lockfile = readYamlFile('./pnpm-lock.yaml') + + expect(lockfile.importers?.['project-1'].dependencies?.['is-negative']).toStrictEqual({ + specifier: '1.0.0', + version: '1.0.0', + }) + + expect(lockfile.importers?.['project-2'].dependencies?.['project-1']).toStrictEqual({ + specifier: '1.0.0', + version: '1.0.0', + }) +}) + +test('remove from within a workspace package dir only affects the specified dependency, when run with link-workspace-packages=false', async () => { + const projects = preparePackages([ + { + name: 'project-1', + version: '1.0.0', + + dependencies: { + 'is-negative': '1.0.0', + }, + }, + { + name: 'project-2', + version: '1.0.0', + + dependencies: { + 'project-1': '1.0.0', + 'is-negative': '1.0.0', + }, + }, + ]) + + const sharedOpts = { + dir: process.cwd(), + workspaceDir: process.cwd(), + lockfileDir: process.cwd(), + sharedWorkspaceLockfile: true, + linkWorkspacePackages: false, + } + + await install.handler({ + ...DEFAULT_OPTS, + ...await filterPackagesFromDir(process.cwd(), []), + ...sharedOpts, + recursive: true, + }) + + await remove.handler({ + ...DEFAULT_OPTS, + ...sharedOpts, + // In this scenario, remove is invoked from within a workspace directory, + // non-recursively + dir: projects['project-2'].dir(), + recursive: false, + }, ['is-negative']) + + // project-1 should be unchanged + { + const pkg = await import(path.resolve('project-1/package.json')) + expect(pkg?.dependencies).toStrictEqual({ + 'is-negative': '1.0.0', + }) + } + + // project-2 has the is-negative dependency removed + { + const pkg = await import(path.resolve('project-2/package.json')) + expect(pkg?.dependencies).toStrictEqual({ + 'project-1': '1.0.0', + }) + } + + // Anything left can still be resolved + projects['project-1'].has('is-negative') + projects['project-2'].has('project-1') + projects['project-2'].hasNot('is-negative') + + // The lockfile agrees with the above + const lockfile = readYamlFile('./pnpm-lock.yaml') + + expect(lockfile.importers?.['project-1'].dependencies?.['is-negative']).toStrictEqual({ + specifier: '1.0.0', + version: '1.0.0', + }) + + expect(lockfile.importers?.['project-2'].dependencies?.['project-1']).toStrictEqual({ + specifier: '1.0.0', + version: '1.0.0', + }) +})