Skip to content

Commit

Permalink
fix: filter-prod flag including all workspace pkgs (#5437)
Browse files Browse the repository at this point in the history
Co-authored-by: maxwellium <896080+maxwellium@users.noreply.github.com>
  • Loading branch information
noorulh06 and maxwellium committed Oct 1, 2022
1 parent 5beb4e2 commit 5179dfe
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
9 changes: 9 additions & 0 deletions .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`)
4 changes: 2 additions & 2 deletions packages/pnpm/src/main.ts
Expand Up @@ -176,9 +176,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 filterPackagesFromDir(wsDir, filters, {
Expand Down
57 changes: 57 additions & 0 deletions 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)
})

0 comments on commit 5179dfe

Please sign in to comment.