diff --git a/.changeset/large-suns-relax.md b/.changeset/large-suns-relax.md new file mode 100644 index 00000000000..4f719104aaf --- /dev/null +++ b/.changeset/large-suns-relax.md @@ -0,0 +1,9 @@ +--- +"pnpm": patch +--- + +fix: Stop --filter-prod option to run command on all the projects when used on workspace. --filter-prod option now only filter from `dependencies` and omit `devDependencies` instead of including all the packages when used on workspace. So what was happening is that if you use `--filter-prod` on workspace root like this: +```bash +pnpm --filter-prod ...build-modules exec node -e 'console.log(require(`./package.json`).name)' +``` +it was printing all the package of workspace, where it should only print the package name of itself and packages where it has been added as `dependency` (not as `devDependencies`) diff --git a/packages/pnpm/src/main.ts b/packages/pnpm/src/main.ts index 6ac4df790f9..428125abffe 100644 --- a/packages/pnpm/src/main.ts +++ b/packages/pnpm/src/main.ts @@ -196,9 +196,9 @@ export default async function run (inputArgv: string[]) { ] const relativeWSDirPath = () => path.relative(process.cwd(), wsDir) || '.' if (config.workspaceRoot) { - filters.push({ filter: `{${relativeWSDirPath()}}`, followProdDepsOnly: false }) + filters.push({ filter: `{${relativeWSDirPath()}}`, followProdDepsOnly: Boolean(config.filterProd.length) }) } else if (!config.includeWorkspaceRoot && (cmd === 'run' || cmd === 'exec' || cmd === 'add' || cmd === 'test')) { - filters.push({ filter: `!{${relativeWSDirPath()}}`, followProdDepsOnly: false }) + filters.push({ filter: `!{${relativeWSDirPath()}}`, followProdDepsOnly: Boolean(config.filterProd.length) }) } const filterResults = await filterPackages(allProjects, filters, { diff --git a/packages/pnpm/test/filterProd.test.ts b/packages/pnpm/test/filterProd.test.ts new file mode 100644 index 00000000000..2f962bd9dbc --- /dev/null +++ b/packages/pnpm/test/filterProd.test.ts @@ -0,0 +1,57 @@ +import path from 'path' +import writeYamlFile from 'write-yaml-file' +import { execPnpm } from './utils' +import { + preparePackages, +} from '@pnpm/prepare' + +test.each([ + { message: '--filter should include devDependencies', filter: '--filter', expected: ['project-1', 'project-3', 'project-4'] }, + { message: '--filter-prod should not include devDependencies', filter: '--filter-prod', expected: ['project-1', 'project-3'] }, +])('$message', async ({ filter, expected }) => { + preparePackages([ + { + name: 'project-1', + version: '1.0.0', + dependencies: { 'project-2': '1.0.0', 'project-3': '1.0.0' }, + devDependencies: { 'json-append': '1' }, + scripts: { + test: 'node -e "process.stdout.write(\'project-1\')" | json-append ../output.json', + }, + }, + { + name: 'project-2', + version: '1.0.0', + dependencies: {}, + devDependencies: { 'json-append': '1' }, + scripts: { + test: 'node -e "process.stdout.write(\'project-2\')" | json-append ../output.json', + }, + }, + { + name: 'project-3', + version: '1.0.0', + dependencies: { 'project-2': '1.0.0' }, + devDependencies: { 'json-append': '1' }, + scripts: { + test: 'node -e "process.stdout.write(\'project-3\')" | json-append ../output.json', + }, + }, + { + name: 'project-4', + version: '1.0.0', + dependencies: {}, + devDependencies: { 'json-append': '1', 'project-3': '1.0.0' }, + scripts: { + test: 'node -e "process.stdout.write(\'project-4\')" | json-append ../output.json', + }, + }, + ]) + + await writeYamlFile('pnpm-workspace.yaml', { packages: ['**', '!store/**'] }) + await execPnpm(['install']) + + await execPnpm(['recursive', 'test', filter, '...project-3']) + const { default: output } = await import(path.resolve('output.json')) + expect(output.sort()).toStrictEqual(expected) +}) \ No newline at end of file