Skip to content

Commit c121235

Browse files
fix: respect transitive dependencies when sorting filtered projects (#12688)
The recursive command runners sorted opts.selectedProjectsGraph, which keeps only the selected projects as keys. Edges to unselected projects were dropped, so a transitive dependency between two selected projects (a -> b -> c with only a and c selected) was lost and the two ran out of order. sortFilteredProjects resolves the order through the full workspace graph: for each sorted project it walks dependencies, tunneling past unselected projects and stopping at selected ones, so a transitive relationship becomes a direct edge. Projects without a real dependency stay in the same chunk, preserving concurrency. Prod-only filters (--filter-prod) prune dev edges. Their selected projects are sorted through the prod-pruned full graph, so transitive prod deps are honored without reintroducing the dropped dev edges. In a mixed selection each project is sorted through the graph that matches how it was selected: regularly filtered projects through the full graph, prod-only ones through the prod-pruned graph. Carrying prod-only projects on the full graph would let a dev-only reverse edge form a cycle and collapse a real prod dependency and its dependent into one chunk. Fixes #8335 --------- Co-authored-by: Zoltan Kochan <[email protected]>
1 parent 8491f8e commit c121235

25 files changed

Lines changed: 1201 additions & 139 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"@pnpm/workspace.projects-sorter": patch
3+
"@pnpm/workspace.projects-filter": patch
4+
"@pnpm/config.reader": patch
5+
"@pnpm/exec.commands": patch
6+
"@pnpm/building.commands": patch
7+
"@pnpm/releasing.commands": patch
8+
"pnpm": patch
9+
---
10+
11+
Fixed the topological order of `--filter`ed commands (`pnpm run`, `pnpm exec`, `pnpm publish`, `pnpm pack`, `pnpm rebuild`) when the selected projects depend on each other only transitively through projects that were not selected. Previously such selected projects could run concurrently or in the wrong order; now a project always runs after the selected projects it transitively depends on, while projects without a real dependency relationship still run concurrently. This now also holds for prod-only filters (`--filter-prod`), which resolve order through the production dependency graph so transitive production dependencies are respected without pulling back the dev dependencies the filter drops, and for selections that mix `--filter` with `--filter-prod` [#8335](https://github.com/pnpm/pnpm/issues/8335).

‎pacquet/crates/cli/src/cli_args/exec/recursive.rs‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
use super::{ExecArgs, prepare_command, spawn_in_dir};
1414
use crate::cli_args::recursive::{
1515
AutoExcludeRoot, ExecutionStatus, Status, count_failures, discover_workspace_projects,
16-
get_resumed_package_chunks, select_recursive_projects, sort_projects, write_recursive_summary,
16+
get_resumed_package_chunks, select_recursive_projects, sort_filtered_projects,
17+
write_recursive_summary,
1718
};
1819
use derive_more::{Display, Error};
1920
use indexmap::IndexMap;
@@ -65,21 +66,26 @@ pub fn exec_recursive(args: &ExecArgs, config: &Config, dir: &Path) -> miette::R
6566
return Err(RecursiveExecError::NoPackage.into());
6667
}
6768

68-
let graph = select_recursive_projects(
69+
let selection = select_recursive_projects(
6970
&projects,
7071
config,
7172
dir,
7273
AutoExcludeRoot::Enabled { workspace_patterns: patterns.as_deref() },
7374
)?;
74-
// An empty `--filter` selection is a no-op, matching pnpm's exit-0 for
75-
// an empty selectedProjectsGraph.
75+
let graph = &selection.selected;
76+
// An empty `--filter` selection is a no-op (exit 0).
7677
if graph.is_empty() {
7778
return Ok(());
7879
}
7980

80-
let mut chunks = sort_projects(&graph);
81+
let mut chunks = sort_filtered_projects(
82+
graph,
83+
selection.full_graph(),
84+
selection.prod_all.as_ref(),
85+
&selection.prod_only_selected,
86+
);
8187
if let Some(resume_from) = &args.resume_from {
82-
chunks = get_resumed_package_chunks(resume_from, chunks, &graph)?;
88+
chunks = get_resumed_package_chunks(resume_from, chunks, graph)?;
8389
}
8490

8591
let bail = !args.no_bail;

‎pacquet/crates/cli/src/cli_args/pack.rs‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! yet, so they take their `false` / empty defaults.
1212
1313
use crate::cli_args::recursive::{
14-
AutoExcludeRoot, discover_workspace_projects, select_recursive_projects, sort_projects,
14+
AutoExcludeRoot, discover_workspace_projects, select_recursive_projects, sort_filtered_projects,
1515
};
1616
use clap::Args;
1717
use miette::Context;
@@ -108,8 +108,15 @@ impl PackArgs {
108108
// workspace root stays in the selection (its own name/version
109109
// eligibility check still applies below).
110110
let (projects, _patterns) = discover_workspace_projects(workspace_root)?;
111-
let graph = select_recursive_projects(&projects, config, dir, AutoExcludeRoot::Disabled)?;
112-
let chunks = sort_projects(&graph);
111+
let selection =
112+
select_recursive_projects(&projects, config, dir, AutoExcludeRoot::Disabled)?;
113+
let graph = &selection.selected;
114+
let chunks = sort_filtered_projects(
115+
graph,
116+
selection.full_graph(),
117+
selection.prod_all.as_ref(),
118+
&selection.prod_only_selected,
119+
);
113120

114121
// In recursive mode `--out` / `--pack-destination` resolves to an
115122
// absolute path against the CLI dir (and defaults the destination

0 commit comments

Comments
 (0)