Skip to content

Commit 52bbeaa

Browse files
Accept the BUILD.bazel that cargo-bazel writes into a vendored crate (#2190)
* Test the build file a rules_rust build writes into a vendored crate Vendored mode keeps a patched crate under `.socket/vendor/cargo` and points a `[patch.crates-io]` path entry at it. Under a rules_rust crate hub (`crate.from_cargo`) the next build is the patched one, and from rules_rust 0.59.0 on the hub's tool, cargo-bazel, also writes a `BUILD.bazel` into that copy: it renders the build file of a crate the workspace takes from a path to `<path>/BUILD.bazel`, each time Bazel evaluates the hub (`crate_universe/src/rendering.rs`, `render_build_files`). The real-Bazel flow of vendored mode already pinned, as known gaps, that `vendor --check` and `vex` then take the copy for a changed one. This adds what was not yet measured about that file and records what the code does with it today. No product code changes. The crate of the fixture gets a dependency: a second crate, which the stand-in registry serves and the fixture's `Cargo.lock` lists, with a placeholder for its checksum as for the crate itself. Without a dependency the build file that cargo-bazel renders names no repository of the hub, and Bazel analyses it without trouble. The hosted flow builds the same crate and passes as before; the index of its patch registry names the dependency as crates.io's. On real Bazel (`cargo::vendored_from_cargo_with_config`; run on 9.3.2 with rules_rust 0.75.0): - The file starts with the banner of every file cargo-bazel renders. - It makes the copy a package of the user's workspace: `bazel query //...` lists two targets there, and `bazel build //...` passes. The package names the repository of the dependency, which only the hub sees, so `bazel cquery //...` and `bazel query 'deps(//...)'` fail on it (`No repository visible as '@crates__...'`): a third known gap of the flow. With a `.bazelignore` line for `.socket/vendor/cargo`, which the test writes as a user would, `//...` is the user's own target again, both commands pass, and the build is still the patched one: the hub reads the copy by its path, copies it into a repository of its own and renders the build file of that repository again. - So a build file that someone else puts into the copy does not reach the crate: with one under the same banner the build still prints the patched marker. It is a package of the workspace all the same, and `bazel build //...` builds its target. `vendor --check` reports it (`vendor_inventory_mismatch`), as it reports the file cargo-bazel wrote. Without a Bazel (`cargo::a_build_file_in_a_vendored_copy_is_a_changed_copy`), six checkouts with one build file each that the scan did not record: `BUILD.bazel` in the copy's own directory under the banner in a Bazel workspace with a crate hub, the same in a checkout that is no Bazel workspace and in a workspace whose module file makes no hub, one without the banner, one named `BUILD`, and one in `src/`. Every one is a changed copy today: `vendor --check` exits 1, `vex` leaves the patch out and writes no document, and a second `scan --mode vendored` downloads the crate again and removes the file. The scan writes no `.bazelignore` in any of them. `vex` of the flows writes its document beside the checkout through `Cell::document`, which needs no Bazel, so the hermetic test shares it. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * Accept the BUILD.bazel that cargo-bazel writes into a vendored crate In a Bazel workspace with a rules_rust crate hub, the build after `scan --mode vendored` is the patched one, and from rules_rust 0.59.0 on the hub's tool, cargo-bazel, writes a `BUILD.bazel` into the vendored copy under `.socket/vendor/cargo/<uuid>/<crate>/` each time Bazel evaluates the hub. Two things went wrong with that file, right after a patched and green build: - The scan recorded no such file, so every check of the copy against the ledger's inventory took it for a changed one (`vendor_inventory_mismatch`): `vendor --check` exited 1, `vex` left the patch out and wrote no document, and the next vendored scan downloaded the crate again. - Bazel read the file as a package of the user's workspace. For a crate with a dependency that package names a repository only the hub sees, and `bazel cquery //...` and `bazel query 'deps(//...)'` failed on it. Both are fixed where the workspace has such a hub, and nowhere else: `MODULE.bazel` at the Bazel root, or a file it includes, calls `from_cargo` on the extension `crate` of rules_rust's `//crate_universe:extensions.bzl` (or `extension.bzl`, its older name). `bazel::cargo_bazel::hub` reads that with the reader the VEX step uses. The file is no change of the copy The inventory check of a Cargo copy in its place leaves that one file out. It is the file when all of this holds: - a regular file named `BUILD.bazel` in the copy's own directory that the inventory does not list. A build file the crate ships is listed and stays held to the crate's bytes; - it starts with the banner of every file cargo-bazel renders (a line of `#`, `# @generated`, and a line that starts with `# DO NOT MODIFY: This file is auto-generated by a crate_universe tool.`; the template is the same from rules_rust 0.56.0 to 0.75.0); - the project is in a Bazel workspace with a crate hub. In any other project the file is a changed copy, as before. Every recorded file is still compared and any other unrecorded file still fails. `verify_vendored_patch_record` is the one check behind `vendor --check`, `vex`, `repair` and the re-run of a vendored scan, so all four follow: the check passes, `vex` attests the crate, `repair` restores nothing, and a second scan reports the copy `already_vendored` without a download. The exemption is made there, on the files found in the copy, and `verify_dir_inventory`, which also holds a download staged for a copy, compares as before. Only the banner is read. The rest of the file differs from one machine and ruleset release to the next (the command that renders it again, the platforms, the labels of the dependencies), and a hub can add text of the user's own to it, so nothing recorded could be compared with it. So a build file of someone else's under the same banner passes `vendor --check`. It does not reach the crate, because the hub copies the directory into a repository of its own and renders the build file there again (`local_crate_mirror.bzl`): the build still prints the patched marker. Bazel is kept out of the copies `bazel::ignore::sync`, which keeps a `.bazelignore` line for the Go module copies under `.socket`, keeps one for `.socket/vendor/cargo` too, under a comment line of its own (`# socket-patch: patched crate copies, not Bazel packages (to opt out, comment out the next line)`). Every command that can write or remove a vendored copy already ends there, so `scan --mode vendored` writes the pair and `vendor --revert` takes it away, and the rules of the Go lines hold: nothing is added for a directory a line already covers, a commented-out path line is the user's "no", a file that cannot be written is the warning `bazelignore_not_updated`, and a run whose `--ecosystems` leaves `cargo` out adds none. The call now takes the two ecosystems apart and says whose lines changed, so a person reads `patched crate copies` or `patched Go module copies` in the line that reports the file. The detail of the warning speaks of patched copies and names `bazel cquery //...` beside the two commands it named. With the line the copy is no package: `//...` is the user's own targets, `bazel cquery //...` and `bazel query 'deps(//...)'` pass, and a build file that someone else puts into a copy is no package either. The build is the same patched one, because the hub reads the copy by its path. Why only under a hub in the module files: rules_rust's `crates_vendor` in local mode names a path crate by its package in the checkout (`rendering.rs`, `make_deps`; read, not run). There the file in the copy is what the crate is built from, and a line would take the crate out of the build. A hub of the older `WORKSPACE` system is not looked for either. In such a workspace nothing changes: no line, and the file is a changed copy. Not covered: a crate that ships a `BUILD.bazel`. cargo-bazel writes over it (`write_outputs`; read, not run), and the copy reads as changed after a build, as before. Tests The vendored flow on real Bazel loses its three known gaps. The scan writes the `.bazelignore` pair among its files. Right after the build `vendor --check` passes and `vex` attests the crate with either ruleset, `//...` is the consumer alone, and the two commands that analyse every target pass. With the path line commented out, as a user who declines it has it, the package is back where the ruleset writes the file, and both commands fail on the repository of the dependency. `vendor --revert` takes the pair away with the copy. On a second machine the second scan downloads nothing and changes no file; the file under someone else's banner passes the check and is no package under the line, and with the line commented out `bazel build //...` builds its target; the changed library is still `vendor_hash_mismatch`. Without a Bazel, of the six build files of the earlier commit only the first is no change of the copy; the five others, the workspace without a hub among them, are the changed copies they were, and `repair` after the scan restores nothing in any of them. A second test holds the line: written under a hub and not without one, not added by a run that leaves Cargo out, declined by a commented-out path line, removed with the copies, and reported to a person in its own words. Unit tests hold the hub lookup, the predicate and the check, one difference per row, and the pair of the crates in `sync`. The cap of the `bazel` group in `scripts/loc-budget.json` follows the code (2486 to 2552). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * Let cargo-bazel's build file through only where Bazel does not read it A review of the change found that it accepted more, and wrote more, than it had shown to be safe. The acceptance. `vendor --check`, `vex`, `repair` and a repeated vendored scan left an unrecorded `BUILD.bazel` under cargo-bazel's banner out of the comparison wherever the workspace had a crate hub. Only the banner is read, so a file that someone else puts into a vendored copy under that banner passed too, and without a `.bazelignore` line over the copy (a checkout vendored by an earlier release, a file that could not be written, the user's opt-out) Bazel reads that file as a package and `bazel build //...` builds its targets. The file is now let through only in a copy that a line of the root's `.bazelignore` covers, where it is no package. Without the line it is the changed copy it was before this change. The hub. Any `crate.from_cargo` call counted, so the `.bazelignore` pair was written, and the file accepted, for a call in a comprehension or behind a condition, and for the hub of another Cargo workspace of the checkout, which writes nothing into this project's copies. A hub of the project's crates is now a call that is a statement of its own and names the project's `Cargo.lock` as `cargo_lockfile`, or its root `Cargo.toml` among `manifests`, by a label of the main repository written out as a string. Anything else leaves the copies and the file as they are. Smaller things from the same review: - a human run that changed the Go lines for one project and the crate lines for another under the same Bazel root printed only the first message; each kind of copy is now named; - the module files are read only when a pair for the crates is about to be added, not on every command of a project with vendored crates; - the row of the crates in the table of directories says that it is theirs, where the loop compared a string, and `sync` hands back the kinds of copy whose lines changed, not a joined phrase; - one definition of `.socket/vendor/cargo` (`CARGO_VENDOR_DIR`). The docs say where the Bazel root is not found (a submodule, a checkout of another user): both parts are off there. Not changed, and still as the description says: a workspace with both a hub and a `crates_vendor` target in local mode over the same Cargo workspace, and a crate that ships a `BUILD.bazel` of its own. Each needs a build on real Bazel first. The Cargo flows pass on Bazel 9.3.2 with rules_rust 0.75.0; the vendored flow now also holds that with the path line commented out `vendor --check` exits 1, for the file cargo-bazel wrote and for a planted one. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 66fe484 commit 52bbeaa

11 files changed

Lines changed: 1450 additions & 272 deletions

File tree

‎crates/socket-patch-cli/CLI_CONTRACT.md‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2663,7 +2663,7 @@ command's integration tests check its output.
26632663
| `redirect_withdrawn_restore_failed` | `warnings[]` (warning; human: `Warning: …` / `Error: …`) | scan `--mode hosted`: a pin whose patch is `withdrawn` / `not_found` could not be restored to its upstream registry entry (the restore refused it, writing the restored files failed, or the apply lock was held). The pin is kept and no `rolledBack` event is recorded for it; the detail names the remedy (`git checkout -- <lockfile>`, or re-run / `socket-patch rollback <purl>`). A write failure is one warning naming every pin it kept. Status and exit unchanged, unless the failed write could not put a file back: see `lockfile_write_failed`. |
26642664
| `patch_withdrawn` | `rolledBack` / `verified` (hosted event) | scan `--mode hosted`: the lockfile pinned a patch the server reports `withdrawn` or `not_found`; the pin was restored (dry run: would be) to its upstream registry entry. `reason` names the uuid and status. See "Withdrawn pins". |
26652665
| `vendor_prebuilt_stub_invalid` | `failed` | RubyGems: the server stub lacks required attributes or is otherwise invalid; no local stub fallback is permitted. |
2666-
| `bazelignore_not_updated` | run-level `warnings[]` | Go vendored and agent mode in a Bazel workspace (additive): the `.bazelignore` at the Bazel root, which keeps Bazel and gazelle out of the module copies under `.socket/vendor/golang` and `.socket/go-patches`, was not updated. It could not be read or written (a symbolic link or a directory in its place, a read-only directory, a read-only file). The detail names the line to add, or says that the lines of a copy that is gone can be deleted; the command's patch or undo stands and its exit code is unchanged. See docs/ecosystems.md "Bazel workspaces: `.bazelignore`". |
2666+
| `bazelignore_not_updated` | run-level `warnings[]` | Go vendored and agent mode, and Cargo vendored mode under a rules_rust crate hub, in a Bazel workspace (additive): the `.bazelignore` at the Bazel root, which keeps Bazel and gazelle out of the module copies under `.socket/vendor/golang` and `.socket/go-patches` and out of the crates under `.socket/vendor/cargo`, was not updated. It could not be read or written (a symbolic link or a directory in its place, a read-only directory, a read-only file). The detail names the line to add, or says that the lines of a copy that is gone can be deleted; the command's patch or undo stands and its exit code is unchanged. See docs/ecosystems.md "Bazel workspaces: `.bazelignore`". |
26672667
| `redirect_cargo_bazel_config_unread` | `warnings[]` (warning; human: `Warning: …`) | scan/get `--mode hosted` in a Bazel workspace (additive): a rules_rust crate_universe hub (`crate.from_cargo` in `MODULE.bazel` or a file it includes, or `crates_repository` in the `WORKSPACE` file) reads the project's `Cargo.lock` or one of its `Cargo.toml` files, or may read them (its `cargo_lockfile` or `manifests` is not written out as strings, or a use of the extension may hide a call), and its `cargo_config` is not one string that is the label of the cargo config hosted mode writes (`.cargo/config.toml`, or the project's `.cargo/config`). cargo-bazel runs cargo with the config its hub names and no other, so the registry of a pin would be unknown to it and `bazel build` would fail (`registry index was not found in any configuration`). The crate is not pinned: nothing is written for it, its event is `skipped` / `redirect_unconfirmed` (a vendored crate that the scan would take over stays vendored: `skipped` with this code, and `redirect_takeover_kept_vendored`), and status and exit code are unchanged. One warning per crate; the detail names the hub's file and line, the config to commit (created with a comment line in it where the project has none), the attribute to give the hub (`cargo_config = "//:.cargo/config.toml"`; the label is written in the package of the hub's own labels), after which the same command pins the crate, and `--mode vendored`. A hub that names that config is still in the way while the file is there and holds nothing: `rollback` deletes a cargo config it leaves empty, and the hub fails on the missing file; the detail asks for a comment line in it. For a use of the extension that may hide a call the detail names that line to rewrite and no attribute: the next run pins the crate or names the hub that is in the way. A `WORKSPACE` call (`crates_repository`, also under the name a `load` of that file gives it) counts only when no hub of the module files reads the project's Cargo files and the checkout names no Bazel 9 or later. The rewriter's own refusals answer first and keep their codes. A pin that is in place already stays `applied`, and no warning is given for it. Nothing is read or changed in no Bazel workspace, or where no such hub is seen to read the project's Cargo files. See docs/ecosystems.md "Bazel workspaces: Cargo". |
26682668
| `maven_bazel_hub_unpatched` | run-level `warnings[]` (human: `Warning: …`) | Maven in a Bazel workspace, vendored and agent mode (additive): a pinned rules_jvm_external hub of the root `MODULE.bazel` fetches the jar of a Maven patch that this run vendored or applied, or tried to (`vendor`, `apply`, and `get` / `scan` in those modes; in agent mode only for a package it found installed; never under `--global`), and the block that hosted mode keeps in `MODULE.bazel` holds no entry for that jar. The hub builds the jar its own lock (`maven_install.json`) names, so the patch does not reach the Bazel build: these modes patch no hub. One warning per purl; the detail names the lock, the hub, the file and line of its `maven.install`, and the hosted command that patches a pinned hub. The events, the exit code and every written file are those of the same run without the workspace. A `vendor` whose `--ecosystems` leaves Maven out gives none. A vendored run that takes a hosted pin over (`vendor`, `scan` / `get --mode vendored`, `vendor --revert`) leaves the block's entry for it as it is. A hosted run never gives it: see the `maven_bazel_*` rows below and docs/ecosystems.md "Bazel workspaces: Maven". |
26692669
| `maven_bazel_unreadable` | vendored / agent: run-level `warnings[]` (human: `Warning: …`); scan/get `--mode hosted`: `warnings[]` (warning) | Maven in a Bazel workspace (additive): whether a rules_jvm_external hub builds the jar of a Maven patch could not be told, because something could not be read with certainty: a hub without `lock_file` in vendored and agent mode (hosted mode gives `maven_bazel_not_pinned`), a `maven.artifact` or `maven.from_toml` tag that cannot be read, a lock that is missing, empty, not format 2 or 3, larger than 16 MiB or no JSON, a `WORKSPACE` file that calls `maven_install` or cannot be read while `.bazeliskrc` or `.bazelversion` at the Bazel root names no release of Bazel 9 or later, a `maven.install` whose `name` or `lock_file` is not a plain string, a construct of `MODULE.bazel` the reader does not follow, and, in vendored and agent mode, a `maven.override` of the artifact (hosted mode gives `maven_bazel_no_effect`). Hosted mode also gives it for a hub on an isolated `use_extension`, an rc file that cannot be read (also one that an import takes from outside the workspace by a relative path, and one with a line whose reading Bazel's own source calls an oversight: a quote that is never closed, a `\` with nothing after it), a `MODULE.bazel` that is a symbolic link, and a block of `MODULE.bazel` that was changed by hand (also in its first line alone), and then writes no entry for the hub. One warning per purl; the detail names the file, the line and the reason. |
@@ -3291,6 +3291,29 @@ detail names the entry and the declarations. Like the other unattested
32913291
references, re-vendoring cannot clear it, so it is not drift. The check never starts an API client or writes
32923292
lock/recovery files. `--check` conflicts with `--revert`.
32933293

3294+
One file that the ledger's inventory of a directory artifact does not list is not drift:
3295+
the `BUILD.bazel` that rules_rust's cargo-bazel (0.59.0 and newer) writes into a Cargo
3296+
copy each time Bazel evaluates a crate hub that takes the crate from that path. It is
3297+
left out of the comparison when it is a regular file of that name in the copy's own
3298+
directory, starts with cargo-bazel's banner (a line of `#`, `# @generated`, and a line
3299+
that starts with `# DO NOT MODIFY: This file is auto-generated by a crate_universe
3300+
tool.`), the project is in a Bazel workspace with a hub of its crates (the `MODULE.bazel`
3301+
at the Bazel root, or a file it includes, calls `crate.from_cargo` of rules_rust's
3302+
crate_universe extension as a statement of its own, and the call names the project's
3303+
`Cargo.lock` or root `Cargo.toml` by a label written out as a string), and a line of the
3304+
root's `.bazelignore` covers the copy, so that Bazel reads no package there. Only the
3305+
banner is read. The same holds wherever the committed
3306+
copy is verified in its place: `vex` attests the crate, `repair` leaves the copy alone,
3307+
and a second `scan --mode vendored` reports it `already_vendored` without a download.
3308+
Every recorded file, a `BUILD.bazel` the crate ships among them, and every other
3309+
unrecorded file is compared as before (`vendor_inventory_mismatch`), as is a download
3310+
staged for a copy, and outside such a workspace nothing changes. In such a workspace the
3311+
commands that can write or remove a vendored crate also keep a line for
3312+
`.socket/vendor/cargo` in the root's `.bazelignore` (the warning `bazelignore_not_updated`
3313+
when they cannot); with that line commented out, or missing, cargo-bazel's file is drift
3314+
as it was. See
3315+
[Bazel workspaces: the build file in a vendored crate](../../docs/ecosystems.md#bazel-workspaces-the-build-file-in-a-vendored-crate).
3316+
32943317
`vendor --check --local-repo <path>` additionally checks existing suffixed Maven
32953318
jar/POM copies for conflicting bytes. A suffixed Maven or sbt pom is also read
32963319
against its upstream parent poms when `--local-repo` or a Maven or Gradle cache

‎crates/socket-patch-cli/src/commands/mod.rs‎

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,47 +55,56 @@ pub(crate) fn project_state_in_scope(common: &crate::args::GlobalArgs) -> bool {
5555
}
5656

5757
/// In a Bazel workspace the root's `.bazelignore` names the directories of
58-
/// the project's patched Go module copies, so that Bazel and gazelle read
59-
/// no package there (`socket_patch_core::bazel::ignore`). A command that can
60-
/// write or remove such a copy calls this once, after the last step that
61-
/// can change a copy or take one back and before it gives up its apply
62-
/// lock. Nothing is done in a dry run or a `--global` run, and a run whose
63-
/// `--ecosystems` leaves `golang` out only takes away the pair of a copy
64-
/// that is gone. So does the prune pass of a scan, which comes here after
65-
/// its scan has (`adds` false) and can only have removed a copy: a file
66-
/// it cannot write is left to the next command's warning.
58+
/// the project's patched Go module copies, and of the vendored crates a
59+
/// crate hub builds, so that Bazel and gazelle read no package there
60+
/// (`socket_patch_core::bazel::ignore`). A command that can write or
61+
/// remove such a copy calls this once, after the last step that can change
62+
/// a copy or take one back and before it gives up its apply lock. Nothing
63+
/// is done in a dry run or a `--global` run, and a run whose `--ecosystems`
64+
/// leaves `golang` or `cargo` out only takes away that ecosystem's pair of
65+
/// a copy that is gone. So does the prune pass of a scan, which comes here
66+
/// after its scan has (`adds` false) and can only have removed a copy: a
67+
/// file it cannot write is left to the next command's warning.
6768
///
6869
/// A human run is told here that the file changed, or why it could not be;
6970
/// the result is that warning again, for the command's envelope.
7071
pub(crate) async fn sync_bazelignore(
7172
common: &crate::args::GlobalArgs,
7273
adds: bool,
7374
) -> Vec<crate::json_envelope::RunWarning> {
74-
use socket_patch_core::{bazel::ignore, crawlers::Ecosystem::Golang};
75+
use socket_patch_core::bazel::ignore;
76+
use socket_patch_core::crawlers::Ecosystem::{Cargo, Golang};
7577
let mut warnings = Vec::new();
7678
if common.dry_run || common.is_global() {
7779
return warnings;
7880
}
7981
// The agent copies are those of `--cwd`, the vendored ones those of
80-
// the manifest's project. A root both are under is named once.
82+
// the manifest's project.
8183
let mut projects = vec![common.cwd.clone()];
8284
if common.manifest_project_is_foreign() {
8385
projects.push(common.project_root());
8486
}
8587
let (loud, mut told) = (!common.json && !common.silent, Vec::new());
88+
let [go, cargo] = [Golang, Cargo].map(|eco| adds && common.ecosystem_selected(eco));
8689
for project in projects {
87-
match ignore::sync(&project, adds && common.ecosystem_selected(Golang)).await {
88-
Ok(Some(file)) if loud && !told.contains(&file) => {
89-
let (verb, what) = match file.exists() {
90-
true => (
91-
"Updated",
92-
"the lines that keep patched Go module copies under .socket out of \
93-
Bazel's packages",
90+
match ignore::sync(&project, go, cargo).await {
91+
Ok(Some((file, copies))) if loud => {
92+
// A root that both projects are under is named once for
93+
// each kind of copy whose lines changed there.
94+
let (shown, copies) = (file.display(), copies.join(" and "));
95+
let line = match file.exists() {
96+
true => format!(
97+
"Updated {shown}: the lines that keep patched {copies} copies under \
98+
.socket out of Bazel's packages"
9499
),
95-
false => ("Removed", "no patched Go module copy is left under .socket"),
100+
false => {
101+
format!("Removed {shown}: no patched {copies} copy is left under .socket")
102+
}
96103
};
97-
println!("{verb} {}: {what}", file.display());
98-
told.push(file);
104+
if !told.contains(&line) {
105+
println!("{line}");
106+
told.push(line);
107+
}
99108
}
100109
Err(detail) if adds => {
101110
if loud {

0 commit comments

Comments
 (0)