Using pnpm --filter for Targeted Builds
🚨 Exact Symptoms & Impact
ERR_PNPM_FILTER_NO_MATCH: No packages matched the filter pattern '...'- Silent execution of unintended packages during CI/CD runs
- Cascading dependency graph failures and pipeline bloat caused by fallback to full-workspace execution
🔍 Root Cause Analysis
Filter resolution failures typically stem from misconfigured workspace boundaries in pnpm-workspace.yaml, incorrect dependency-aware syntax (omitting ^ or ...), or targeting invalid package names/globs. When pnpm cannot resolve the filter against the workspace manifest, it defaults to executing across the entire repository. This behavior breaks isolated build guarantees and inflates CI compute costs. Proper implementation of pnpm Workspace Filtering is critical to maintaining predictable execution scopes and preventing silent scope creep.
🛠️ Diagnostic & Recovery Workflow
1. Validate Workspace Boundaries
Confirm the workspace manifest exists at the repository root and explicitly declares package directories.
# pnpm-workspace.yaml
packages:
- 'packages/*'
- 'apps/*'
Debug Command: pnpm list --recursive --depth=0
Verifies all expected packages are recognized by the workspace resolver.
2. Test Filter Resolution (Dry Run)
Validate that your filter pattern resolves to the correct package set before triggering builds.
# Verify exact match
pnpm list --filter "my-package" --recursive
# Verify dependency-aware resolution (upstream dependencies)
pnpm list --filter "...my-package" --recursive
# Verify dependent resolution (downstream consumers)
pnpm list --filter "^my-package" --recursive
3. Execute Targeted Build
Run the build command with the validated filter. Cross-reference terminal output to ensure only the specified package and its resolved upstream dependencies execute.
pnpm --filter "...my-package" run build
📦 Required Configuration Baseline
Ensure the following files are present and correctly structured to prevent silent resolution failures:
pnpm-workspace.yaml: Must reside at the repository root.package.json: Every workspace package must contain a valid, uniquenamefield matching the filter pattern..npmrc(Recommended): Setauto-install-peers=trueto maintain consistent dependency resolution across filtered scopes. For broader architectural guidance, refer to Monorepo Architecture & Orchestration.
🛡️ CI/CD Safeguards & Prevention
Implement these controls to eliminate scope creep and cascading build failures:
- Change-Aware Filtering: Replace bare
pnpm run buildwithpnpm --filter ... --since HEAD~1 run buildto restrict execution to modified packages and their dependencies. - Production Graph Isolation: Append
--filter-prodto excludedevDependenciesfrom the resolution graph during deployment pipelines. - Pre-Commit Enforcement: Integrate
@pnpm/monorepo-lintinto pre-commit hooks to validate workspace consistency before merges. - Documentation Standardization: Mandate dependency-aware filtering (
...or^) in CI/CD templates and document approved patterns inCONTRIBUTING.md.
❓ Frequently Asked Questions
What is the difference between pnpm --filter and pnpm --filter ...?
The ... prefix triggers dependency-aware filtering. pnpm will include the target package plus all upstream dependencies required for the build to succeed, preventing missing module errors during compilation.
Why does pnpm --filter run builds on packages I didn't specify?
This occurs when using dependency-aware syntax (...) which inherently pulls in upstream dependencies, or when a package's scripts explicitly invoke unscoped workspace commands. Use --filter-prod or exact package names to strictly restrict execution.
Can I combine pnpm --filter with Turborepo or Nx?
Yes, but it is generally redundant. Turborepo and Nx provide native pipeline caching and task graph resolution. Reserve pnpm --filter for raw script execution or when explicitly bypassing orchestrator overhead.