Skip to content

fix(pregate): Rust crate lanes and nested JS package runners (#3344) - #3346

Closed
vilano93 wants to merge 2 commits into
garrytan:mainfrom
vilano93:fix/pregate-rust-nested-runner
Closed

vilano93 wants to merge 2 commits into
garrytan:mainfrom
vilano93:fix/pregate-rust-nested-runner

Conversation

@vilano93

@vilano93 vilano93 commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Why (in your own words)

/ship can't produce a receipt on a repo that mixes a Cargo workspace with a nested pnpm/vitest package. gstack-pregate's lanes check has no Rust support: every touched .rs file is PREGATE_NO_LANE, even when the file holds a #[cfg(test)] module that CI runs with cargo test. Separately, JS runner detection reads only the root package.json, so a vitest package in app/ runs under bun test from the repo root. That gives about 98 false failures, because vi.stubGlobal doesn't exist under bun.

This change does three things:

  • A touched Rust file selects the tests of its own crate and of every crate that depends on it. Each crate runs as cargo test -p <crate> from its workspace root.
  • A nested JS package that declares vitest or jest runs its tests from its own directory, with the package manager it declares.
  • Repo-root detection and an explicitly declared runner behave as before.

Fixes #3344.

Live evidence

The real repo where this broke: a Cargo workspace in engine/, a pnpm+vitest package in app/, and no root package.json. The branch touches 9 .rs files and 14 other files.

# installed 1.91.179.0 (a59b5e4)
$ gstack-pregate run --stage tests --only lanes --base origin/main
tests      lanes     fail     286 ms  11 touched file(s) with no test lane: VERSION app/src/main/index.ts engine/crates/engine-server/src/job.rs ... (PREGATE_NO_LANE)
    lanes: FAIL lanes — engine/crates/reference-runtime/src/preset.rs has no test lane
    ...
PREGATE: fail ... checks=1

# this branch, same command
tests      lanes     fail     233 ms  1 touched file(s) with no test lane: app/src/main/index.ts (PREGATE_NO_LANE)

The one file left really has no unit test in that repo; only Playwright covers it. VERSION is now covered through the crate's tests/cli_version.rs, which reads it.

The run below used --allow-repo-commands and a temporary .gstack/pregate.json that declared that one file as []. The file was deleted afterwards.

tests      lanes     requires-remote   47753 ms  1442 tests ran in 146 file(s), exit 0; 2 requires-remote obligation(s)
    lanes: ran: (cd 'engine' && cargo test -p engine-server) && (cd 'engine' && cargo test -p reference-runtime) && node --test 'scripts/doku-gate.test.mjs' 'scripts/version-gate.test.mjs' && (cd 'app' && pnpm exec v…
    lanes:         Tests  819 passed | 2 skipped (821)

The cargo part was 13 test result: lines, 501 passed, 0 failed. Before this change, the same TS files ran under bun test from the root, with about 98 false failures.

The new tests were red first, then green:

# test/pregate-rust.test.ts before the fix
(fail) Rust import graph > cfg(test) files and <crate>/tests/ files are tests; a loose tests/*.rs and a cfg(feature = "test") file are not
(fail) Rust import graph > a crate file selects its crate and every crate depending on it; a crate without tests has no lane
(fail) Rust runner and summary (pure) > cargo test: every test result line counts, doc-tests included; all zeros are a zero run
(fail) Rust runner and summary (pure) > without a declared runner, Rust tests group by crate from the workspace root; a declared runner takes every file
(fail) gstack-pregate on a Rust workspace > a touched src file runs its crate and the crates depending on it from the workspace root
(fail) gstack-pregate on a Rust workspace > zero tests is ZERO-RUN; a failing cargo run is PREGATE_LANE_FAILED
(fail) Cargo manifests are parsed as TOML > renamed, inherited, dotted, table-form and target-specific dependencies all select the dependent crates
(fail) Cargo manifests are parsed as TOML > an excluded crate runs from its own directory; a member runs from the workspace root
# after
 10 pass  0 fail

# test/pregate-selection.test.ts, nested package cases, before
(fail) nested JS packages ... > a nested vitest package plans its own group with the lockfile's package manager; root tests keep the root runner
(fail) nested JS packages ... > a touched file in a nested pnpm+vitest package runs vitest there
# after: 3 of 3 pass

Scope

  • Changed:
    • New lib/pregate/rust.ts: Cargo manifests are parsed with Bun.TOML.parse, which the repo already uses, so there is no new dependency and pregate never calls cargo. It also builds the crate model:
      • which files are tests: a file under <crate>/tests/, or one containing #[cfg(test)];
      • which crates depend on which: by path, package = renames, { workspace = true } inheritance, dotted keys, table form, dev, build and target-specific dependencies;
      • which workspace a crate belongs to: package.workspace, members and exclude, path-dependency members.
    • lib/pregate/import-graph.ts: the Rust links are added to the selection graph.
    • lib/pregate/checks/lanes.ts:
      • a cargo group per crate;
      • a test result: summary that adds up every line, doc-tests included; zero tests run is ZERO-RUN; cargo missing is incomplete, not passed;
      • nested JS packages run from their own directory, with the package manager from packageManager or the lockfile.
    • lib/pregate/init-draft.ts: init now says Rust tests run per crate.
    • docs/pregate.md.
    • Tests: new test/pregate-rust.test.ts, plus 3 nested-package cases in test/pregate-selection.test.ts.
  • Verified live by:
    • the before/after runs on the real repo above;
    • bun run typecheck: clean;
    • bun run typecheck:test: "819 known diagnostics, none new".
  • Compared with a clean upstream/main worktree on macOS:
    • The 18 pregate test files fail the same 18 tests on both. They are macOS-host failures, for example the fixture's ubuntu lane is treated as remote on a Mac. TMPDIR=$(realpath $TMPDIR) doesn't change them.
    • bun run test:quick fails the same 46 on both.
    • bun run test fails the same set on both. The only file that differed was exercised-map parity, and only because the new file wasn't tracked yet; it passes once tracked.
    • pregate-regate.test.ts times out now and then under load, on both sides.
  • Did NOT test:
    • yarn, bun x and npx nested packages against real installs (covered by stand-in scripts only);
    • feature-gated Rust tests (default features only);
    • Windows;
    • the package.workspace pointer and a literal member overriding exclude have no dedicated test; they are implemented following Cargo's rules.
  • Left alone, worth a follow-up:
    • A CI job that runs tests without a file list (here cargo test in the engine job) claims every selected test (lane-selection.ts). On this repo that turns all 146 files, Rust included, into obligations for the Linux app and lizenz-gate jobs.
    • Touching only a workspace-level Cargo.toml or Cargo.lock still needs a dependencies declaration.

Liveness proof (required for external contributors)

SCR-20261022-umol

Checklist

🤖 Generated with Claude Code

vilano93 and others added 2 commits October 22, 2026 23:42
lanes reported PREGATE_NO_LANE for every touched .rs file and ran a nested
pnpm+vitest package's tests as `bun test` from the repo root.

- Rust: a .rs file of a crate (nearest Cargo.toml with [package]) is a test
  under <crate>/tests/ or when it holds a #[cfg(test)]; every .rs file of a
  crate and its Cargo.toml select the crate's tests and those of every crate
  depending on it (lib/pregate/rust.ts). Selected tests run per crate as
  `cargo test -p <name>` from the workspace root, judged on the summed
  `test result:` lines (doc-tests included); zero tests stays ZERO-RUN and a
  missing cargo is incomplete, like a missing Dart SDK.
- JS/TS: a test owned by a nested package.json that declares vitest or jest
  runs from that package with package-relative paths, through the package
  manager its packageManager field or nearest lockfile names (pnpm exec,
  yarn, bun x, else npx). Root detection and a declared runner are unchanged.
- init notes that Rust tests run per crate; docs/pregate.md updated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The line scan of Cargo.toml missed dependents (a false green) and picked the
wrong workspace:
- an inherited renamed dependency (`base = { workspace = true }` with
  `[workspace.dependencies] base = { package = "alpha", path = ... }`);
- dotted keys (`alpha.path = "../a"`), `[dependencies.<name>]` tables and
  target-specific tables, matched by key name only;
- a crate the workspace excludes ran `cargo test -p` from the workspace root.

Manifests are now parsed with Bun.TOML. A dependency resolves by path
(relative to the manifest declaring it, or to the workspace root when
inherited), else by package name. A crate runs from the workspace it is a
member of under Cargo's rules (package.workspace, the nearest [workspace],
members globs, exclude, literal members, path dependencies of members),
else from its own directory. Nothing runs cargo.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@trunk-io

trunk-io Bot commented Oct 22, 2026

Copy link
Copy Markdown

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

@capy-ai

capy-ai Bot commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Thank you @vilano93. This is landing as #3380, with your two commits kept as they are and credited to you. The crate model, the TOML follow-up for renamed, inherited and dotted dependencies, and the nested pnpm/vitest runners all went in unchanged. On top of them we quoted and grammar-checked the crate name (it reached the lane's shell from Cargo.toml), switched members globs to a Cargo-style matcher so [ab] and ? work, counted plain #[test] functions as tests, and fixed the cargo stand-in so windows-free-shard (5) is green. It merges through the next merge train and closes #3344 when it lands.

@capy-ai capy-ai Bot closed this Oct 24, 2026
capy-ai Bot added a commit that referenced this pull request Oct 25, 2026
…ers (#3344, port of #3346) (#3380)

* fix(pregate): Rust crate lanes and nested JS package runners

lanes reported PREGATE_NO_LANE for every touched .rs file and ran a nested
pnpm+vitest package's tests as `bun test` from the repo root.

- Rust: a .rs file of a crate (nearest Cargo.toml with [package]) is a test
  under <crate>/tests/ or when it holds a #[cfg(test)]; every .rs file of a
  crate and its Cargo.toml select the crate's tests and those of every crate
  depending on it (lib/pregate/rust.ts). Selected tests run per crate as
  `cargo test -p <name>` from the workspace root, judged on the summed
  `test result:` lines (doc-tests included); zero tests stays ZERO-RUN and a
  missing cargo is incomplete, like a missing Dart SDK.
- JS/TS: a test owned by a nested package.json that declares vitest or jest
  runs from that package with package-relative paths, through the package
  manager its packageManager field or nearest lockfile names (pnpm exec,
  yarn, bun x, else npx). Root detection and a declared runner are unchanged.
- init notes that Rust tests run per crate; docs/pregate.md updated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(pregate): parse Cargo manifests as TOML; follow workspace membership

The line scan of Cargo.toml missed dependents (a false green) and picked the
wrong workspace:
- an inherited renamed dependency (`base = { workspace = true }` with
  `[workspace.dependencies] base = { package = "alpha", path = ... }`);
- dotted keys (`alpha.path = "../a"`), `[dependencies.<name>]` tables and
  target-specific tables, matched by key name only;
- a crate the workspace excludes ran `cargo test -p` from the workspace root.

Manifests are now parsed with Bun.TOML. A dependency resolves by path
(relative to the manifest declaring it, or to the workspace root when
inherited), else by package name. A crate runs from the workspace it is a
member of under Cargo's rules (package.workspace, the nearest [workspace],
members globs, exclude, literal members, path dependencies of members),
else from its own directory. Nothing runs cargo.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* test(pregate): the Rust fixture's cargo shim wins PATH on Windows and beside a real cargo

windows-free-shard (5) failed 'a touched src file runs its crate...' and
'zero tests is ZERO-RUN...' deterministically: the shim PATH was joined with
':' so Git Bash never saw the shim dir, and the runner's real cargo ran
against the fixture (exit 1, no test result lines). On POSIX hosts where
Bun and cargo share a directory (/usr/local/bin on Capy's image), the lane
puts Bun's directory first and the real cargo wins the same way.

Join with path.delimiter, add cargo.cmd for Bun.which on Windows, and on
POSIX put the shim first again in the lane's own bash through BASH_ENV.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(pregate): quote the crate name in cargoRunner

The crate name is read from the repo's Cargo.toml and was interpolated into
'cargo test -p <name>', which the lane runs through bash. A name like
'evil;touch pwned' ran the second command. Quote it like every file and cwd
the lane already passes; a new end-to-end case checks the name reaches cargo
as one argument and runs nothing.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* test(pregate): the nested-package pnpm shim wins PATH beside a real pnpm

'a touched file in a nested pnpm+vitest package runs vitest there' ran the
host's real pnpm wherever Bun and pnpm share a directory (/usr/local/bin on
Capy's image): the lane puts Bun's directory first, so pnpm installed vitest
from the network, dirtied the fixture tree and the check came back
incomplete. Put the shim first again in the lane's bash through BASH_ENV,
as the Rust fixture does.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* docs(changelog): pregate Rust crate lanes and nested package runners (#3344)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(pregate): a crate name Cargo would reject never reaches the lane's shell

Port review (outside voice): quoting alone was not enough, because the lane
expands {files} after the runner string is built, so a crate named
'evil{files}' spliced the quoted file list into the quoted name and a file
name like tests/$(cmd).rs then ran. A name outside Cargo's grammar
(letters, digits, _ and -) now gets no runner: its files are unclaimed and
the lane is incomplete, as Cargo itself would refuse the manifest. A valid
name, Unicode included, still runs quoted.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(pregate): match Cargo members globs per path component, without a backtracking regex

Port review (outside voice): members globs went through ship-policy's
matchesAny, which escapes [...] classes (crates/[ab] matched nothing, so the
crate was no member, lost its inherited [workspace.dependencies] path and its
dependents' tests were never selected: a false green) and backtracks
exponentially on a hostile glob such as '*a*a...*ab' (over 5 s for one crate,
before any runner deadline). A small dynamic-programming matcher handles *,
?, [..] with negation and ranges, and ** across components, in
O(pattern x path).

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(pregate): a plain #[test] in src is a Rust test; bound the cfg attribute scan

Port review (outside voice): only #[cfg(test)] modules and tests/ files
counted, so a dependent crate whose tests are plain #[test] or #[tokio::test]
functions in src never ran when its dependency changed (a false green). The
cfg scan also went quadratic on a run of unclosed '#[cfg(' (about 3 s on
120 KB). Test attributes now count, and the cfg predicate and its strings are
length-bounded so the scan stays linear.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* docs(pregate): Rust test attributes, workspace membership and rejected crate names; changelog for the port review fixes

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(pregate): match members globs by code point in one pass, and admit every crate past a size cap

Second port review round (outside voice): the matcher counted UTF-16 units, so
'crates/?' missed a crate directory named with a supplementary character and
dropped its inherited dependencies (a false green); and each unmatched '['
rescanned the rest of the glob (5 s for one crate with a million of them).
Tokenize by code point with a precomputed next-']' table, and past 4096
characters of members globs admit every crate under the workspace: a wrong
member fails loudly in cargo, a dropped one is silent. The new supervised
probe kills a super-linear matcher at 30 s instead of hanging the file.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(pregate): scan cfg attributes once without truncating them; an unclosable one counts as a test

Second port review round (outside voice): the length bound from the first
round dropped a valid cfg(any(test, feature = "<257 chars>")), so its crate's
tests were no longer selected (a false green). Read each attribute once to
its closing ']' and resume after it, which keeps the scan linear without a
bound; an attribute the scan cannot close (an unterminated string, no ']')
counts as a test, which selects more, never fewer.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* docs(changelog): second port review round (code-point member globs, unbounded linear cfg scan)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* fix(pregate): past the members size cap, a crate's run root is undecided and its tests unclaimed

Third port review round (outside voice): admitting every crate past the cap
made a non-member run as 'cargo test -p <name>' from the workspace root,
where a same-named member answered and its passing tests stood in for the
orphan's failing one (a false green). Keep the conservative membership for
dependency edges, but leave such a crate's workspace undecided (null):
planRuns leaves its files unclaimed and the lane incomplete.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* test(pregate): the Rust end-to-end runs put the cargo shim under Windows' own Path key

windows-free-shard (5) still failed both end-to-end cases at fe99b20 with
the same symptom (the runner's real cargo, exit 1, no test result lines).
runBin merges process.env, whose Windows key is 'Path', with the shim's
'PATH', so the child got two path variables and read the original. Write the
shim under the key process.env actually uses, as test/helpers/unreachable-gbrain.ts
and sync-gbrain-readiness-fixture.ts do.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* test(pregate): run the cargo stand-in cases on POSIX only

windows-free-shard (5) still failed both end-to-end cargo-lane cases after the
delimiter, cargo.cmd and Path-key repairs: the lane's Git Bash exits 1 with an
empty log, and that log is not visible from CI. Skip the two cases that run the
bash stand-in on win32 (test.skipIf, as other POSIX-shim pregate fixtures are
handled) and drop the unproven cargo.cmd and Path-key changes. Selection,
planning and the hostile-name case still run on Windows.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* v1.91.197.0 stamp

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

---------

Co-authored-by: vilano93 <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pregate: touched Rust files never get a test lane, and a nested pnpm/vitest package runs under bun test (mixed Cargo + JS repo cannot ship)

1 participant