Skip to content

run --filter: find the workspace root with install's package.json walk - #46041

Draft
robobun wants to merge 1 commit into
mainfrom
robobun/e1716a3c/filter-workspace-root-owner
Draft

robobun wants to merge 1 commit into
mainfrom
robobun/e1716a3c/filter-workspace-root-owner

Conversation

@robobun

@robobun robobun commented Oct 19, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

  • bun run --filter and --workspaces take a package.json in a sticky, shared directory above the cwd (/tmp) as the workspace root, and run every package it lists: evil build: PLANTED-SCRIPT-RAN.
  • bun install passes over that file since install: consolidate the open package manager pull requests #44968. get_candidate_package_patterns (src/runtime/cli/filter_arg.rs:49) had its own upward loop without the rule.

Fix

  • get_candidate_package_patterns now iterates package_json_walk, the PackageJsonWalk of bun install behind one exported function. Both commands pass over the same directories with the same warning.
  • PackageJsonWalk::next stats the directory from the path it holds, so the check does not allocate.
  • Verified: test/cli/run/filter-workspace.test.ts, 19 new cases, 15 fail on main. Five related files pass (Notes).
  • Self-reviewed: 5 execution concerns raised, 5 addressed. The scope concern is open (scope comment below).

Background

Downsides

  • node_modules/.bin of that directory stays first on PATH, also for bun install on main.
  • A root directly in such a directory (the root of a tmpfs mount) is not found from a member. A member script that runs bun run --filter or npm run -w then fails, also under bun install. No opt-out.
  • From a member the walk makes 12 syscalls (main 11) and 21 allocations (main 20). Release binary: +344 bytes.
Notes

The transcripts and cost numbers below were taken on main at 017b243 and on this change applied to it. The branch is now on a later main, and they were not measured again there. Since then #46021 changed the old loop for a package.json path that is too long (see below).

Reproduction. Another user owns /tmp/x/package.json ({"name":"planted","workspaces":["*"]}) and /tmp/x/evil, whose build script prints PLANTED-SCRIPT-RAN. /tmp/x has mode 1777. The project is /tmp/x/proj.

$ cd /tmp/x/proj && bun run --filter '*' build        # main
proj build: my own build
evil build: PLANTED-SCRIPT-RAN uid=0

$ bun run --filter '*' build                          # this PR
warn: ignoring package.json in /tmp/x: directory is writable by other users
proj build: my own build

--filter, -F, --workspaces, the forms without run, --parallel, --sequential and --cwd= all reach select_packages and give the same result.

The rule. It is the rule of PackageJsonWalk::next, unchanged. The open of <dir>/package.json did not fail with ENOENT, <dir> is not the directory the command started in, and <dir> is sticky and writable by its group or by everyone (a failed stat counts as shared). Bun then prints the warning, does not read the file in the walk, and goes on upward. POSIX only. A package.json there that is not JSON ended the command with error: ParserError before. It is now passed over like any other.

What stays open. None of these is changed here. Each was run on this change. The scope comment below asks which of them a maintainer wants closed.

  • node_modules/.bin of every directory above the cwd comes first on PATH (RunCommand::path_for_run), also for the lifecycle scripts of bun install.
  • In a cwd with no package.json, bun run <script>, --parallel and --sequential take the script from the nearest package.json above, and its directory is first on PATH.
  • The resolver reads every package.json above the cwd before the walk runs. In a cwd with no package.json, a script still gets npm_package_name and npm_package_json of the file that the warning names.
  • bun run --filter takes a root that does not list the cwd. bun install needs the cwd to be a member.
  • A FIFO named package.json in that directory blocks both commands in open.
  • Bare imports resolve through node_modules of every directory above, as in Node.
  • A directory that is not sticky, with a package.json that another user owns. That is the rule by file owner.

What other tools do with the same files. npm 11.16.0: npm prefix in /tmp/x/proj prints /tmp/x, and npm run build -w evil runs the planted package. npm run build and node --run build (v26.3.0) run a planted /tmp/x/node_modules/.bin/node. Neither has a rule by directory mode. The difference from npm is the one that #44968 made for bun install.

A real root in such a directory. Root at mode 1777 with workspaces: ["packages/*"], members a and b, bun run <flags> build:

cwd flags main this PR
root --filter '*', --workspaces a, b a, b
packages/ --filter '*', --workspaces a, b warning, then a, b
packages/a --filter '*' a, b warning, then a
packages/a --workspaces a, b warning, then error: No workspace packages found
packages/a --filter b b warning, then error: No workspace packages matched the filter "b"

After a pass-over with no other root above, the candidates are the packages in and below the cwd (**/package.json), as when there is no root. The last row is also what a script of a gets when it runs bun run --filter b build or npm run build -w b (which bun rewrites), while the command was started in the root. With that script as the postinstall of a, bun install in the root ends with error: postinstall script from "a" exited with 1. On main all three work. To keep such a tree working, move it one directory down, or remove the sticky bit or the group and world write bits from the directory.

