Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/install/PackageManager/add_remove_with_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
16 changes: 12 additions & 4 deletions src/install/PackageManager/workspace_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@ use crate::{PackageID, PackageNameHash};
pub struct Candidate<'a> {
pub name: &'a [u8],
pub abs_posix_dir: &'a [u8],
/// 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,
}

impl Candidate<'_> {
fn dirs(&self) -> impl Iterator<Item = &[u8]> {
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,
Expand Down Expand Up @@ -141,9 +149,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;
Expand All @@ -153,7 +160,7 @@ fn base_matches(base: &Base, c: &Candidate<'_>, explicit_root_only: bool) -> boo
_ => return false,
}
}
}
}),
}
}

Expand Down Expand Up @@ -436,6 +443,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();
Expand Down
98 changes: 87 additions & 11 deletions src/runtime/cli/filter_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -145,9 +146,26 @@ fn get_candidate_package_patterns<'a>(
pub(crate) struct WorkspacePackage {
pub(crate) package_json_path: Box<[u8]>,
pub(crate) dir: Box<[u8]>,
/// Directory symlinks that also reach this package. A path filter matches any of them.
alias_dirs: Vec<Box<[u8]>>,
pub(crate) json: bun_resolver::PackageJSON,
}

/// 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),
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<WorkspacePackage>,
Expand Down Expand Up @@ -178,13 +196,61 @@ pub(crate) fn select_packages(

let mut iter = PackageFilterIterator::init(&glob_patterns, &root_dir)?;
let mut discovered: Vec<WorkspacePackage> = Vec::new();
// Whether `discovered[i].dir` goes through a directory symlink.
let mut dir_is_link: Vec<bool> = 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();
// Index into `discovered` by real directory. `usize::MAX` when its package.json did not parse.
let mut by_real_dir: StringHashMap<usize> = 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;
}
let dir = strings::without_trailing_slash(resolve_path::dirname::<platform::Auto>(
&package_json_path,
));
if ctx.workspaces && dir == &*root_dir {
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,
};
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
// A link under a glob that points back at the root is not a package. Under --workspaces
// the root itself is not one either.
Comment thread
robobun marked this conversation as resolved.
Resolved
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);
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;
Comment thread
robobun marked this conversation as resolved.
Resolved
} else {
package.alias_dirs.push(dir.into());
}
continue;
}
*slot.value_ptr = usize::MAX;
let Some(json) = bun_resolver::PackageJSON::parse::<{ IncludeDependencies::Main }>(
resolver,
dir,
Expand All @@ -198,33 +264,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<Box<[u8]>> = discovered
let mut to_posix = |dir: &[u8]| -> Box<[u8]> {
strings::without_trailing_slash(resolve_path::join_abs_string_buf::<platform::Posix>(
dir,
&mut path_buf.0,
&[b".".as_slice()],
))
.into()
};
let posix_dirs: Vec<Box<[u8]>> = discovered.iter().map(|p| to_posix(&p.dir)).collect();
let posix_alias_dirs: Vec<Vec<Box<[u8]>>> = discovered
.iter()
.map(|p| {
strings::without_trailing_slash(resolve_path::join_abs_string_buf::<platform::Posix>(
&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<Candidate<'_>> = 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();
Expand Down
169 changes: 165 additions & 4 deletions test/cli/run/filter-workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
});
Expand Down Expand Up @@ -764,6 +765,166 @@ 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", {
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 }),
});
}

test.each([
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
["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 runBuild(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 runBuild(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 runBuild(String(dir), ["--filter", "*"]);
expect({ ran, warnings: stderr.split("skipping this workspace package").length - 1, exitCode }).toEqual({
ran: ["ran-app"],
warnings: 1,
exitCode: 0,
});
});
});

// #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 });
});

// 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.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), args);
expect({ ran: result.ran, exitCode: result.exitCode }).toEqual({ ran: ["ran-real"], exitCode: 0 });
},
);
});

describe("selectors", () => {
test("'foo...' runs foo and the workspaces it depends on", () => {
runInCwdSuccess({
Expand Down
Loading