Skip to content

Write no hosted Cargo pin that a rules_rust hub would fail on - #2192

Open
Mikola Lysenko (mikolalysenko) wants to merge 4 commits into
mainfrom
bazel-guard/cargo-hosted-config
Open

Mikola Lysenko (mikolalysenko) wants to merge 4 commits into
mainfrom
bazel-guard/cargo-hosted-config

Conversation

@mikolalysenko

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

Copy link
Copy Markdown
Collaborator

In a Bazel workspace, scan --mode hosted and get --mode hosted no longer pin a crate while a rules_rust crate_universe hub reads the project's Cargo files without its cargo config: the crate is skipped with the new warning redirect_cargo_bazel_config_unread instead of leaving bazel build broken.

Based on main, in two commits, a merge of main and one commit of review fixes. The first commit is tests only and records what main does today; the second is the change, with the expectations of the first changed to match.

Decisions for the maintainer

  1. An uncertain reading of a hub withholds the pin. Wherever a crate_universe hub, or a use of its extension, is seen and cannot be read with certainty (a lock or manifest label that is not written out, a cargo_config that is not one string, a call that is not a statement of its own), the crate is skipped with a warning that names the line. The price is a missed patch with an explanation; the alternative is to write as before whenever the reading is not certain, which leaves those builds to break.
  2. A WORKSPACE file left behind by a move to Bzlmod can withhold a pin. A crates_repository call in a WORKSPACE file counts when no from_cargo hub of the module files concerns the project and the checkout does not name Bazel 9.0.0 or later. On Bazel 8, or where no release is named, Bazel may not read that file at all, and the pin is withheld all the same. The warning names the call.
  3. The WORKSPACE spelling is read, not measured. That a WORKSPACE hub fails on a pin, also under labels that use the workspace's own name, is read from the ruleset and from how Bazel names the main repository. The flows on real Bazel are Bzlmod only.
  4. Size. The implementation is +364 / -13 lines, of which 203 hold code, against a first estimate of about 180 lines. The growth is the WORKSPACE reading, the second remedy text for a line that may hide a hub, the one-string check of cargo_config, and 49 code lines for the five review fixes below.
  5. Exit code 0. A withheld crate exits 0 with a warning, as every other crate hosted mode leaves out does today. The alternative is exit 1 for this one case.
  6. A general doubt about the workspace files withholds nothing where no hub is seen, a MODULE.bazel that cannot be read included. The alternative protects against a hub hidden behind an unreadable construct and takes hosted mode from unrelated Cargo projects beside any such workspace. A middle is possible and not built: withhold only where the root module depends on rules_rust and a module file cannot be read.
  7. An existing pin on such a hub stays silent. It could get a warning that the Bazel build is failing and how to mend it.
  8. The comment-line advice, or the rollback fix first. A user who gives the hub the attribute and creates no file gets a patched build, because the scan creates the config; a later rollback exits 0, deletes that config and the build fails, as it did before the scan. The warning puts the file first. A config that is there and holds nothing is now held back with its own warning, because there the build passed before the scan. The alternative is to land a fix that keeps the cargo config a hub names through rollback, and to drop both.
  9. The code name, redirect_cargo_bazel_config_unread.

What the code does today

A crate_universe hub of rules_rust (crate.from_cargo in MODULE.bazel, crates_repository in a WORKSPACE file) runs cargo over the Cargo.toml files and the Cargo.lock it names, in a directory of its own. Its tool, cargo-bazel, links what stands beside each manifest into that directory, writes a root manifest of its own there, removes every cargo config it finds there and puts back one: the file the hub names as cargo_config, and none when the hub names none (symlink_roots, write_root_manifest and setup_cargo_config in crate_universe/src/splicing/splicer.rs, rules_rust 0.56.0 and 0.75.0).

Hosted mode names a registry of its own on the dependency in Cargo.toml and defines it in the project's .cargo/config.toml. The hub of the rules_rust README names no cargo_config:

crate.from_cargo(
    name = "crates",
    cargo_lockfile = "//:Cargo.lock",
    manifests = ["//:Cargo.toml"],
)

On that hub socket-patch scan --mode hosted --yes exits 0, reports the crate applied and rewrites .cargo/config.toml, Cargo.lock and Cargo.toml. The next bazel build fails in the hub:

registry index was not found in any configuration: `socket-patch-<uuid>`

The first commit holds exactly that on real Bazel (9.3.2 with rules_rust 0.75.0 and 7.7.2 with 0.56.0), with the product code as it is.

What changes

For a run with a Cargo candidate, in a Bazel workspace (a MODULE.bazel, REPO.bazel, WORKSPACE or WORKSPACE.bazel in the project's directory or above it, up to the root of its git checkout), hosted mode reads the workspace files for the calls that make a crate_universe hub:

  • the from_cargo tags of the crate extension of rules_rust in MODULE.bazel and the files it includes (//crate_universe:extensions.bzl, and extension.bzl, which hands the same extension on);
  • crates_repository(...) calls of the WORKSPACE file, also under the name a load of that file gives the rule, when no such tag concerns the project and the checkout names no Bazel 9.0.0 or later, which never reads that file. Bazel 7 reads that file beside the module files.

A hub concerns the project when a label of its cargo_lockfile or its manifests is the project's Cargo.lock, its Cargo.toml or the manifest of a workspace member. @<name>// is the checkout under the root module's name, and in a WORKSPACE file under the name its workspace(name = ...) gives the main repository. The hub is in the way unless its cargo_config is one string, the label of the config the rewriter writes (.cargo/config.toml, or the project's legacy .cargo/config), and also while that file is there and holds nothing: a rollback of the pin would delete it, and the hub fails on a missing file.

For a crate the rewriter would otherwise pin, nothing is written. The crate is skipped (redirect_unconfirmed), the exit code is 0 and the status success, as for every other crate hosted mode leaves out, and one warning per crate says why and names the two ways on:

Warning: cfg-if@1.0.4: the rules_rust hub at MODULE.bazel:10 reads the Cargo files of this project, and it names no `cargo_config`. cargo-bazel hands the cargo it runs no cargo config but the one its hub names, and without .cargo/config.toml, where hosted mode defines the patch registry, that cargo fails on the pinned Cargo.toml (`registry index was not found in any configuration`) and `bazel build` with it; dependency skipped (nothing rewritten). Commit .cargo/config.toml (where the project has none, create it with a comment line in it: `rollback` deletes a config it leaves empty, and a hub that names a missing file fails), give the hub `cargo_config = "//:.cargo/config.toml"`, the label of that file as one string, and run this command again; or use `--mode vendored`, which commits the patched crate and needs no registry

The label in the warning is written in the package of the hub's own labels: for a hub that names //:svc/app/Cargo.toml it is //:svc/app/.cargo/config.toml.

Where the reason is a line that may hide a hub, the hub that is seen may name the config already and there may be no other, so the warning names that line and no attribute:

Warning: cfg-if@1.0.4: MODULE.bazel:17: `crate` is used other than to call a tag, so a rules_rust hub that socket-patch cannot read may take the Cargo files of this project. [...] dependency skipped (nothing rewritten). Write that line as the call of a tag (`crate.from_cargo(...)`) or as `use_repo(...)` and run this command again: it then pins the crate, or names the hub that is in the way; or use `--mode vendored`, which commits the patched crate and needs no registry
  • The rewriter's own refusals answer first and keep their codes.
  • A pin that is in place already stays what it was: applied, nothing to write, no warning.
  • A vendored crate that the scan would take over stays vendored and patched (redirect_takeover_kept_vendored).
  • The model is redirect_cargo_directory_source: the same function, the same exit code, the same shape of text.

Nothing changes outside a Bazel workspace, and nothing for a project that no hub is seen to read: a workspace without such a hub, a hub that names the config, a hub whose labels are all written out and name other files (another directory, another repository), the tag of the same name of another ruleset. Measured against a binary of the base in pairs of identical checkouts, as JSON, as a person and as a dry run: 27 of 27 pairs, over nine such shapes, have the same exit code, the same stdout and stderr and the same files, byte for byte.

Where the reading is not certain

A broken build weighs more than a patch that waits for one line, so wherever a hub, or a use of the extension through a name bound to it, is seen, a reading that is not certain withholds the pin, and the warning says what could not be read:

  • a cargo_lockfile or manifests that is not written out as a string or a list of strings (a constant, a concatenation, an attribute left to **): the hub may read the project's files;
  • a cargo_config that is not written out as one string is not taken for the right file, a list of one label included;
  • a call that is not a statement of its own counts: it may run;
  • a use of the extension that may hide a call counts;
  • a WORKSPACE call counts as described above. Bazel 7 reads that file and Bazel 8 when it is told to, so a WORKSPACE file that a move to Bzlmod left behind can withhold a pin where the module files make no hub. The warning names the call. Where workspace(name = ...) is not written out as a string, any @name// label of a lock or a manifest in that file is taken for the checkout, and none is as a cargo_config.

Where no hub is seen at all, nothing is withheld, also when something else in the workspace files cannot be read. Withholding there would take hosted mode from every Cargo project beside a Bazel workspace that has nothing to do with rules_rust.

Contents

Two commits.

  1. Tests only: the real-Bazel flow cargo::hosted_from_cargo_without_config, which records the break (tests +141 / -9, workflow header +11 / -8 comment lines).
  2. The change, with its tests and docs.
lines
Implementation +364 / -13, of which 203 hold code: bazel/cargo.rs 178, patch/redirect/mod.rs 13, hosted/engine.rs 11, bazel/mod.rs 1
Unit tests in src +674
tests/e2e_bazel_build/cargo.rs +515 / -13
Fixture files 0
Docs (docs/ecosystems.md, CLI_CONTRACT.md) +143 / -2
bazel-cargo.yml +11 / -8, comments

scripts/loc-budget.json: the cap of the bazel group follows the code, 2486 to 2665.

The engine reads the workspace with each read of the candidate files and hands the reason to the cargo rewriter under a synthetic key among those files, as it does for the hubs of rules_jvm_external. The check sits where the three plans of a crate are complete, so it withholds only what would be written.

What it proves, and where

  • Real Bazel 9.3.2, 8.8.2 (rules_rust 0.75.0) and 7.7.2 (rules_rust 0.56.0), macOS arm64, run the way bazel-compatibility.yml runs a leg; on 9.3.2 again after the merge of main and after the review fixes. The flow on the README hub: the build prints the unpatched marker; the scan withholds, changes no file and a person reads why; the same build prints the same marker, changes no file and passes with --lockfile_mode=error. Then both ways the warning names. Vendored mode on that hub: the build prints the patched marker, and vendor --revert with the next build gives every file back. The config committed and the one attribute on the hub: the same hosted scan applies and the build prints the patched marker, from the patch host. 124 s, 58 s and 110 s for this flow; 217 s, 174 s and 188 s for the three Cargo flows of a leg. The two existing flows pass unchanged.
  • Held against a binary of the base on 9.3.2, the flow fails where it asks that the scan applied nothing.
  • Without a Bazel (e2e_bazel_build, the built binary in checkouts of the fixture's Cargo files): on the README hub scan, its dry run and get write nothing; a line that may hide a hub withholds beside a hub that names the config, and the warning names the line; with the file and the attribute the same scan pins and rollback gives the checkout back; in six shapes where no hub reads the project the envelope and the three files are those of a checkout in no Bazel workspace; a vendored crate stays vendored.
  • Unit tests: twelve over workspace files (which hub concerns which project, every spelling of the right config, wrong and unreadable ones, a named config that holds nothing, the label the warning gives, the calls that count, a hidden call and its own way on, the WORKSPACE file per Bazel release, beside a module hub of another project, under a loaded name of the rule and under the names it gives the checkout), one for the rewriter (each of the three files alone is enough to withhold), one for the engine (the lock and each manifest handed over).
  • Mutations of the product, one at a time: 24 ran against the first version of this change, and 11 against what changed since (the two remedy texts, the WORKSPACE name, the one-string config, and four on what is withheld and handed over, three of which the first set of tests had let through). Each is caught by a unit test.
  • The whole unit suite of the core crate (8300 tests) and every existing test target that exercises hosted Cargo pass with no expectation changed.

The flow keeps to the loopback stand-in. A hub without cargo_config cannot be handed a mirror through it, so the flow does what the rules_rust README says for a private registry: CARGO_BAZEL_ISOLATED=false, and the mirror is named in the cargo home of the test. cargo-bazel itself looks up a crates.io crate in what that cargo home keeps of the crates.io index, so the test writes those two files there, as a cargo home holds them that has fetched the crate from crates.io.

Not covered

In the first four a hub without the config fails as before.

  • A hub that another Bazel module declares; a crates_repository inside a .bzl file that the WORKSPACE file loads, or called under a name that no load of that file gives it.
  • Where no hub is seen: a tag called on use_extension(...) itself, with no name bound to it; a MODULE.bazel that cannot be read (a link that leads out of the Bazel root, a file past the reader's bounds); and what else the workspace files hide from every reading.
  • Flags and rc files: none is read, in the checkout or outside it. --noenable_bzlmod has Bazel read the WORKSPACE file in place of module files that make a hub.
  • A Bazel root above a project that is in no git checkout: only the project's own directory is looked at for a workspace file there.
  • The in-memory hosted engine, which is handed a project's files and reads no Bazel file.
  • crate.from_specs hubs and crates_vendor targets.
  • A hub with a cargo-bazel lock (lockfile) asks for a repin after any change of Cargo.lock. Unchanged.
  • A pin that an earlier release wrote on such a hub stays, reported as applied. That build is failing already.
  • socket-patch vex attests a hosted pin from the three files and does not look at the hub.
  • WORKSPACE hubs and rules_rs have unit and hermetic tests only, no flow on real Bazel: that a WORKSPACE hub fails on a pin is read from the ruleset and from how Bazel names the main repository, not measured. Linux and Windows were not run locally; the workflow's Linux legs run on this pull request.
  • One held gap: rollback deletes a cargo config it leaves empty, and a hub that names a missing file fails (Failed to splice workspace crates; measured once on Bazel 9.3.2, held by no test). A config that is there and holds nothing gets no pin for that reason. A hub that names a file which is not there yet still gets the pin, which creates the file, and a later rollback deletes it again: that build failed before the scan too. The warning says to create the file with a comment line in it, and a hermetic test holds the deletion so that the advice is dropped when that is fixed.
  • A warning's text is matched against package names where a vendored package fails its takeover by a hosted scan: this warning holds everyday words (config, registry, build) that are also names of packages, so in a run that withholds a crate and fails the takeover of such a package for another reason, that package's event can carry this code. The matcher is the takeover's own and treats every warning that way.

Code review

A review of this pull request gave ten findings. Each was checked against the code; five were fixed in one commit, with a unit row for each that fails on the code as it was (three shown by running the rows first, two by a mutation of the fix), and the engine's copy of a rule was replaced by the rewriter's own.

Fixed:

  1. A WORKSPACE hub beside another project's module hub got the pin. The WORKSPACE file was looked at only when the module files made no crate.from_cargo hub at all, and Bazel 7 reads both. It now counts when no module hub concerns the project.
  2. The rule under a loaded name was not seen (load(..., crates = "crates_repository")). The load is now followed.
  3. A hub that names a config which holds nothing got the pin, and a later rollback deleted the file and left the hub failing, where the build passed before the scan. Such a hub is now in the way, and the warning asks for a comment line.
  4. The label in the warning could name no package. It was always written in the project's directory; it is now written in the package of the hub's own labels.
  5. Another repository's config passed under a workspace(name = ...) that is not written out: the leniency for what a hub may read was also applied to what names the project's config.
  6. Three copies of the rule for which cargo config is written. The engine now calls the rewriter's cargo_config_key; the in-memory stage keeps its own, which reads another type.

Not fixed, and why:

  1. A module file that cannot be read withholds nothing where no hub is seen. This is decision 6 above; the narrower middle the review suggests is named there.
  2. A pin that is in place on such a hub gets no warning. This is decision 7 above. The warning's text says the crate was skipped, which would be untrue there, so it needs a second form.
  3. The workspace files are read with each read of the candidate files, and again by the Maven lane in a run that has both. The files are bounded at 1 MiB each; sharing one read between the two lanes is a change to the Maven lane.
  4. The takeover's matcher can read this warning as naming a package called config, registry or build. It is the last entry of Not covered: the matcher treats every warning that way, and the fix belongs there.

🤖 Generated with Claude Code

The real-Bazel Cargo flows run on a hub that names the project's cargo
config (`cargo_config = "//:.cargo/config.toml"`). The hub of the
rules_rust README names none:

    crate.from_cargo(
        name = "crates",
        cargo_lockfile = "//:Cargo.lock",
        manifests = ["//:Cargo.toml"],
    )

cargo-bazel links what stands beside each manifest such a hub names into
a directory of its own and writes a root manifest of its own there. It
then removes every cargo config it finds there and puts back only the
file the hub names (`symlink_roots`, `write_root_manifest` and
`setup_cargo_config` in crate_universe/src/splicing/splicer.rs,
rules_rust 0.56.0 and 0.75.0). Hosted mode names a registry of its own
on the dependency in Cargo.toml and defines it in `.cargo/config.toml`,
so on this hub the cargo that cargo-bazel runs has never heard of that
registry.

`cargo::hosted_from_cargo_without_config` holds what the release does
there: the build passes on the upstream crate; `scan --mode hosted
--yes` exits 0, reports the crate applied and rewrites
`.cargo/config.toml`, `Cargo.lock` and `Cargo.toml`; and the next `bazel
build //:consumer` fails in the hub with cargo's

    registry index was not found in any configuration: `socket-patch-<uuid>`

It passes on Bazel 9.3.2 (rules_rust 0.75.0) and 7.7.2 (rules_rust
0.56.0) with the product code as it is. The next commit changes the
product and these expectations.

The flow is the fixture of the other two with one per-test edit: the
`cargo_config` line is taken out of the era's MODULE.bazel, and the
checkout has no cargo config. The hermetic test of the fixture holds
that every era's hub is the README's but for that line.

The crate under test still comes from the loopback stand-in and from no
other host. The README says how a hub without a config reaches a private
registry: with `CARGO_BAZEL_ISOLATED=false` it runs its cargo with the
cargo home of the machine, and the test's cargo home names the mirror in
its `config.toml`. cargo-bazel itself reads no cargo config the hub does
not name. For a crate the lock takes from crates.io it goes by what the
cargo home keeps of that index: `config.json` for the download URL, and
with rules_rust 0.56.0 the crate's cached file of the index as well. A
cargo behind a mirror fetches neither, so the test writes both into its
cargo home, as a home holds them whose cargo fetched the crate from
crates.io itself. `cargo_env` takes the isolation switch as an argument
for that; the two existing flows pass `true`, as before.

bazel-cargo.yml: the header names the third flow. A leg still holds two
workspace copies at a time (`--test-threads=2`).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A crate_universe hub of rules_rust runs cargo over the Cargo.toml files
and the Cargo.lock it names, in a directory of its own, with one cargo
config: the file the hub names as `cargo_config`, and none when it
names none (the previous commit has the source). Hosted mode names a
registry of its own on the dependency in Cargo.toml and defines it in
the project's `.cargo/config.toml`. On the hub of the rules_rust README,
which names no `cargo_config`, `scan --mode hosted` exited 0 and
reported the crate applied, and the next `bazel build` failed:

    registry index was not found in any configuration: `socket-patch-<uuid>`

A command that exits 0 must not leave a build broken. Hosted mode now
writes no pin for a crate while such a hub is in the way.

What changes

In a Bazel workspace (`bazel::bazel_root`: a workspace file in the
project's directory or above it, up to the root of its git checkout),
`scan --mode hosted` and `get --mode hosted` read the workspace files
for the calls that make a crate_universe hub:

- the `from_cargo` tags of the `crate` extension of rules_rust in
  MODULE.bazel and the files it includes, under either file the ruleset
  defines the extension in (`//crate_universe:extensions.bzl`, and
  `extension.bzl`, which hands it on; rules_rust 0.56.0 and 0.75.0);
- `crates_repository(...)` calls of the WORKSPACE file, when the module
  files make no such tag and the checkout names no Bazel 9.0.0 or later,
  which never reads that file.

A hub concerns the project when a label of its `cargo_lockfile` or its
`manifests` is the project's Cargo.lock, its Cargo.toml or the manifest
of a workspace member. `@<name>//` is the checkout under the root
module's name, and in a WORKSPACE file under the name that
`workspace(name = ...)` gives the main repository
(`WorkspaceGlobals.workspace`, Bazel 7.7.2 and 8.8.2). The hub is in the
way unless its `cargo_config` is one string, the label of the config the
rewriter writes (`.cargo/config.toml`, or the project's legacy
`.cargo/config`).

For a crate the rewriter would otherwise pin, nothing is written. The
crate is `skipped` (`redirect_unconfirmed`), status and exit code are
those of every other crate hosted mode leaves out (0), and one warning
per crate, `redirect_cargo_bazel_config_unread`, names the hub by file
and line and the two ways on:

    <crate>@<version>: the rules_rust hub at MODULE.bazel:10 reads the
    Cargo files of this project, and it names no `cargo_config`. [...]
    dependency skipped (nothing rewritten). Commit .cargo/config.toml
    (where the project has none, create it with a comment line in it:
    `rollback` deletes a config it leaves empty, and a hub that names a
    missing file fails), give the hub `cargo_config =
    "//:.cargo/config.toml"`, the label of that file as one string, and
    run this command again; or use `--mode vendored`, which commits the
    patched crate and needs no registry

The file comes first in it. A hub that is given the attribute alone
fails on the missing file (`Failed to splice workspace crates`; measured
once on Bazel 9.3.2, held by no test), and a config that a scan created
is deleted by the `rollback` that empties it; a comment line keeps it.

Where the reason is a use of the extension that may hide a hub, the hub
that is seen may name the config already, and there may be no other. The
warning then names that line and no attribute:

    <crate>@<version>: MODULE.bazel:17: `crate` is used other than to
    call a tag, so a rules_rust hub that socket-patch cannot read may
    take the Cargo files of this project. [...] Write that line as the
    call of a tag (`crate.from_cargo(...)`) or as `use_repo(...)` and
    run this command again: it then pins the crate, or names the hub
    that is in the way; or use `--mode vendored`, [...]

The rewriter's own refusals answer first and keep their codes. A pin
that is in place already stays what it was: applied, with nothing to
write and no warning. A vendored crate that the scan would take over
stays vendored and patched (`redirect_takeover_kept_vendored`).

The model is `redirect_cargo_directory_source`: the same place in
`rewrite_cargo_with`, the same exit code and the same shape of text. The
check sits where the three plans of a crate are complete, so that it
withholds only what would be written. The engine reads the workspace
with each read of the candidate files, for a run with a cargo candidate
on disk, and hands the reason to the rewriter under a synthetic key
among those files, as it does for the hubs of rules_jvm_external.

Where the reading is not certain

A broken build weighs more than a patch that waits for one line of the
user's, so wherever a hub, or a use of the extension through a name
bound to it, is seen, a reading that is not certain withholds the pin,
and the warning says what could not be read:

- a `cargo_lockfile` or `manifests` that is not written out as a string
  or a list of strings (a constant, a concatenation, an attribute left
  to `**`): the hub may read the project's files;
- a `cargo_config` that is not written out as one string is not taken
  for the right file, a list of one label included;
- a call that is not a statement of its own counts: it may run;
- a use of the extension that may hide a call (`Tags::doubts`) counts;
- a WORKSPACE call counts as above. Bazel 7 reads that file and Bazel 8
  when it is told to, so a WORKSPACE file that a move to Bzlmod left
  behind can withhold a pin on Bazel 8 where the module files make no
  hub. The warning names the call. Where `workspace(name = ...)` is not
  written out as a string, any `@name//` label of that file is taken
  for the checkout.

Where no hub is seen to concern the project, nothing is read further,
said or written: in no Bazel workspace, in a workspace without such a
hub, for a hub that names the config, for a hub whose labels are all
written out and name other files, and for the tag of the same name of
another ruleset. A doubt of the reader about the workspace files in
general (`Workspace::doubts`) withholds nothing where no hub is seen:
it would take hosted mode from every Cargo project beside a Bazel
workspace that has nothing to do with rules_rust.

Tests

- bazel::cargo: eleven unit tests over workspace files: the README hub
  and its warning, each spelling of the right config, a wrong or
  unreadable config, which files concern which project, hubs whose
  files are not written out, every call that counts, tags of other
  extensions, a hidden call and its own way on, workspaces without a
  hub, the WORKSPACE file per Bazel release and under the names it
  gives the checkout, and the read from a checkout on disk.
- The rewriter: one test. Each of the three files alone is enough to
  withhold (no lock, a config that lost the registry, a config that
  defines it already). The engine: one test, the lock and each manifest
  handed over.
- e2e_bazel_build, without a Bazel: the binary in checkouts of the
  fixture's Cargo files. On the README hub `scan`, its dry run and `get`
  write nothing and a person reads why; a line that may hide a hub
  withholds beside a hub that names the config, and the warning names
  the line; with the file and the attribute the same scan pins, and
  `rollback` gives the checkout back. Where no hub reads the project
  (six shapes) the envelope and the three files are those of a checkout
  without a Bazel workspace, byte for byte. A vendored crate stays
  vendored.
- e2e_bazel_build on real Bazel: `cargo::hosted_from_cargo_without_config`
  now holds that the scan withholds, that the same build still prints
  the unpatched marker and changes no file, and then both ways the
  warning names, on the hub of the README: `scan --mode vendored` and
  the build prints the patched marker, and `vendor --revert` gives the
  checkout back; with the committed config and the one attribute the
  hosted scan applies and the build prints the patched marker, from the
  patch host. Bazel 9.3.2 and 8.8.2 (rules_rust 0.75.0) and 7.7.2
  (rules_rust 0.56.0), macOS arm64: 124 s, 58 s and 110 s for this
  flow, 217 s, 174 s and 188 s for the three Cargo flows of a leg, two
  at a time. Held against a binary of the parent commit, the flow fails
  where it asks that the scan applied nothing.

One held gap: `rollback` deletes a cargo config it leaves empty, and a
hub that names a missing file fails. The warning says to create such a
file with a comment line in it, and the hermetic test holds the deletion
so that the advice goes when that is fixed.

Not covered

In the first four a hub without the config fails as before.

- A hub that another Bazel module declares; a `crates_repository` in a
  `.bzl` file that the WORKSPACE file loads, or called under another
  name.
- Where no hub is seen: a tag called on `use_extension(...)` itself,
  with no name bound to it; a MODULE.bazel that cannot be read (a link
  that leads out of the Bazel root, a file past the reader's bounds);
  and what else the workspace files hide from every reading.
- Flags and rc files: none is read, in the checkout or outside it.
  `--noenable_bzlmod` has Bazel read the WORKSPACE file in place of
  module files that make a hub.
- A Bazel root above a project that is in no git checkout: only the
  project's own directory is looked at for a workspace file there.
- The in-memory hosted engine, which is handed a project's files and
  reads no Bazel file.
- `crate.from_specs` hubs and `crates_vendor` targets.
- A hub with a cargo-bazel lock (`lockfile`) asks for a repin after any
  change of Cargo.lock, a pin included. That is unchanged.
- A pin an earlier release wrote on such a hub stays and stays reported
  as applied; that build is failing already.

docs/ecosystems.md has the section "Bazel workspaces: Cargo", and
CLI_CONTRACT.md the code. scripts/loc-budget.json: the bazel group's cap
follows the code, 2486 to 2618.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Two files conflicted, each in one long line that both sides had added to:

- CLI_CONTRACT.md, the hosted paragraph: main's sentences on the patch
  mirror and on pixi projects are kept, and this branch's pointer to
  `redirect_cargo_bazel_config_unread` is put back in its place.
- docs/ecosystems.md, the support table: main's new pixi row is kept, and
  the Cargo row keeps this branch's sentence on Bazel workspaces.

`patch/redirect/mod.rs` and `hosted/engine.rs` merged without a conflict.
The `bazel` line budget is unchanged by main (2618 of 2618).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A review of the withhold found five cases where a pin could still be
written that a rules_rust hub fails on, or where the warning sent the user
the wrong way. Each has a unit row that fails on the code as it was.

- A `crates_repository` of the `WORKSPACE` file was looked at only when
  the module files made no `crate.from_cargo` hub at all. Bazel 7 reads
  both, so a workspace whose module hub is over another project's files
  and whose `WORKSPACE` hub reads this one got the pin. The file now
  counts when no hub of the module files concerns the project.
- The rule was known by its own name alone. A `load` that gives it
  another (`load(..., crates = "crates_repository")`) is now followed.
- A hub that names the right config was passed although the file held
  nothing. The pin is written into it, a later `rollback` deletes the file
  it leaves empty, and the hub then fails on the missing file: a build
  that passed before the scan. Such a hub is now in the way, and the
  warning asks for a comment line. A file that is not there yet is still
  created by the pin, as before.
- The warning gave the label of the config in the project's directory
  (`//svc/app:.cargo/config.toml`). Where the hub names the project's
  files from another package (`//:svc/app/Cargo.toml`), that directory is
  no package and the label names nothing. The label is now written in the
  package of the hub's own labels.
- Under a `workspace(name = ...)` that is not written out, any `@name//`
  label was taken for the checkout, also as `cargo_config`. That widens
  what a hub may read, which is safe, and what names the project's
  config, which is not: another repository's config passed. It now
  applies to the lock and the manifests only.

The engine also had a third copy of the rule for which cargo config the
rewriter writes. It now calls the rewriter's own, `cargo_config_key`.

The hermetic test that held the `rollback` gap now holds both halves: a
config that holds nothing gets no pin, and a config that the pin creates
is still deleted by `rollback`. The `bazel` line budget follows the code,
2618 to 2665.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review November 20, 2026 19:57
@cursor

cursor Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes hosted-mode behavior for Cargo in Bazel/monorepos and adds static analysis of Starlark workspace files; mistakes could skip legitimate pins or miss edge-case hubs, but the change is fail-safe (withhold rather than break builds).

Overview
Hosted Cargo in Bazel workspaces no longer writes registry pins when a rules_rust crate_universe hub reads the project's Cargo.lock / manifests but does not point cargo_config at the file socket-patch uses—avoids registry index was not found and broken bazel build. The crate is skipped with warning redirect_cargo_bazel_config_unread, exit 0, and guidance to commit .cargo/config.toml, set cargo_config = "//:.cargo/config.toml" on the hub, or use --mode vendored.

New logic in crates/socket-patch-core/src/bazel/cargo.rs scans MODULE.bazel (crate.from_cargo) and, when applicable, WORKSPACE / crates_repository, including ambiguous labels and hidden extension uses. The hosted engine injects the result via a synthetic key into the cargo rewriter, which withholds writes; existing pins stay applied without a warning.

Tests and CI: E2E hosted_from_cargo_without_config (README-style hub), hermetic checkout tests, and expanded bazel-cargo.yml coverage; loc budget for bazel/ raised.

Reviewed by Cursor Bugbot for commit 64c75e9. Configure here.

@mikolalysenko Mikola Lysenko (mikolalysenko) added the Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review label Nov 20, 2026
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.

1 participant