Costs, measured. Debug builds of main and of this change, gdb with a syscall catchpoint and breakpoints on the Rust allocator entry points, counted between entry and return of get_candidate_package_patterns. Command: bun run --filter '*' --if-present nosuchscript. Cases: the cwd is the root, a member, packages/ with no manifest, a project with no root above it, and the mode-1777 case above.

  • Syscalls: main 5 / 11 / 6 / 8 / 10, PR 5 / 12 / 7 / 8 / 12. The added one is the stat of a directory above the cwd that has a package.json. The last number includes the write of the warning.
  • Heap allocations (first four cases): main 12 / 20 / 13 / 13, PR 13 / 21 / 14 / 14. The added one is the path Vec of the walk, once per process.
  • Pooled path-buffer borrows: main 3 / 9 / 6 / 12, PR 3 / 7 / 4 / 6. Path joins: main 1 / 3 / 2 / 4, PR 1 / 2 / 1 / 1.
  • bun pm ls from a member, allocations inside PackageJsonWalk::next: main 2, PR 1.
  • Release builds, size: text 106,738,870 to 106,739,214 bytes (+344), data 116,624 in both.
  • warn: ignoring package.json lines for one command with one nested bun run --filter, below a stray package.json in a mode-1777 directory: main 0, PR 2.
  • Plain bun run <script> does not reach this code.

Long working directory. #46021 made the old loop treat a package.json path that does not fit a path buffer like a missing file, and go on to the parent directory. The walk returns ENAMETOOLONG for such a path, and this PR passes over it in the same way. The tests that #46021 added for it pass.

Tests. test/cli/run/filter-workspace.test.ts, block "package.json in a sticky, shared directory above the cwd": 19 cases. No case needs a second user. As uid 65534 all 19 run: 15 fail on a debug build of main, and 4 are controls that pass both ways. As root 18 run, because one case needs a file that cannot be opened. With the debug build of this PR: filter-workspace.test.ts 149 pass, test/cli/install/bad-workspace.test.ts 44 pass (file unchanged), test/cli/install/bun-workspaces.test.ts 285 pass, test/cli/run/workspaces.test.ts 4 pass, test/cli/run/run_command.test.ts 33 pass, test/cli/run/multi-run.test.ts 132 pass. On an earlier base, cargo check --workspace passed for x86_64-pc-windows-msvc and aarch64-apple-darwin.

Related. An earlier branch for this command had only the rule by file owner. It was never a PR, and this PR replaces it. #41797 (tsconfig.json) and #29310 (bunfig.toml) are open PRs with their own upward lookups.


no test proof · iteration 1 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/cli/run/filter-workspace.test.ts

@robobun

robobun commented Oct 19, 2026

Copy link
Copy Markdown
Collaborator Author

This draft needs a scope decision from a maintainer.

#44968 decided the rule for bun install: a package.json in a sticky, shared directory above the cwd is passed over, and there is no rule by file owner. No one decided it for bun run. The question I asked on #41759 (#41759 (comment)) has no answer yet. This PR is the smallest answer to it. These are the choices:

  1. bun install only. bun run stays as it is, like npm and node --run. Close this PR.
  2. The workspace root only. Merge this PR. bun install and bun run --filter / --workspaces then agree on which package.json can be the root. The routes below stay open, and the docs say so.
  3. Every route from such a directory. One exported predicate with a named opt-out, also for node_modules/.bin on PATH and for the script lookup in the nearest package.json. This PR is then the first step.

This is what choice 2 leaves open. Another user owns /tmp/x/node_modules/.bin/node. The project is /tmp/x/proj, and its script is "build": "node --version".

$ cd /tmp/x/proj && bun run --filter proj build      # this branch
warn: ignoring package.json in /tmp/x: directory is writable by other users
proj build: PLANTED-node-RAN uid=0 from /tmp/x/node_modules/.bin/node

$ bun install                                        # main, "postinstall": "node --version"
warn: ignoring package.json in /tmp/x: directory is writable by other users
$ node --version
PLANTED-node-RAN uid=0 from /tmp/x/node_modules/.bin/node
  • npm run build (11.16.0) and node --run build (v26.3.0) run the planted node too.
  • With only node_modules/.bin/node in /tmp/x and no package.json, no warning is printed.
  • In a cwd with no package.json, plain bun run hello runs scripts.hello of /tmp/x/package.json with no warning.

No user issue reports this. A directory that is not sticky, with a package.json that another user owns, is not changed by this PR: that is the rule by file owner that #44968 left out.

@robobun

robobun commented Oct 19, 2026

Copy link
Copy Markdown
Collaborator Author

Status: draft. It waits for a maintainer's scope decision (see the comment above).

How it was reproduced. Another user owns /tmp/x/package.json ("workspaces": ["*"]) and the package /tmp/x/evil. /tmp/x has mode 1777. In /tmp/x/proj, bun run --filter '*' build on main prints evil build: PLANTED-SCRIPT-RAN. With this PR it prints warn: ignoring package.json in /tmp/x: directory is writable by other users and runs only the script of proj.

PR: #46041

@robobun

robobun commented Oct 19, 2026

Copy link
Copy Markdown
Collaborator Author
Updated 5:05 AM PT - Oct 19th, 2026

@robobun, your commit 50a1c68 is building: #128829

@robobun

robobun commented Oct 19, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 10:28 PM PT - Oct 20th, 2026

✅ @robobun, your commit c7218faa4e6a6133da7231a1a88c2dea1d99c18a passed in Build #129728! 🎉


🧪   To try this PR locally:

bunx bun-pr 46041

That installs a local version of the PR into your bun-46041 executable, so you can run:

bun-46041 --bun

`bun run --filter` and `bun run --workspaces` looked for the workspace
root with their own upward loop. That loop took a package.json from a
sticky, group- or world-writable directory above the cwd (/tmp) as the
root, and ran the script in every package it lists. `bun install` passes
over such a file with a warning (#44968).

`get_candidate_package_patterns` now iterates `package_json_walk`, which
is `PackageJsonWalk` of `bun install` behind one exported function. Both
commands pass over the same directories and print the same warning.

`PackageJsonWalk::next` stats the directory from the path it already
holds, so the check no longer allocates.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants