Repository navigation
Commit 52bbeaa
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
- src/commands
- tests
- e2e_bazel_build
- fixtures/bazel/rules_rust/bzlmod/common
- socket-patch-core/src
- bazel
- vendor
- docs
- scripts
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2663 | 2663 | | |
2664 | 2664 | | |
2665 | 2665 | | |
2666 | | - | |
| 2666 | + | |
2667 | 2667 | | |
2668 | 2668 | | |
2669 | 2669 | | |
| |||
3291 | 3291 | | |
3292 | 3292 | | |
3293 | 3293 | | |
| 3294 | + | |
| 3295 | + | |
| 3296 | + | |
| 3297 | + | |
| 3298 | + | |
| 3299 | + | |
| 3300 | + | |
| 3301 | + | |
| 3302 | + | |
| 3303 | + | |
| 3304 | + | |
| 3305 | + | |
| 3306 | + | |
| 3307 | + | |
| 3308 | + | |
| 3309 | + | |
| 3310 | + | |
| 3311 | + | |
| 3312 | + | |
| 3313 | + | |
| 3314 | + | |
| 3315 | + | |
| 3316 | + | |
3294 | 3317 | | |
3295 | 3318 | | |
3296 | 3319 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
67 | 68 | | |
68 | 69 | | |
69 | 70 | | |
70 | 71 | | |
71 | 72 | | |
72 | 73 | | |
73 | 74 | | |
74 | | - | |
| 75 | + | |
| 76 | + | |
75 | 77 | | |
76 | 78 | | |
77 | 79 | | |
78 | 80 | | |
79 | 81 | | |
80 | | - | |
| 82 | + | |
81 | 83 | | |
82 | 84 | | |
83 | 85 | | |
84 | 86 | | |
85 | 87 | | |
| 88 | + | |
86 | 89 | | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
94 | 99 | | |
95 | | - | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
96 | 103 | | |
97 | | - | |
98 | | - | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
99 | 108 | | |
100 | 109 | | |
101 | 110 | | |
| |||
0 commit comments