Skip to content

Test Cargo hosted and vendored modes under rules_rust on real Bazel - #2147

Merged
Mikola Lysenko (mikolalysenko) merged 1 commit into
mainfrom
bazel-small/51-cargo-rules-rust
Nov 19, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 1 commit into
mainfrom
bazel-small/51-cargo-rules-rust

Conversation

@mikolalysenko

@mikolalysenko Mikola Lysenko (mikolalysenko) commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

No user-visible change.

What this is

Tests only. The real-Bazel suite (e2e_bazel_build) held flows for Go, npm, PyPI and RubyGems. This adds Cargo: hosted and vendored mode for a crate that rules_rust's crate_universe resolves from a Cargo workspace (crate.from_cargo under Bzlmod), with vex after the patched build. crate_universe runs cargo over the workspace's Cargo.toml and Cargo.lock, so what those two files name is what the next bazel build compiles. No file under crates/*/src changes.

One file that the other ecosystems' Bazel tests share changes: twelve lines of harness.rs keep the caller's cargo settings away from every Bazel process a test starts (see "The shared harness edit"). Because of that edit, this pull request starts the Go, npm, PyPI and RubyGems Bazel workflows as well as the Cargo one.

Contents

Lines
Implementation (crates/*/src) 0
Tests 1,235
Fixture 71
  • crates/socket-patch-cli/tests/e2e_bazel_build/cargo.rs (1,081 lines, 665 of them code): the crate builder, the loopback stand-in, the two flows, and one test that needs no Bazel. One mod line in main.rs.
  • crates/socket-patch-cli/tests/e2e_bazel_build/harness.rs (12 lines added): two more entries in the lists of variables no Bazel process of a test sees, and in the list of the test that guards them. The reusable workflow and the other ecosystems' files are untouched.
  • .github/workflows/bazel-cargo.yml (141 lines): the caller of bazel-compatibility.yml for the cargo:: tests.
  • crates/socket-patch-cli/tests/fixtures/bazel/rules_rust/bzlmod/{common,e9,e7} (six files, 71 lines, no binary): a rust_binary that prints one constant of the crate, Cargo.toml, a Cargo.lock with a placeholder for the crate's checksum, and one MODULE.bazel per ruleset release. The hub is the one of the rules_rust README with cargo_config = "//:.cargo/config.toml" added, and no cargo-bazel lock (lockfile).

What it proves, and where

Two tests, each one flow in a fresh output base:

cargo::hosted_from_cargo_with_config

  1. bazel build and bazel run //:consumer print OUT: PRISTINE <id>. The stand-in registry was asked for the crate's index file, and at least twice for the crate file (twice in every run here). The test counts requests by path. It does not tell which program sent them.
  2. socket-patch scan --mode hosted --yes changes exactly .cargo/config.toml (adds [registries.socket-patch-<uuid>], keeps what the project had), Cargo.toml (registry = "socket-patch-<uuid>" on the dependency) and Cargo.lock (the patch registry as the entry's source, the checksum of the patched crate file).
  3. The same two Bazel commands, with no step in between and no repin, print OUT: PATCHED <uuid>. The stand-in patch host was asked for its index file of the crate, and at least twice for the crate file (twice in every run here); the upstream crate file was not asked for again.
  4. socket-patch vex exits 0, and its document names the crate and the patch (Patched via Socket patch <uuid>); no file of the checkout changes. A second scan --mode hosted --yes, as a pipeline runs it every time, applies the patch again and rewrites no file.
  5. socket-patch rollback --yes puts the three files back byte for byte, and the build prints the upstream marker again.
  6. On a second machine, against a second patch, a patch host that serves another crate file than the lock pins fails the build, in both forms: its index names the pinned checksum (cargo: failed to verify the checksum of ...), or its index names the checksum of what it serves (cargo: checksum for ... changed between lock files). That patch host was asked for the file (once in every run here), and the upstream crate file was not asked for in its place.

cargo::vendored_from_cargo_with_config

  1. As above.
  2. socket-patch scan --mode vendored --yes changes exactly: the unpacked crate under .socket/vendor/cargo/<uuid>/<name>-<version>/ (Cargo.toml, src/lib.rs), socket-patch.vendor.json beside it, .socket/vendor/state.json, Cargo.toml (a [patch.crates-io] entry with the path of the copy) and Cargo.lock (the entry gets the copy's version, <version>+socket.<uuid>, and loses its source and checksum).
  3. The same two Bazel commands, with no step in between and no repin, print the patched marker, and the build sends no request to the stand-in.
  4. socket-patch vex, right after that build, changes no file. With rules_rust 0.56.0 it exits 0 and its document names the crate and the patch. With rules_rust 0.75.0 it exits 1 and writes no document (a known gap, below).
  5. socket-patch vendor --revert removes the copy and puts the two files back byte for byte, and the build prints the upstream marker again.
  6. On a second machine, against a second patch, the first build is the patched one, from the committed copy. scan --mode vendored --yes then runs a second time, as a pipeline runs it every time (what it does depends on the ruleset, below). After it, with either ruleset, the next build is still the patched one and writes no file, vendor --check passes, and vex exits 0 and names the crate and the patch. Then the library of the committed copy is changed. Nothing in a build holds that copy to a checksum, as for any crate a Cargo workspace takes from a path. The test holds what is true: the changed library is what Bazel builds next, and socket-patch vendor --check reports it (vendor_hash_mismatch).

Both markers are read from what Bazel built, and each holds an id made for the run.

Releases and seconds per test at this commit (macOS arm64, one fresh output root per release, a repository cache that earlier runs had filled, two tests at a time as the workflow runs them, on a machine that ran other builds). A leg is one job of the workflow: one Bazel release on one platform.

Bazel rules_rust hosted vendored leg disk at its peak
9.3.2 0.75.0 (Rust 1.99.1) pass, 70 s pass, 91 s 93 s 3,619 MB
8.8.2 0.75.0 pass, 69 s pass, 92 s 94 s 3,719 MB
7.7.2 0.56.0 (Rust 1.83.0) pass, 61 s pass, 73 s 75 s 3,485 MB

The run that filled the cache for rules_rust 0.56.0 took 164 s and 167 s on Bazel 7.7.2.

The other ecosystems' flows, once each on Bazel 9.3.2 at this commit, because harness.rs changed: go::hosted_go_deps pass (56 s), npm::hosted_npm_translate_lock pass (20 s), npm::vendored_npm_translate_lock pass (20 s), pypi::hosted_pip_parse pass (20 s), ruby::hosted_bundle_fetch pass (104 s).

rules_rust 0.75.0 also builds the fixture on Bazel 7.7.2: both flows passed there in an earlier form, before the vex and second-scan steps were added, and that was not run again. Bazel 7 keeps 0.56.0 because that release behaves differently in ways the flows record below.

On Linux, on GitHub's runners, the three Cargo legs of this pull request pass at its head: Bazel 9.3.2 in 343 seconds, 8.8.2 in 317 and 7.7.2 in 293, with a cold cache (the two flows together take 213 seconds on 9.3.2). The Go, npm, PyPI and RubyGems legs, which the harness edit starts, pass too, with the other checks (69 in all). The macOS leg on a GitHub runner first runs in the nightly.

Runs that each changed one thing and checked that the tests then fail. With the review's changes in place (the test logic as in this commit; only comments, one variable name and the base commit changed after), all as expected:

  • with the CARGO_ entry taken out of the harness list and CARGO_NET_OFFLINE=true exported, both flows fail at the first build on Bazel 9.3.2 (cargo: no matching package named, with its note about offline mode);
  • three requests for a crate file expected at the first build: both flows fail on 9.3.2 (the stand-in had been asked once for the index file and twice for the crate file);
  • one more request for the upstream crate file expected after the patched build: the hosted flow fails on 9.3.2;
  • the request for the upstream crate file after the undo expected the other way round: the hosted flow fails on 9.3.2 (2 requests before the undo, 3 by the end of its build);
  • without Bazel, four changes to the harness lists: the CARGO_ entry or the REPIN entry removed (the guarding test fails with a child sees CARGO_SETTING, a child sees REPIN), or either name removed from the test's own list (CARGO_* is removed and not tested, REPIN is removed and not tested).

Eleven more were run on Bazel 9.3.2 before the review changes and before the last two rebases, and were not run again; the assertions they target did not change. All eleven failed as expected: the first scan as a dry run; the files the scan wrote put back before the build; the upstream bytes granted as the patch; the tampering left out; the undo as a dry run; the second machine's index left at the pinned checksum; the mirror taken out of the cargo config (cargo: no matching package named, which is what a build that gets past the stand-in meets); vex expected the other way round in each flow; the second vendored scan run before any build of its machine; the second vendored scan as a dry run; the hosted flow's second scan run in vendored mode.

Why both hubs name a cargo config

The crate under test never comes from crates.io. cargo reads no Bazel downloader config, so the test's workspace says where its crates come from the way a project behind a crates.io mirror does: .cargo/config.toml replaces the crates-io source with the loopback stand-in. cargo-bazel leaves the cargo config of a checkout out of the workspace it resolves unless the hub names it, so a hub without cargo_config can only resolve against crates.io itself. Hosted mode needs the attribute anyway (the patch registry is defined in that file); the vendored flow has it only for the stand-in.

The crate's name has 69 characters. crates.io takes no name of more than 64 (the Cargo Book, the name field), so nobody can publish it there. The upstream crate file and each patched one are built for the run, and the test writes the upstream checksum into the Cargo.lock of its workspace copy, so no repository cache can supply them.

The shared harness edit

Every Bazel process a test starts, in every ecosystem's flows, now runs without any variable whose name starts with CARGO_ and without REPIN. Before, the cargo that rules_rust starts read the caller's CARGO_NET_OFFLINE, CARGO_SOURCE_*, CARGO_REGISTRIES_* and CARGO_HTTP_*, and rules_rust read CARGO_BAZEL_REPIN or REPIN as its switch for a repin; the Cargo flows stopped with a failure when one of those two was set.

  • What cargo test itself puts under that prefix into the test process was read from a run: CARGO_MANIFEST_DIR, CARGO_MANIFEST_PATH and fourteen CARGO_PKG_* names (and CARGO_HOME, where the shell or rustup sets it). The tests read those at compile time, and no Bazel process needs them. The bare name CARGO is not under the prefix and stays; rules_rust 0.75.0 sets it itself for its tool.
  • The Cargo flows set two names on purpose: CARGO_HOME (a directory of the test, because rules_rust 0.56.0 runs its cargo with the cargo home of the environment) and CARGO_BAZEL_ISOLATED. Both are set after the removal, as before. Checked two ways: a temporary test, not committed, printed what a Bazel command of a Cargo flow gets, and it showed both names with the flow's values, as variables and as --repo_env options, and every other CARGO_* name of the process and REPIN removed; and on Bazel 7.7.2 the last step of the hosted flow passes only when the hub's cargo uses the cargo home the test names.
  • With CARGO_NET_OFFLINE=true, CARGO_BAZEL_REPIN=1 and REPIN=1 exported, both Cargo flows pass on Bazel 9.3.2 (67 s and 88 s). That run was made with the harness edit as it is here, before the last changes to the wording of three failure messages and before the last rebase.
  • The test that guards the two lists has one more name for each, and fails when either entry or either name is removed.

What starts on main

bazel-cargo.yml calls bazel-compatibility.yml with three Linux legs (9.3.2 and 8.8.2 on the rules_rust 0.75.0 fixture, 7.7.2 on the 0.56.0 one) and one macOS leg (9.3.2).

  • On a pull request that touches: the caller or the reusable workflow, the Bazel installer or its pins, cargo.rs or harness.rs, the rules_rust fixture, any file of socket-patch-core with cargo in its name or in the name of one of its directories (among them vex/discover/cargo.rs, which vex reads a Cargo project with), or patch/user_patches.rs (it reads the Cargo.toml and the cargo config of every scan).
  • On a push to main that touches any of those, or: main.rs of the test target, patch/redirect/**, vendor/**, hosted/**, the CLI's commands/**, the shared test helpers, or the toolchain files (Cargo.lock, Cargo.toml, crates/*/Cargo.toml, rust-toolchain.toml, .cargo/**). The rest of the VEX code is in neither list, as in the npm caller; a change there gets its run in the nightly.
  • Nightly at 04:57 UTC, in a concurrency group of its own, and on dispatch. The macOS leg runs nightly and on dispatch only.
  • Not required for merge: no merge_group trigger, and no job is a needs of ci-ok.

Sizes below are as du prints them (1 MB = 1,048,576 bytes).

Outside hosts of a leg, beyond what every Bazel leg needs (GitHub for the Bazel release and the ruleset archives, bcr.bazel.build): static.rust-lang.org (the Rust toolchain: six archives, 116 MB with Rust 1.99.1 and 128 MB with Rust 1.83.0 for macOS arm64 as downloaded, and 167 MB for Linux x86_64 with Rust 1.99.1 by the sizes that host publishes), GitHub again for the cargo-bazel binary (15 MB) and for more ruleset archives than other legs fetch (the rules_rust 0.75.0 archive alone is 63 MB), and static.crates.io for the crates rules_rust builds its own tools from (60 with 0.75.0, one with 0.56.0). By the build event files of a cold build, Bazel's downloader fetched 15 files from github.com, 6 from static.rust-lang.org and 60 from static.crates.io on Bazel 9.3.2, and 14, 6 and 1 on 7.7.2, beside what it reads from the Bazel registry. No leg asks crates.io about the crate under test: Bazel's own fetches of it went to the loopback stand-in.

Cache: the repository cache of the reusable workflow, one per ruleset release, came to 228 MB (9.3.2) and 232 MB (8.8.2) with rules_rust 0.75.0, each from empty, to 241 MB when 8.8.2 filled the cache 9.3.2 had left, and to 155 MB with 0.56.0, on macOS arm64.

Disk: every workspace copy unpacks the Rust toolchain for itself (1.7 to 1.8 GB). A test now removes its first copy, with its Bazel server stopped, before its second machine builds, so a leg holds two copies at its peak, one per test, where it held four. Measured with du over the tests' scratch directories every four seconds during a run on Bazel 9.3.2: 7,062 MB at the largest reading before the change (four copies: 1,807, 1,791, 1,732 and 1,730 MB), 3,619 MB after (two copies: 1,818 and 1,801 MB). After the change the largest reading was 3,719 MB on 8.8.2 and 3,485 MB on 7.7.2. Not measured on Linux. A run that names no repository cache (SOCKET_PATCH_BAZEL_E2E_REPO_CACHE unset, which is how a developer starts a test by the module docs) keeps the first copy to the end, because the cache both machines read is inside that copy's scratch directory; that path ran once, the hosted flow on Bazel 7.7.2, and passed.

Known behaviour the flows record

With rules_rust 0.75.0 (Bazel 9.3.2 and 8.8.2):

  • Known gap: vendor --check fails right after a patched build. cargo-bazel writes a BUILD.bazel into the committed copy under .socket/vendor/cargo/, which the scan did not record. socket-patch vendor --check then exits 1 (vendor_check_failed: vendor_inventory_mismatch). The test asserts this because it is what the release does; it is not what a user needs.

  • Known gap: vex attests nothing in vendored mode right after a patched build. For the same reason socket-patch vex leaves the patch out under vendor_inventory_mismatch, exits 1 (no_applicable_patches) and writes no document.

    Both assertions are marked KNOWN GAP in cargo.rs and listed under "Known gaps" in its module docs. When the product is fixed, the two marked branches are deleted and the vendored flow expects vendor --check to pass and vex to attest the crate with every ruleset, as it does with rules_rust 0.56.0 today.

    The flow also holds the way out a user has today: a second scan --mode vendored downloads the crate again from the patch host, reports the copy rebuilt and removes the BUILD.bazel. The next build is still the patched one and does not write the file again, because nothing the hub reads has changed; vendor --check passes and vex attests the crate. The file comes back when Bazel evaluates the hub again (seen once, on Bazel 9.3.2, after an edit of Cargo.lock; a restart of the Bazel server did not bring it back; neither is asserted). vendor --revert removes the file with the copy.

  • A frozen Bazel lock fails after the command. Bazel records the hub's input files in MODULE.bazel.lock. Right after scan in either mode, and again right after the undo, bazel build --lockfile_mode=error fails with MODULE.bazel.lock is no longer up-to-date, until a build that may update the lock (or bazel mod deps --lockfile_mode=update) has run and the user has committed the lock. Any edit of Cargo.lock does this.

  • The first patched build rewrites MODULE.bazel.lock. In hosted mode the lock then names the crate file on the patch host, token segment included, as Cargo.lock and .cargo/config.toml already name the index.

  • The undo leaves Bazel's lock behind. socket-patch's files are back at once; MODULE.bazel.lock is the one of the patched build until the next build writes the one of before.

  • The hub's cargo starts from an empty cargo home whenever the hub is evaluated, so each evaluation reads the registry index and downloads the crate file again: the stand-in was asked for the upstream crate file once more by the build after rollback. In hosted mode the patch host has to be reachable at each of those evaluations.

With rules_rust 0.56.0 (Bazel 7.7.2): MODULE.bazel.lock has no entry for the hub and a build writes no file. The undo restores the checkout at once, a frozen lock keeps passing, vendor --check passes after a build, vex attests the crate in vendored mode right after the build, and a second vendored scan reports the copy already_vendored and leaves it alone. The hub's cargo runs with the cargo home of its environment and asks a registry only for what that home does not hold: the build after rollback sent no request for the crate file. The tests give every Bazel process a CARGO_HOME of their own for that reason.

On both:

  • In hosted mode vex attests the crate after the patched build, and a second hosted scan applies the patch again without rewriting a file.
  • No repin is needed on a hub without a cargo-bazel lock. Every build of the flows runs without a repin variable, whatever the caller's shell has set.
  • In vendored mode nothing verifies the committed copy at build time, and a library changed after Bazel has built the copy is picked up by the next build. rules_rust also warns Build is not hermetic - path dependency pulling in crate at ... for the copy (its source prints that for every path dependency; not asserted).
  • A crate file cargo refuses fails the whole hub (error evaluating module extension ... crate), not one target. The flow holds cargo's two messages; the Bazel line is seen in the build output, not asserted.
  • rollback prints its usual reinstall_required warning (not asserted); under Bazel the next build is that install.

Not claimed

  • macOS on a GitHub runner, until the nightly; Windows. The disk peak of a leg on Linux was not measured; the legs pass.
  • Which program sent which request to the stand-in. By the ruleset's design the hub's cargo downloads a crate file and Bazel's downloader then fetches it for the crate's repository; the tests count requests per path and do not tell the two apart.
  • A hub without cargo_config, which is the README's hub as written: neither mode is tested there.
  • A hub with a cargo-bazel lock (lockfile), crate.spec hubs, crates_vendor, crates_repository under WORKSPACE, rules_rs.
  • A crate that is only a dependency of a dependency, a requirement inherited from [workspace.dependencies], two versions of one crate, a crate with a build script.
  • Agent mode.
  • The VEX document beyond the two things the flows read in it (the crate's purl and the patch's id), and vex in a checkout with more than one patched crate.
  • socket-patch repair under Bazel: no flow runs it.
  • Bazel's own downloader refusing a crate file: on a fresh machine the hub's cargo refuses it first.
  • rules_rust releases other than 0.75.0 and 0.56.0, Bazel releases other than the three, and rules_rust 0.75.0 on Bazel 7.7.2 with the steps as they are now.
  • The real crates.io and the real patch host: every registry of the crate under test is a loopback stand-in.
  • A read-only source tree or remote execution, where cargo-bazel 0.75.0 could not write its BUILD.bazel into the copy.
  • The other ecosystems' flows on Bazel 8.8.2 and 7.7.2 with the harness edit: they ran on 9.3.2 only.

Code review

A review of this pull request found eight things. Five are fixed here and three are left open.

  • The vendored flow asserted two rough edges as if they were the contract (vendor --check exits 1 and vex writes no document right after a patched build with rules_rust 0.75.0). The assertions stay, because they are what the release does. Each is now marked KNOWN GAP in a comment that says what a fix of the product changes in the test, the module docs name both under one heading, "Known gaps", and they come first under "Known behaviour the flows record" above.
  • The caller's cargo settings reached the hub's cargo. Only CARGO_HOME and one more variable were set, so CARGO_NET_OFFLINE, CARGO_SOURCE_*, CARGO_REGISTRIES_* and CARGO_HTTP_* of the shell went through. Fixed in the harness: every CARGO_* name is removed from every Bazel process of a test ("The shared harness edit").
  • The flows failed at once when CARGO_BAZEL_REPIN or REPIN was set, instead of keeping those names away from Bazel. Fixed by the same edit, which also removes REPIN; the check that stopped the test is deleted.
  • Each flow kept its first workspace copy alive while its second machine built, so a leg held four unpacked toolchains, about 7 GB, which can fill the disk of a hosted runner. Fixed: the first copy is removed before the second machine builds. Measured peak on Bazel 9.3.2: 7,062 MB before, 3,619 MB after.
  • A helper said that cargo and then Bazel's downloader fetched the crate file, but only counted requests. Reworded to what is asserted: the helper is now asked_for, and its doc, its failure message, the evidence lines and the steps above say that the stand-in was asked for the crate file, with the count. Telling the two programs apart needs the stand-in to record more than the path of a request, which is a harness change, so it is not asserted.
  • Left open: the Cargo module writes out steps the harness also has (its own form of the undo step and of the frozen-lock build). Not changed here.
  • Left open: small helpers are repeated between the ecosystems' modules (reading a text file of a tree, a sha256 in hex, a lock entry, the second machine, the refused build). Not changed here.
  • Left open: no test without Bazel checks that the literal trigger paths of bazel-cargo.yml name files that exist; the PyPI module has such a test for its caller. Not changed here.

The three open items belong to one change of the harness for all five modules, not to the pull request that adds one of them.

Decisions for the owner

  1. The hub. Both flows run on the README hub plus cargo_config, because that is the only hub a stand-in registry can reach. The README hub as written stays without a test unless a flow may fetch a real crate from crates.io. Is that acceptable?
  2. Two ruleset releases. Bazel 7.7.2 builds with rules_rust 0.56.0 so that the older behaviour (no lock entry, no BUILD.bazel in the copy, cargo home of the environment) stays covered. One release for all three would drop a 155 MB cache and that coverage.
  3. Cost. Each Cargo leg downloads about 230 MB when its cache is cold and holds two unpacked Rust toolchains at its peak (3.4 to 3.7 GB on macOS arm64; the archives are larger on Linux, not measured). The legs ran in about a minute and a half here with a filled cache.
  4. The two known gaps have one cause and are pinned as today's behaviour. With rules_rust 0.75.0, after a build, vendor --check fails and vex attests nothing in vendored mode, and the next vendored scan downloads the crate again, all because cargo-bazel writes a BUILD.bazel into the vendored copy. The stale MODULE.bazel.lock after a scan is a separate rough edge with a cause of its own. Fix the gaps before more Cargo flows land, or after?

Generated by Claude Code

The real-Bazel suite held flows for Go, npm, PyPI and RubyGems. This
adds the two modes that reach a crate Bazel builds: rules_rust's
crate_universe runs cargo over the workspace's Cargo.toml and Cargo.lock
(crate.from_cargo under Bzlmod), so what those two files name is what
the next `bazel build` compiles. No file under crates/*/src changes.

Two tests, `cargo::hosted_from_cargo_with_config` and
`cargo::vendored_from_cargo_with_config`, on Bazel 9.3.2 and 8.8.2 with
rules_rust 0.75.0 (Rust 1.99.1) and on Bazel 7.7.2 with rules_rust
0.56.0 (Rust 1.83.0). The hub is the one of the rules_rust README with
`cargo_config = "//:.cargo/config.toml"` added, and no cargo-bazel lock.
Each test, in a fresh output base:

- builds the fixture workspace and reads "OUT: PRISTINE <id>" from
  `bazel run //:consumer`; the stand-in registry was asked for the index
  file of the crate and at least twice for the crate file. The test
  counts requests. The stand-in records no sender, so the test does not
  tell the hub's cargo from Bazel's downloader;
- runs the one command of its mode and holds the changed files to its
  list. `scan --mode hosted --yes` adds the patch registry to
  .cargo/config.toml, names it on the dependency in Cargo.toml and
  gives the crate's entry of Cargo.lock that registry as its source and
  the checksum of the patched crate file. `scan --mode vendored --yes`
  commits the unpacked crate under .socket/vendor/cargo/<uuid>/ with
  its ledger, adds a [patch.crates-io] entry to Cargo.toml and gives
  the lock's entry the copy's version and no source;
- runs the same two Bazel commands, with no step in between and no
  repin, and reads the patched marker, which holds an id made for the
  run. Hosted: the stand-in patch host was asked for its index file of
  the crate and at least twice for the crate file, and the upstream
  crate file was not asked for again. Vendored: the build asked the
  stand-in for nothing;
- runs `vex` in the checkout Bazel has built in. Hosted: it exits 0 and
  its document names the crate as patched by the patch, and a second
  `scan --mode hosted` applies the patch again and changes no file.
  Vendored: per ruleset, below;
- runs the undo (`rollback --yes`, or `vendor --revert`): every file
  the command wrote is the one of before, byte for byte, and the build
  prints the upstream marker again;
- on a second machine, against a second patch: a patch host that
  serves another crate file than the lock pins fails the hosted build
  on the checksum, both when its index names the pinned checksum (cargo
  refuses the download) and when it names the one of the file it serves
  (cargo refuses the lock's entry), and the upstream crate is not asked
  for in its place. In vendored mode the first build there is the
  patched one; the scan runs a second time, as on every run of a
  pipeline, and after it the next build writes nothing, `vendor --check`
  passes and `vex` attests the crate. Then the library of the committed
  copy is changed. Nothing in a build holds that copy to a checksum, as
  for any crate a workspace takes from a path: the test holds that the
  changed library is what Bazel builds next, and that `vendor --check`
  reports it (vendor_hash_mismatch).

Known gaps. Two assertions of the vendored flow hold what the release
does with rules_rust 0.75.0, which is not what a user needs. They are
marked KNOWN GAP in cargo.rs and listed under "Known gaps" in its module
docs. cargo-bazel 0.75.0 writes a BUILD.bazel into the committed copy,
which the scan did not record, so right after a build that is the
patched one:

- `vendor --check` exits 1 (vendor_inventory_mismatch);
- `vex` leaves the patch out under the same code, exits 1
  (no_applicable_patches) and writes no document.

The flow also holds the way out a user has today: a second
`scan --mode vendored` downloads the crate again, reports the copy
rebuilt and removes the file. The build after that scan does not write
the file again, since nothing the hub reads has changed, and the check
and `vex` pass. `vendor --revert` removes the file with the copy. A fix
of the product makes both commands pass right after the build, and the
two marked branches of the test go.

What else a build writes into the checkout depends on the ruleset, and
each release is held to its case:

- rules_rust 0.75.0 has Bazel watch the files the hub reads, and
  MODULE.bazel.lock has an entry for the hub. Right after either
  command, and again right after the undo, a `--lockfile_mode=error`
  build fails ("MODULE.bazel.lock is no longer up-to-date") until a
  build that may update the lock has run. The first patched build
  rewrites that lock, which in hosted mode then names the crate file on
  the patch host; the undo leaves it behind, and the next build writes
  the lock of before.
- With rules_rust 0.56.0 the lock has no entry for the hub and a build
  writes no file: the undo restores the checkout at once,
  `vendor --check` passes, `vex` attests the crate, and a second scan
  reports the copy already_vendored.
- rules_rust 0.75.0 gives the hub's cargo a home inside the output
  base that is empty at every evaluation of the hub, so each one reads
  the index and downloads the crate file again: the stand-in was asked
  for the upstream crate file once more by the build after `rollback`.
  rules_rust 0.56.0 runs cargo with the home it is given, and that
  build sent no request.

rules_rust 0.75.0 builds the fixture on Bazel 7.7.2 as well. Bazel 7
keeps the older release so that both behaviours stay covered.

The crate under test never comes from crates.io. No downloader config
reaches cargo, so the workspace names a mirror of crates.io in its
.cargo/config.toml (a source replacement), which is the loopback
stand-in, and the hub names that file: cargo-bazel leaves the cargo
config of a checkout out unless the hub does. That is why the hub of
the vendored flow names a config too. The crate's name has 69
characters and crates.io takes none of more than 64, so a build that
gets past the stand-in finds nothing there: with the mirror taken out
of the config, the first build fails with "no matching package named".
The upstream crate file and each patched one are built for the run,
and the test writes the upstream checksum into the Cargo.lock of its
workspace copy, so no repository cache can supply them.

One edit of the shared harness, for the Bazel children of every
ecosystem's flows: every variable whose name starts with CARGO_, and
REPIN, is removed from them. The cargo a ruleset starts reads the
caller's CARGO_NET_OFFLINE, CARGO_SOURCE_*, CARGO_REGISTRIES_* and
CARGO_HTTP_*, and rules_rust reads CARGO_BAZEL_REPIN or REPIN as its
switch for a repin; `cargo test` itself sets CARGO_MANIFEST_DIR,
CARGO_MANIFEST_PATH and CARGO_PKG_* for the test process. The hermetic
test that guards those lists gains one name for each. The Cargo flows
set CARGO_HOME (a directory of the test: under Bzlmod rules_rust 0.56.0
runs its cargo with the cargo home of the environment) and
CARGO_BAZEL_ISOLATED after that scrub, through the cell's repo_env.
With CARGO_NET_OFFLINE=true, CARGO_BAZEL_REPIN=1 and REPIN=1 exported
both flows pass on 9.3.2; with the prefix taken out of the list again,
both fail under CARGO_NET_OFFLINE=true at the first build (cargo: "no
matching package named").

A test removes its first workspace copy (the Bazel server stopped, the
scratch root deleted) before its second machine builds, when the run
names a repository cache, as CI does. Without one the cache both
machines read is inside the first copy's scratch root, and that copy
stays until the test ends.

- crates/socket-patch-cli/tests/e2e_bazel_build/cargo.rs: the crate
  builder, the stand-in (the Socket API, the sparse index and the
  crate file of the patch registry, and the two hosts of crates.io),
  the two flows, and a hermetic test that holds the fixture and the
  workflow's legs to the table in the test code.
- crates/socket-patch-cli/tests/e2e_bazel_build/harness.rs: the two
  names above in the lists of what no Bazel child sees, and in the
  list of the test that guards them.
- crates/socket-patch-cli/tests/fixtures/bazel/rules_rust/bzlmod/
  {common,e9,e7}: the workspace, with a placeholder for the crate's
  checksum in Cargo.lock and no cargo config (the test writes it, with
  the port of its stand-in).
- .github/workflows/bazel-cargo.yml: the caller of
  bazel-compatibility.yml for `cargo::`. Three Linux legs on a pull
  request that touches these files or a Cargo file of
  socket-patch-core, on a main push that also touches the shared engine
  code and test helpers, nightly at 04:57 UTC and on dispatch; one
  macOS leg nightly and on dispatch. Not required for merge.

Measured on macOS arm64 on this tree, each release in a fresh output
root, two tests at a time, on a machine that ran other builds, with a
repository cache that earlier runs had filled: 70 s and 91 s (hosted,
vendored) on 9.3.2, 69 s and 92 s on 8.8.2, 61 s and 73 s on 7.7.2.
The run that filled the cache of rules_rust 0.56.0 took 164 s and
167 s. A cold leg downloads about 230 MB with rules_rust 0.75.0 and
155 MB with 0.56.0: the Rust toolchain from static.rust-lang.org (about
120 MB of the 230), the cargo-bazel binary and the ruleset archives
from GitHub, and the crates rules_rust builds its own tools from, from
static.crates.io (60 with 0.75.0, one with 0.56.0). Every workspace
copy unpacks the toolchain for itself, 1.7 to 1.8 GB. With the first
copy removed before the second machine builds, a leg holds two copies
at its peak, one per test: 3,619 MB on 9.3.2 (3,719 MB on 8.8.2,
3,485 MB on 7.7.2), where the four copies of before came to 7,062 MB
on 9.3.2.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@cursor

cursor Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
Tests and CI only; no production src changes. Harness env scrubbing is shared across Bazel e2e ecosystems.

Overview
This PR extends the real-Bazel e2e suite with Cargo (rules_rust crate.from_cargo under Bzlmod): new cargo.rs exercises hosted and vendored socket-patch scan end-to-end—unpatched build, patch, patched build, vex, undo, and a second-machine tamper scenario—using a loopback crates.io mirror and a fixture crate name that cannot exist on crates.io.

It adds bazel-cargo.yml (path-filtered PR/push/nightly legs on Bazel 7.7.2–9.3.2 with rules_rust 0.56.0 / 0.75.0 fixtures) and a small rules_rust/bzlmod fixture. harness.rs now strips CARGO_* and REPIN from every Bazel child so hub cargo is isolated (a shared harness tweak that also re-triggers other ecosystem Bazel workflows when harness.rs changes). Tests document KNOWN GAP behaviour on rules_rust 0.75.0 where BUILD.bazel in the vendored copy breaks immediate vendor --check / vex after build.

Reviewed by Cursor Bugbot for commit 04ce7d6. Configure here.

@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: review brief for the approver

What it does. Adds two real-Bazel Cargo flows under rules_rust, one for hosted mode and one for vendored mode. Each reads the result from what Bazel builds. Two branches marked KNOWN GAP pin today's behaviour of cargo-bazel 0.75.0 rather than what a user needs. A new bazel-cargo.yml workflow runs the flows. The shared harness also strips CARGO_* and rules_rust's repin switches from Bazel's child processes. Only tests, fixtures and workflows change.

Risk: low. No product code changes. The Bazel flows are #[ignore] and cfg(unix), and the new workflow isn't required. The one shared edit adds names to the harness's strip lists. The strip runs before the test's own repo_env and --repo_env, so the CARGO_HOME that a test sets on purpose survives it.

Look here

  • [harness.rs:81](https://github.com/SocketDev/socket-patch/blob/04ce7d606585814e92ac532c66cb23e598d2bec3/crates/socket-patch-cli/tests/e2e_bazel_build/harness.rs#L81): REMOVED_PREFIXES gains CARGO_, which affects every Bazel flow.
  • cargo.rs:601 second_machine: the second clean machine.
  • cargo.rs:671 and cargo.rs:937: the two KNOWN GAP branches. If you'd rather these be failing tests or an issue, say so.
  • cargo.rs:506 asked_for: it counts requests per path, so it can't tell cargo's fetch from Bazel's downloader. Its doc says so.
  • bazel-cargo.yml: the triggers. It is a clone of bazel-npm.yml with contents: read and no secrets.

Verified

  • cargo check -p socket-patch-cli --tests passes.
  • cargo test -p socket-patch-cli --test e2e_bazel_build: 10 passed, 7 ignored (real Bazel isn't installed here). This includes the fixture-lock test and the harness guard test.
  • cargo fmt --all -- --check is clean, and scripts/check-tool-pins.py passes.
  • There is no leftover debug code, and CHANGELOG.md is untouched.
  • CI on 04ce7d60: ci-ok and clippy are green, along with all 72 checks.

Changes I made: none.

Open questions (minor, none blocking):


Generated by Claude Code

@mikolalysenko Mikola Lysenko (mikolalysenko) added the Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review label Nov 19, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[agent] Reviewed at 04ce7d6. Tests, fixture and workflow only, plus the harness scrubbing REPIN and CARGO_* from Bazel children. Both flows ran and passed on 7.7.2, 8.8.2 and 9.3.2, and they assert the built binary's marker, exact lock, manifest and config contents, request counts and a tampered-crate refusal. Non-blocking: two KNOWN GAP branches pin today's behaviour where vendor --check and vex fail after Bazel writes a BUILD.bazel into the vendored copy; the mod cargo; line in main.rs will conflict with #2146.

Merged via the queue into main with commit 3f92b8b Nov 19, 2026
72 checks passed
@mikolalysenko
Mikola Lysenko (mikolalysenko) deleted the bazel-small/51-cargo-rules-rust branch November 19, 2026 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants