From 28628568abb058159dc51dbeebe0fba0a792b5ab Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Fri, 18 Sep 2026 13:52:17 +0000 Subject: [PATCH 1/7] run: run a package once when two "workspaces" entries match it `bun run --filter`, `--workspaces`, `--parallel` and `--sequential` walk each "workspaces" entry on its own. Two entries that match the same package.json, for example "packages/*" and "packages/legacy", put the package in the list twice, so its script ran twice. `select_packages` now skips a package.json path it already has. The first match keeps its place, so the discovery order does not change. --- src/runtime/cli/filter_arg.rs | 6 +++ test/cli/run/filter-workspace.test.ts | 56 +++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/src/runtime/cli/filter_arg.rs b/src/runtime/cli/filter_arg.rs index 55941cf2a477..a24358392021 100644 --- a/src/runtime/cli/filter_arg.rs +++ b/src/runtime/cli/filter_arg.rs @@ -2,6 +2,7 @@ use core::ptr::NonNull; use bstr::BStr; use bun_ast::{self, ExprData, Log}; +use bun_collections::StringHashMap; use bun_core::Global; use bun_core::Output; use bun_core::{ZStr, strings}; @@ -178,7 +179,12 @@ pub(crate) fn select_packages( let mut iter = PackageFilterIterator::init(&glob_patterns, &root_dir)?; let mut discovered: Vec = Vec::new(); + // Each "workspaces" entry is walked on its own, so two entries can yield the same path. + let mut seen_paths: StringHashMap<()> = StringHashMap::default(); while let Some(package_json_path) = iter.next()? { + if seen_paths.get_or_put(&package_json_path)?.found_existing { + continue; + } let dir = strings::without_trailing_slash(resolve_path::dirname::( &package_json_path, )); diff --git a/test/cli/run/filter-workspace.test.ts b/test/cli/run/filter-workspace.test.ts index ffeb5be84746..cb01955cd70b 100644 --- a/test/cli/run/filter-workspace.test.ts +++ b/test/cli/run/filter-workspace.test.ts @@ -764,6 +764,62 @@ describe("bun", () => { }); }); +describe.concurrent('"workspaces" entries that match the same package', () => { + function fixture(workspaces: string[] | { packages: string[] }) { + return tempDir("filter-workspaces-overlap", { + packages: { + app: { "package.json": JSON.stringify({ name: "app", scripts: { build: "echo ran-app" } }) }, + legacy: { "package.json": JSON.stringify({ name: "legacy", scripts: { build: "echo ran-legacy" } }) }, + }, + "package.json": JSON.stringify({ name: "root", private: true, workspaces }), + }); + } + + // `ran` has one element per script run, so a package that runs twice is listed twice. + async function run(cwd: string, args: string[]) { + await using proc = Bun.spawn({ + cmd: [bunExe(), "run", ...args, "build"], + cwd, + env: bunEnv, + stdout: "pipe", + stderr: "pipe", + }); + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + return { ran: (stdout.match(/ran-\w+/g) ?? []).sort(), stderr, exitCode }; + } + + test.each([ + ["a glob and a path", ["packages/*", "packages/legacy"]], + ["two globs", ["packages/*", "packages/l*"]], + ["one path written two ways", ["packages/app", "packages/legacy", "./packages/legacy/"]], + ['the { "packages": [...] } form', { packages: ["packages/*", "packages/legacy"] }], + ])("%s: each package runs once", async (_, workspaces) => { + using dir = fixture(workspaces); + const { ran, exitCode } = await run(String(dir), ["--filter", "*"]); + expect({ ran, exitCode }).toEqual({ ran: ["ran-app", "ran-legacy"], exitCode: 0 }); + }); + + test.each([[["--workspaces"]], [["--parallel", "--workspaces"]], [["--sequential", "--filter", "*"]]])( + "%j runs each package once", + async args => { + using dir = fixture(["packages/*", "packages/legacy"]); + const { ran, exitCode } = await run(String(dir), args); + expect({ ran, exitCode }).toEqual({ ran: ["ran-app", "ran-legacy"], exitCode: 0 }); + }, + ); + + test("a package.json that fails to parse is reported once", async () => { + using dir = fixture(["packages/*", "packages/legacy"]); + await Bun.write(join(String(dir), "packages", "legacy", "package.json"), "this is { not valid json"); + const { ran, stderr, exitCode } = await run(String(dir), ["--filter", "*"]); + expect({ ran, warnings: stderr.split("skipping this workspace package").length - 1, exitCode }).toEqual({ + ran: ["ran-app"], + warnings: 1, + exitCode: 0, + }); + }); +}); + describe("selectors", () => { test("'foo...' runs foo and the workspaces it depends on", () => { runInCwdSuccess({ From 0242fe551d5443937ca0cda8beba1bb2acc8b217 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Fri, 18 Sep 2026 21:46:47 +0000 Subject: [PATCH 2/7] run: run a workspace package once when a symlink also points to it `bun run --filter` and `bun run --workspaces` walk each "workspaces" glob with an accessor that reports a directory symlink as a directory. A link under the glob makes the walk find the same package.json twice, and the package runs twice. Dedupe the discovered packages by the real path of their directory. The path that is not a link is the one that runs. The other paths stay on the package as aliases, and a path filter matches through any of them. So `--filter ./packages/alias`, a relational path filter, and a negated path filter all treat the link and its target as one package. --- .../PackageManager/add_remove_with_filter.rs | 1 + .../PackageManager/workspace_selection.rs | 17 ++- src/runtime/cli/filter_arg.rs | 91 ++++++++++++++-- test/cli/run/filter-workspace.test.ts | 103 ++++++++++++++---- 4 files changed, 178 insertions(+), 34 deletions(-) diff --git a/src/install/PackageManager/add_remove_with_filter.rs b/src/install/PackageManager/add_remove_with_filter.rs index 1ae652f46c3e..75901ee33089 100644 --- a/src/install/PackageManager/add_remove_with_filter.rs +++ b/src/install/PackageManager/add_remove_with_filter.rs @@ -221,6 +221,7 @@ pub(crate) fn select_targets( .map(|(target, subject)| Candidate { name: &target.name, abs_posix_dir: subject, + alias_posix_dirs: &[], is_root: target.name_hash.is_none(), }) .collect(); diff --git a/src/install/PackageManager/workspace_selection.rs b/src/install/PackageManager/workspace_selection.rs index 531b1b411411..70135079ff1b 100644 --- a/src/install/PackageManager/workspace_selection.rs +++ b/src/install/PackageManager/workspace_selection.rs @@ -16,9 +16,18 @@ use crate::{PackageID, PackageNameHash}; pub struct Candidate<'a> { pub name: &'a [u8], pub abs_posix_dir: &'a [u8], + /// Other directories that reach the same package (directory symlinks). A path selector + /// matches the candidate through any of them. + pub alias_posix_dirs: &'a [Box<[u8]>], pub is_root: bool, } +impl Candidate<'_> { + fn dirs(&self) -> impl Iterator { + core::iter::once(self.abs_posix_dir).chain(self.alias_posix_dirs.iter().map(|d| &**d)) + } +} + #[derive(Clone, Copy, PartialEq, Eq)] pub enum RootSelection { Implicit, @@ -141,9 +150,8 @@ fn base_matches(base: &Base, c: &Candidate<'_>, explicit_root_only: bool) -> boo } } Base::Name(glob) => bun_glob::r#match(glob, c.name).matches(), - Base::Path(glob) => bun_glob::r#match(glob, c.abs_posix_dir).matches(), - Base::Subtree(glob) => { - let mut dir = c.abs_posix_dir; + Base::Path(glob) => c.dirs().any(|dir| bun_glob::r#match(glob, dir).matches()), + Base::Subtree(glob) => c.dirs().any(|mut dir| { loop { if bun_glob::r#match(glob, dir).matches() { return true; @@ -153,7 +161,7 @@ fn base_matches(base: &Base, c: &Candidate<'_>, explicit_root_only: bool) -> boo _ => return false, } } - } + }), } } @@ -436,6 +444,7 @@ pub fn select_lockfile_workspaces( .map(|(&pkg_id, dir)| Candidate { name: pkg_names[pkg_id as usize].slice(string_buf), abs_posix_dir: dir, + alias_posix_dirs: &[], is_root: pkg_resolutions[pkg_id as usize].tag == ResolutionTag::Root, }) .collect(); diff --git a/src/runtime/cli/filter_arg.rs b/src/runtime/cli/filter_arg.rs index a24358392021..ae4c4ff68e1c 100644 --- a/src/runtime/cli/filter_arg.rs +++ b/src/runtime/cli/filter_arg.rs @@ -146,9 +146,28 @@ fn get_candidate_package_patterns<'a>( pub(crate) struct WorkspacePackage { pub(crate) package_json_path: Box<[u8]>, pub(crate) dir: Box<[u8]>, + /// Other paths the walk found for the same real directory (directory symlinks). A path + /// filter matches any of them. + alias_dirs: Vec>, pub(crate) json: bun_resolver::PackageJSON, } +/// Whether `dir` goes through a directory symlink below the workspace root: its real path is not +/// the root's real path plus the same relative part. +fn is_link_path(root_dir: &[u8], root_real_dir: &[u8], dir: &[u8], real_dir: &[u8]) -> bool { + let (Some(rel), Some(real_rel)) = ( + dir.strip_prefix(root_dir), + real_dir.strip_prefix(root_real_dir), + ) else { + return true; + }; + if cfg!(windows) { + !strings::eql_case_insensitive_ascii_check_length(rel, real_rel) + } else { + rel != real_rel + } +} + pub(crate) struct SelectedPackages { pub(crate) root_dir: Box<[u8]>, pub(crate) packages: Vec, @@ -179,8 +198,24 @@ pub(crate) fn select_packages( let mut iter = PackageFilterIterator::init(&glob_patterns, &root_dir)?; let mut discovered: Vec = Vec::new(); + // Whether `discovered[i].dir` goes through a directory symlink. + let mut dir_is_link: Vec = Vec::new(); // Each "workspaces" entry is walked on its own, so two entries can yield the same path. let mut seen_paths: StringHashMap<()> = StringHashMap::default(); + // A directory symlink that a glob matches makes the walk find one package.json under two + // paths. Index into `discovered` by real directory, or `usize::MAX` when its package.json + // did not parse. + let mut by_real_dir: StringHashMap = StringHashMap::default(); + let mut dir_z_buf = path_buffer_pool::get(); + let mut real_dir_buf = path_buffer_pool::get(); + let mut root_real_buf = path_buffer_pool::get(); + let root_real_dir: &[u8] = match bun_sys::realpath( + resolve_path::z(&root_dir, &mut dir_z_buf), + &mut root_real_buf, + ) { + Ok(real) => real, + Err(_) => &root_dir, + }; while let Some(package_json_path) = iter.next()? { if seen_paths.get_or_put(&package_json_path)?.found_existing { continue; @@ -191,6 +226,32 @@ pub(crate) fn select_packages( if ctx.workspaces && dir == &*root_dir { continue; } + let real_dir: &[u8] = + match bun_sys::realpath(resolve_path::z(dir, &mut dir_z_buf), &mut real_dir_buf) { + Ok(real) => real, + Err(_) => dir, + }; + let is_link = is_link_path(&root_dir, root_real_dir, dir, real_dir); + let slot = by_real_dir.get_or_put(real_dir)?; + if slot.found_existing { + let i = *slot.value_ptr; + if i == usize::MAX { + continue; + } + let package = &mut discovered[i]; + if dir_is_link[i] && !is_link { + // The path that is not a link is the one that runs. + package + .alias_dirs + .push(core::mem::replace(&mut package.dir, dir.into())); + package.package_json_path = package_json_path; + dir_is_link[i] = false; + } else { + package.alias_dirs.push(dir.into()); + } + continue; + } + *slot.value_ptr = usize::MAX; let Some(json) = bun_resolver::PackageJSON::parse::<{ IncludeDependencies::Main }>( resolver, dir, @@ -204,33 +265,43 @@ pub(crate) fn select_packages( ); continue; }; + *slot.value_ptr = discovered.len(); discovered.push(WorkspacePackage { dir: dir.into(), + alias_dirs: Vec::new(), package_json_path, json, }); + dir_is_link.push(is_link); } + drop(dir_z_buf); + drop(real_dir_buf); + drop(root_real_buf); let mut path_buf = path_buffer_pool::get(); - let posix_dirs: Vec> = discovered + let mut to_posix = |dir: &[u8]| -> Box<[u8]> { + strings::without_trailing_slash(resolve_path::join_abs_string_buf::( + dir, + &mut path_buf.0, + &[b".".as_slice()], + )) + .into() + }; + let posix_dirs: Vec> = discovered.iter().map(|p| to_posix(&p.dir)).collect(); + let posix_alias_dirs: Vec>> = discovered .iter() - .map(|p| { - strings::without_trailing_slash(resolve_path::join_abs_string_buf::( - &p.dir, - &mut path_buf.0, - &[b".".as_slice()], - )) - .into() - }) + .map(|p| p.alias_dirs.iter().map(|d| to_posix(d)).collect()) .collect(); drop(path_buf); let candidates: Vec> = discovered .iter() .zip(&posix_dirs) - .map(|(p, dir)| Candidate { + .zip(&posix_alias_dirs) + .map(|((p, dir), alias_dirs)| Candidate { name: &p.json.name, abs_posix_dir: dir, + alias_posix_dirs: alias_dirs, is_root: false, }) .collect(); diff --git a/test/cli/run/filter-workspace.test.ts b/test/cli/run/filter-workspace.test.ts index cb01955cd70b..c5da2936524d 100644 --- a/test/cli/run/filter-workspace.test.ts +++ b/test/cli/run/filter-workspace.test.ts @@ -723,12 +723,13 @@ describe("bun", () => { }); const stdoutval = stdout.toString(); const count = (needle: string) => stdoutval.split(needle).length - 1; - // `pkga` is matched once. `cyc` is matched at `packages/cyc` and once more - // through its own `loop` alias, where the cycle is detected and descent - // stops instead of recursing until the path length limit. + // The walk reaches `cyc` at `packages/cyc` and once more through its own + // `loop` alias, where the cycle is detected and descent stops instead of + // recursing until the path length limit. Both paths are one package, so + // it runs once. expect({ scripta: count("scripta"), scriptcyc: count("scriptcyc"), exitCode }).toEqual({ scripta: 1, - scriptcyc: 2, + scriptcyc: 1, exitCode: 0, }); }); @@ -764,6 +765,20 @@ describe("bun", () => { }); }); +// Runs the `build` script. `ran` has one element per script run, so a package +// that runs twice is listed twice. +async function runBuild(cwd: string, args: string[]) { + await using proc = Bun.spawn({ + cmd: [bunExe(), "run", ...args, "build"], + cwd, + env: bunEnv, + stdout: "pipe", + stderr: "pipe", + }); + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + return { ran: (stdout.match(/ran-\w+/g) ?? []).sort(), stderr, exitCode }; +} + describe.concurrent('"workspaces" entries that match the same package', () => { function fixture(workspaces: string[] | { packages: string[] }) { return tempDir("filter-workspaces-overlap", { @@ -775,19 +790,6 @@ describe.concurrent('"workspaces" entries that match the same package', () => { }); } - // `ran` has one element per script run, so a package that runs twice is listed twice. - async function run(cwd: string, args: string[]) { - await using proc = Bun.spawn({ - cmd: [bunExe(), "run", ...args, "build"], - cwd, - env: bunEnv, - stdout: "pipe", - stderr: "pipe", - }); - const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); - return { ran: (stdout.match(/ran-\w+/g) ?? []).sort(), stderr, exitCode }; - } - test.each([ ["a glob and a path", ["packages/*", "packages/legacy"]], ["two globs", ["packages/*", "packages/l*"]], @@ -795,7 +797,7 @@ describe.concurrent('"workspaces" entries that match the same package', () => { ['the { "packages": [...] } form', { packages: ["packages/*", "packages/legacy"] }], ])("%s: each package runs once", async (_, workspaces) => { using dir = fixture(workspaces); - const { ran, exitCode } = await run(String(dir), ["--filter", "*"]); + const { ran, exitCode } = await runBuild(String(dir), ["--filter", "*"]); expect({ ran, exitCode }).toEqual({ ran: ["ran-app", "ran-legacy"], exitCode: 0 }); }); @@ -803,7 +805,7 @@ describe.concurrent('"workspaces" entries that match the same package', () => { "%j runs each package once", async args => { using dir = fixture(["packages/*", "packages/legacy"]); - const { ran, exitCode } = await run(String(dir), args); + const { ran, exitCode } = await runBuild(String(dir), args); expect({ ran, exitCode }).toEqual({ ran: ["ran-app", "ran-legacy"], exitCode: 0 }); }, ); @@ -811,7 +813,7 @@ describe.concurrent('"workspaces" entries that match the same package', () => { test("a package.json that fails to parse is reported once", async () => { using dir = fixture(["packages/*", "packages/legacy"]); await Bun.write(join(String(dir), "packages", "legacy", "package.json"), "this is { not valid json"); - const { ran, stderr, exitCode } = await run(String(dir), ["--filter", "*"]); + const { ran, stderr, exitCode } = await runBuild(String(dir), ["--filter", "*"]); expect({ ran, warnings: stderr.split("skipping this workspace package").length - 1, exitCode }).toEqual({ ran: ["ran-app"], warnings: 1, @@ -820,6 +822,67 @@ describe.concurrent('"workspaces" entries that match the same package', () => { }); }); +// #43358: `packages/*` matches `packages/alias` (a link to `packages/real`) +// and `packages/real`. Both are one package, so it runs once. +describe.concurrent("a directory symlink to a workspace package", () => { + function fixture() { + const dir = tempDir("filter-symlink-alias", { + packages: { + real: { + "package.json": JSON.stringify({ name: "real", scripts: { build: "echo ran-real" } }), + }, + other: { + "package.json": JSON.stringify({ + name: "other", + dependencies: { real: "workspace:*" }, + scripts: { build: "echo ran-other" }, + }), + }, + }, + "package.json": JSON.stringify({ name: "root", private: true, workspaces: ["packages/*"] }), + }); + // "junction" so the link is creatable on unprivileged Windows; the type + // is ignored on POSIX. + symlinkSync(join(dir, "packages", "real"), join(dir, "packages", "alias"), "junction"); + return dir; + } + + test.each([ + [ + ["--filter", "*"], + ["ran-other", "ran-real"], + ], + [["--workspaces"], ["ran-other", "ran-real"]], + [["--filter", "real"], ["ran-real"]], + [ + ["--filter", "other..."], + ["ran-other", "ran-real"], + ], + [["--filter", "./packages/alias"], ["ran-real"]], + [["--filter", "./packages/real"], ["ran-real"]], + [ + ["--filter", "./packages/*"], + ["ran-other", "ran-real"], + ], + // A relational path filter finds the dependents through either path. + [ + ["--filter", "...{./packages/real}"], + ["ran-other", "ran-real"], + ], + [ + ["--filter", "...{./packages/alias}"], + ["ran-other", "ran-real"], + ], + // A negated path filter on either path excludes the package. + [["--filter", "*", "--filter", "!./packages/alias"], ["ran-other"]], + [["--filter", "*", "--filter", "!./packages/real"], ["ran-other"]], + ])("%j runs each package once", async (args, ran) => { + using dir = fixture(); + const result = await runBuild(String(dir), args); + expect({ ran: result.ran, exitCode: result.exitCode }).toEqual({ ran, exitCode: 0 }); + }); +}); + describe("selectors", () => { test("'foo...' runs foo and the workspaces it depends on", () => { runInCwdSuccess({ From 7c734ffcaa815b5f3c437744733e592452b35d27 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Fri, 18 Sep 2026 21:48:21 +0000 Subject: [PATCH 3/7] run: shorten the comments on the alias dir fields --- src/install/PackageManager/workspace_selection.rs | 3 +-- src/runtime/cli/filter_arg.rs | 10 +++------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/install/PackageManager/workspace_selection.rs b/src/install/PackageManager/workspace_selection.rs index 70135079ff1b..d176bfa01f02 100644 --- a/src/install/PackageManager/workspace_selection.rs +++ b/src/install/PackageManager/workspace_selection.rs @@ -16,8 +16,7 @@ use crate::{PackageID, PackageNameHash}; pub struct Candidate<'a> { pub name: &'a [u8], pub abs_posix_dir: &'a [u8], - /// Other directories that reach the same package (directory symlinks). A path selector - /// matches the candidate through any of them. + /// Directory symlinks that also reach this package. A path selector matches any of them. pub alias_posix_dirs: &'a [Box<[u8]>], pub is_root: bool, } diff --git a/src/runtime/cli/filter_arg.rs b/src/runtime/cli/filter_arg.rs index ae4c4ff68e1c..7c77b45eb404 100644 --- a/src/runtime/cli/filter_arg.rs +++ b/src/runtime/cli/filter_arg.rs @@ -146,14 +146,12 @@ fn get_candidate_package_patterns<'a>( pub(crate) struct WorkspacePackage { pub(crate) package_json_path: Box<[u8]>, pub(crate) dir: Box<[u8]>, - /// Other paths the walk found for the same real directory (directory symlinks). A path - /// filter matches any of them. + /// Directory symlinks that also reach this package. A path filter matches any of them. alias_dirs: Vec>, pub(crate) json: bun_resolver::PackageJSON, } -/// Whether `dir` goes through a directory symlink below the workspace root: its real path is not -/// the root's real path plus the same relative part. +/// Whether `dir` goes through a directory symlink below the workspace root. fn is_link_path(root_dir: &[u8], root_real_dir: &[u8], dir: &[u8], real_dir: &[u8]) -> bool { let (Some(rel), Some(real_rel)) = ( dir.strip_prefix(root_dir), @@ -202,9 +200,7 @@ pub(crate) fn select_packages( let mut dir_is_link: Vec = Vec::new(); // Each "workspaces" entry is walked on its own, so two entries can yield the same path. let mut seen_paths: StringHashMap<()> = StringHashMap::default(); - // A directory symlink that a glob matches makes the walk find one package.json under two - // paths. Index into `discovered` by real directory, or `usize::MAX` when its package.json - // did not parse. + // Index into `discovered` by real directory. `usize::MAX` when its package.json did not parse. let mut by_real_dir: StringHashMap = StringHashMap::default(); let mut dir_z_buf = path_buffer_pool::get(); let mut real_dir_buf = path_buffer_pool::get(); From cf6a3c89cea85280009ae639eb714256e0dd2c86 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Fri, 18 Sep 2026 22:10:36 +0000 Subject: [PATCH 4/7] run: skip the root by real dir under --workspaces, test which path runs --- src/runtime/cli/filter_arg.rs | 7 ++--- test/cli/run/filter-workspace.test.ts | 39 +++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/runtime/cli/filter_arg.rs b/src/runtime/cli/filter_arg.rs index 7c77b45eb404..0f828296a4de 100644 --- a/src/runtime/cli/filter_arg.rs +++ b/src/runtime/cli/filter_arg.rs @@ -219,14 +219,15 @@ pub(crate) fn select_packages( let dir = strings::without_trailing_slash(resolve_path::dirname::( &package_json_path, )); - if ctx.workspaces && dir == &*root_dir { - continue; - } let real_dir: &[u8] = match bun_sys::realpath(resolve_path::z(dir, &mut dir_z_buf), &mut real_dir_buf) { Ok(real) => real, Err(_) => dir, }; + // The root is not a workspace package, also when a link under a glob points back at it. + if ctx.workspaces && (dir == &*root_dir || real_dir == root_real_dir) { + continue; + } let is_link = is_link_path(&root_dir, root_real_dir, dir, real_dir); let slot = by_real_dir.get_or_put(real_dir)?; if slot.found_existing { diff --git a/test/cli/run/filter-workspace.test.ts b/test/cli/run/filter-workspace.test.ts index c5da2936524d..9c70fb064b8e 100644 --- a/test/cli/run/filter-workspace.test.ts +++ b/test/cli/run/filter-workspace.test.ts @@ -881,6 +881,45 @@ describe.concurrent("a directory symlink to a workspace package", () => { const result = await runBuild(String(dir), args); expect({ ran: result.ran, exitCode: result.exitCode }).toEqual({ ran, exitCode: 0 }); }); + + // The error names `package.dir`, so it shows which of the two paths is the package. + test("the real path is the package, also when the link is found first", async () => { + using dir = tempDir("filter-symlink-first", { + packages: { + real: { "package.json": JSON.stringify({ name: "real", scripts: { other: "echo" } }) }, + }, + "package.json": JSON.stringify({ + name: "root", + private: true, + workspaces: ["packages/alias", "packages/real"], + }), + }); + symlinkSync(join(dir, "packages", "real"), join(dir, "packages", "alias"), "junction"); + const { stderr, exitCode } = await runBuild(String(dir), ["--workspaces"]); + expect({ at: stderr.match(/Missing 'build' script at '(.*)'/)?.[1], exitCode }).toEqual({ + at: join(dir, "packages", "real"), + exitCode: 1, + }); + }); + + test("--workspaces does not run the root through a link back to it", async () => { + using dir = tempDir("filter-symlink-root", { + packages: { + real: { + "package.json": JSON.stringify({ name: "real", scripts: { build: "echo ran-real" } }), + }, + }, + "package.json": JSON.stringify({ + name: "root", + private: true, + workspaces: ["packages/*"], + scripts: { build: "echo ran-root" }, + }), + }); + symlinkSync(String(dir), join(dir, "packages", "up"), "junction"); + const result = await runBuild(String(dir), ["--workspaces"]); + expect({ ran: result.ran, exitCode: result.exitCode }).toEqual({ ran: ["ran-real"], exitCode: 0 }); + }); }); describe("selectors", () => { From ccea8bbe9b70f9d65451dd7b809ed3e2e2926381 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Fri, 18 Sep 2026 22:21:39 +0000 Subject: [PATCH 5/7] run: skip a link back to the root under --filter too --- src/runtime/cli/filter_arg.rs | 6 +++-- test/cli/run/filter-workspace.test.ts | 37 +++++++++++++++------------ 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/runtime/cli/filter_arg.rs b/src/runtime/cli/filter_arg.rs index 0f828296a4de..6be52a5510b0 100644 --- a/src/runtime/cli/filter_arg.rs +++ b/src/runtime/cli/filter_arg.rs @@ -224,8 +224,10 @@ pub(crate) fn select_packages( Ok(real) => real, Err(_) => dir, }; - // The root is not a workspace package, also when a link under a glob points back at it. - if ctx.workspaces && (dir == &*root_dir || real_dir == root_real_dir) { + // A link under a glob that points back at the root is not a package. Under --workspaces + // the root itself is not one either. + if (ctx.workspaces && dir == &*root_dir) || (dir != &*root_dir && real_dir == root_real_dir) + { continue; } let is_link = is_link_path(&root_dir, root_real_dir, dir, real_dir); diff --git a/test/cli/run/filter-workspace.test.ts b/test/cli/run/filter-workspace.test.ts index 9c70fb064b8e..971d62200b59 100644 --- a/test/cli/run/filter-workspace.test.ts +++ b/test/cli/run/filter-workspace.test.ts @@ -902,24 +902,27 @@ describe.concurrent("a directory symlink to a workspace package", () => { }); }); - test("--workspaces does not run the root through a link back to it", async () => { - using dir = tempDir("filter-symlink-root", { - packages: { - real: { - "package.json": JSON.stringify({ name: "real", scripts: { build: "echo ran-real" } }), + test.each([[["--workspaces"]], [["--filter", "*"]]])( + "%j does not run the root through a link back to it", + async args => { + using dir = tempDir("filter-symlink-root", { + packages: { + real: { + "package.json": JSON.stringify({ name: "real", scripts: { build: "echo ran-real" } }), + }, }, - }, - "package.json": JSON.stringify({ - name: "root", - private: true, - workspaces: ["packages/*"], - scripts: { build: "echo ran-root" }, - }), - }); - symlinkSync(String(dir), join(dir, "packages", "up"), "junction"); - const result = await runBuild(String(dir), ["--workspaces"]); - expect({ ran: result.ran, exitCode: result.exitCode }).toEqual({ ran: ["ran-real"], exitCode: 0 }); - }); + "package.json": JSON.stringify({ + name: "root", + private: true, + workspaces: ["packages/*"], + scripts: { build: "echo ran-root" }, + }), + }); + symlinkSync(String(dir), join(dir, "packages", "up"), "junction"); + const result = await runBuild(String(dir), args); + expect({ ran: result.ran, exitCode: result.exitCode }).toEqual({ ran: ["ran-real"], exitCode: 0 }); + }, + ); }); describe("selectors", () => { From daa0f4140e56c3ac250e1dca8d6275574808d995 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Fri, 18 Sep 2026 22:22:34 +0000 Subject: [PATCH 6/7] run: shorten the root skip comment --- src/runtime/cli/filter_arg.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/runtime/cli/filter_arg.rs b/src/runtime/cli/filter_arg.rs index 6be52a5510b0..fac8445f0371 100644 --- a/src/runtime/cli/filter_arg.rs +++ b/src/runtime/cli/filter_arg.rs @@ -224,8 +224,7 @@ pub(crate) fn select_packages( Ok(real) => real, Err(_) => dir, }; - // A link under a glob that points back at the root is not a package. Under --workspaces - // the root itself is not one either. + // Neither a link back at the root, nor the root itself under --workspaces, is a package. if (ctx.workspaces && dir == &*root_dir) || (dir != &*root_dir && real_dir == root_real_dir) { continue; From ab011481e859588b6eaa6344b2b6efef4de1bcc9 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Fri, 18 Sep 2026 22:33:36 +0000 Subject: [PATCH 7/7] ci: retrigger