Repository navigation
Attest a Cargo or NuGet patch as affected in VEX where a Bazel hub builds the package from the lock - #2224
Attest a Cargo or NuGet patch as affected in VEX where a Bazel hub builds the package from the lock#2224Mikola Lysenko (mikolalysenko) wants to merge 5 commits into
Conversation
…om the lock Two flows on real Bazel, tests only, that record what socket-patch does today in a workspace where a ruleset builds a package from the project's lock and agent mode patches the copy a package manager unpacked outside the checkout: - `cargo::agent_from_cargo_with_config`: rules_rust's `crate.from_cargo(cargo_lockfile = "//:Cargo.lock", ...)`, and the crate in `$CARGO_HOME/registry/src`, as after `cargo fetch`. - `nuget::agent_from_lock`: rules_csharp's `nuget.from_lock(packages_lock_json = "//:packages.lock.json", ...)`, and the package in NuGet's global packages folder, as after `dotnet restore`. In both, `scan --mode agent --yes` exits 0 with the patch applied to that copy and writes nothing into the checkout but `.socket/`, and `apply` passes. `bazel build` and `bazel run //:consumer` print the unpatched marker before the command and after it: each hub has Bazel download the package the lock pins, and no build reads a cargo home or NuGet's folder. The build right after the command is a warm one, which looks at nothing outside the checkout again, so each flow ends with a build on a second machine: a copy of the checkout without `MODULE.bazel.lock` and with an empty output base, whose Bazel has the patched cargo home or NuGet folder in its environment and evaluates the hub anew. It prints the unpatched marker too. Meanwhile `scan --mode agent --vex`, `apply --vex`, `vex` and `vex --no-verify` each write an OpenVEX document whose one statement says the package is `not_affected`, "Patched via Socket patch <uuid>". That document is false for what Bazel builds. The flows assert it as it is today and mark it KNOWN GAP, with what a fix changes in the test. The copy in the cargo home is the one the hub's own cargo unpacked there where the ruleset runs that cargo with the home of the environment (rules_rust 0.56.0), and is laid out by the test where it does not (0.75.0); NuGet's folder is laid out as the hosted flow already does. No cargo and no dotnet runs. The Bazel commands of the NuGet flow get the two variables of the .NET home that `socket-patch` gets (`NUGET_PACKAGES`, `DOTNET_CLI_HOME`), so a build that read the folder would find it. What the flows share is in the harness: `Cell::patches_outside`, and `Cell::builds_anew` for the second machine, which removes the first workspace copy before it builds where the repository cache outlives that copy. The grant helpers of the two modules are split so that a patch record can carry the file contents agent mode patches from, and in the Cargo module the helper that names the hosts of a child takes its cargo home as well; the hosted and vendored flows call them as before and assert what they asserted. The two workflows say that their legs now run agent mode, and start on a pull request that changes the VEX code (`vex/` of the core, the `vex` command) or, for NuGet, agent mode's own file. Both flows pass on Bazel 9.3.2 (macOS arm64) with no change to the product: NuGet in 14 s, Cargo in 92 s with an empty repository cache. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…ilds the package from the lock Agent mode patches a crate in the cargo home (`$CARGO_HOME/registry/src`) and a NuGet package in NuGet's global packages folder. In a Bazel workspace neither copy is built: rules_rust's `crate.from_cargo(cargo_lockfile = ...)` and rules_csharp's `nuget.from_lock(packages_lock_json = ...)` have Bazel download what the project's lock pins. The flows of the commit before this one measured it on Bazel 9.3.2: `scan --mode agent` exits 0 with the patch applied, `bazel build` prints the upstream marker before and after, also on a second machine that evaluates the hub anew, and `scan --vex`, `apply --vex`, `vex` and `vex --no-verify` each attest the package as `not_affected`. That document is false for what Bazel builds. Cargo and NuGet join the table of hubs in `bazel/built.rs`, so what main does for npm, PyPI and RubyGems holds for them too. When a call that is a statement of its own names the project's own `Cargo.lock` or `packages.lock.json` in a plain string, and that lock lists the package at the patched version without the patch, the document says `affected` of the package, with an action statement that names the lock, the call and its line, and what to run. `vex --json` reports a `skipped` event with `vex_bazel_not_in_build` and `details.status: "affected"`, the envelope is `partialFailure` and the exit code is 0, as on main. `--no-bazel` / `--vex-no-bazel` / `SOCKET_VEX_NO_BAZEL` leaves Bazel out of it. What the table holds for the two: - rules_rust: the extension by both files that define it at 0.56.0 and 0.75.0 (the constant of `bazel/cargo.rs`), the tag `from_cargo`, the attribute `cargo_lockfile`. Of the lock, the crates of a registry that no `[replace]` swaps out: those are the ones Bazel downloads. A call with a cargo-bazel lock (`lockfile`) is passed over, because the hub renders its crates from that file until a repin. So is one without `cargo_lockfile`, which names no lock. - rules_csharp: the extension as `bazel/nuget.rs` names it, the tag `from_lock`, the attribute `packages_lock_json`. A `packages.lock.json` never names a patch (a hosted pin there is a hash), and rules_csharp 0.5.0 makes one hub of all the `nuget.from_lock` calls: the sources of the last call that has any serve every package, and the first lock that names a package pins it. So a project patched in agent mode can be built patched through the hosted pin of another project of the workspace (measured on Bazel 9.3.2 with two calls). The `nuget_config` of every call is therefore searched for the id of the patch together with the lock. Where one names it, or is not a plain label, or cannot be read, or a call passes `*` or `**`, nothing is said of the hub. The locks are still read one call at a time, and `feeds` and `local_package_dirs` are not read. The action statement names the modes that a flow of this repository builds patched on real Bazel under that hub, so a hub carries its own sentence: hosted mode with the attribute it needs of the call (`cargo_config`, `nuget_config`), and for a crate vendored mode as well. The sentence of the three hubs on main is the same bytes as before, and their calls are judged one at a time as before. Left as main has them: a hosted pin (the caller hands none over, and whether a hub takes one is for `bazel/cargo.rs` and `bazel/nuget.rs` to say), a vendored crate (rules_rust builds it), a vendored NuGet package (no test builds one under its hub), a project whose lock no call names, a workspace that is not read with certainty, and a project in no Bazel workspace. Tests: unit tests of the table (what a lock lists, which calls are passed over, the hub of several `nuget.from_lock` calls, a doubt on the hub by either file of the rules_rust extension, the sentence of each hub). Two tests without Bazel per ecosystem run the binary in a checkout of the fixture: one runs the scans the action statement names and holds the document after each to `not_affected`; the other holds `vex`, wherever the hub decides nothing and with `--no-bazel`, to what it gives in the same checkout without its `MODULE.bazel` (exit code, both streams, the document), through `Cell::keeps_its_statement` of the harness. For NuGet one of those hubs has a second call whose `nuget_config` names the patch. The two agent flows now expect `affected` from the run, from `apply`, from `vex` and from `vex --no-verify`, and `not_affected` from `vex --no-bazel`, while the build still prints the upstream marker, on the second machine too. No assertion of the hosted and vendored flows is edited. One existing unit test took a Cargo purl for a package of no hub; it takes a Go module now. Docs: the VEX cells of Cargo and NuGet in "Bazel workspaces: what is supported", the section "Bazel workspaces: VEX" and the `vex_bazel_not_in_build` row of the CLI contract. The two workflows start their legs on a change to `bazel/built.rs` and to the reader of the workspace files, each by its path. The size budget of `bazel/` follows the code (3337 to 3403 lines). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
main gained the gem_checksums withhold and the Maven scan of a pinned hub. Two conflicts: - docs/ecosystems.md, the table of "Bazel workspaces: what is supported": main's RubyGems row (its hosted cell names the gem_checksums conditions) with this branch's VEX cells for Cargo and NuGet. - scripts/loc-budget.json: the cap of the `bazel` group is the count of the merged tree, 3681. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…he Bazel VEX answer A NuGet project is every project file below the directory a command runs in, each with a `packages.lock.json` beside it: agent mode patches what any of them restored, and hosted mode and `vex` read a pin in each of their locks. The hub table took only the lock in the run's own directory for the project's, as it does for the other ecosystems. So for a run at the root of a solution, where `nuget.from_lock` names `//src/App:packages.lock.json`, the document kept `not_affected` for a package that Bazel downloads unpatched. The NuGet hub now takes a lock at or below the directory of the run. The other hubs are as they were: a lock below the run is still the lock of another project there. Three smaller changes to the same function, none of which changes what is said of a workspace that Bazel can load: - Whether every `nuget_config` of the hub is a plain label was a comparison of two counts: the labels that resolve, and the calls that give the attribute. A dict counts once as given and once per key as resolved, so the counts could be equal while a config was not read. Each call is now asked on its own: no `nuget_config`, or one string that names a file of the main repository. - The attributes with which a call can take its packages from a file that is not read (`uv_lock`, `lockfile`) were one list for every hub. Each hub now has its own. - The configs of a rules_csharp hub were read again for every lock. They are read once for the hub, when the first lock asks. The docs and the contract row say which lock is the project's own for NuGet, and the list of statements that stay wrong names the member of a Cargo workspace, whose `Cargo.lock` lies above it. The `bazel` line budget is the new count, 3704. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
PR SummaryMedium Risk Overview
E2E: new real-Bazel agent flows for Cargo and NuGet, plus harness helpers ( Reviewed by Cursor Bugbot for commit 5a5cdc4. Configure here. |
…gnore line `cargo::vendored_from_cargo_with_config` comments out the line the scan wrote in `.bazelignore` and expects `bazel cquery //...` and `bazel query 'deps(//...)'` to fail on a repository that the main repository does not see. It looked for the hub's repository (`@crates__...`) alone. The build file that cargo-bazel writes into the committed copy names `@platforms` as well, and Bazel reports the first one it meets: on a Linux runner with Bazel 9.3.2 the query named `@platforms` and the flow failed on its own check. The flow now takes either name for that failure. Test only: nothing the command does changes. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
socket-patch vexand--vexnow attest a Cargo crate or a NuGet package asaffected, not as patched, when its patch is only in the copy a package manager installed and a rules_rust or rules_csharp hub of the Bazel workspace builds the package from the project's lock.Based on main, with main merged in. Agent mode is not supported for a project that Bazel builds, and nothing here changes that: this change only makes the VEX document tell the truth about a patch that was applied that way. Support for projects that Bazel builds is being marked experimental as a whole in #2222.
Decisions for the maintainer
crate.from_cargocall has to name the project's cargo config (cargo_config), or vendored mode. For a NuGet package: hosted mode, with the note that thenuget.from_lockcall has to name the project'snuget.config(nuget_config). These are the modes that a test of this repository builds patched on real Bazel under each hub, so vendored mode is not named for NuGet. Say if the wording should differ.nuget.from_lockcalls is read as one hub for its configs. rules_csharp makes one hub of all the calls: the sources of the last call that has any serve every package, and the first lock that names a package decides its version and hash. So a project patched in agent mode can be built patched through the hosted pin of another project of the same workspace, andaffectedwould be false there (measured on Bazel 9.3.2: the build prints the patched marker). Thenuget_configof every call is therefore searched for the patch. The document keeps main'snot_affectedstatement when one of them names the patch, is not a plain label or cannot be read, or when a call passes*or**. The locks are still read one call at a time, andfeedsandlocal_package_dirsare not read. Reading which lock pins the package, the way hosted mode does, would let the document sayaffectedin more of those workspaces. It is more code and I left it out. Say if you want it.bazel/built.rsor the reader of the workspace files, each named by its path. They stay not required for merge. Each agent flow ends with a build on a second machine, which adds about 30 to 60 s to a Cargo leg and about 6 s to a NuGet leg (macOS arm64).packages.lock.jsonbeside it, and agent mode, hosted mode andvexalready read a solution that way. So a run at the root of a solution saysaffectedwhennuget.from_locknames a lock below it. This came out of the review of this change. Say if such a run should keep main's statement instead.What the code does today
Agent mode patches a crate in the cargo home (
$CARGO_HOME/registry/src) and a NuGet package in NuGet's global packages folder. In a Bazel workspace neither copy is built:crate.from_cargo(cargo_lockfile = ...)andnuget.from_lock(packages_lock_json = ...)have Bazel download what the project's lock pins.The first commit records this on real Bazel, with no product change:
scan --mode agent --yesexits 0 with the patch applied.bazel buildandbazel run //:consumerprint the upstream marker before the command and after it.MODULE.bazel.lockand with an empty output base, so its Bazel evaluates the hub anew, and it has the patched cargo home or NuGet folder in its environment. The build right after the command is a warm one and looks at nothing outside the checkout again.scan --mode agent --vex,apply --vex,vexandvex --no-verifyeach write onenot_affectedstatement.That document is false for what Bazel builds. For npm, PyPI and RubyGems main already says
affectedin this situation.What changes
Cargo and NuGet join the table of hubs in
crates/socket-patch-core/src/bazel/built.rs. When a call that is a statement of its own names the project's ownCargo.lockorpackages.lock.jsonin a plain string, and that lock lists the package at the patched version without the patch, the document saysaffected. For NuGet the project's own lock is also one below the directory of the run (decision 5). The action statement names the lock, the call and its line, and what to run:The event (
vex_bazel_not_in_build,details.status: "affected"), thepartialFailureenvelope, the exit code 0 and the--no-bazelswitch are main's and are not changed.What the table holds for the two rulesets:
from_cargo, the attributecargo_lockfile. From the lock it takes the crates of a registry that no[replace]swaps out, which are the ones Bazel downloads.bazel/nuget.rsalready names it, the tagfrom_lock, the attributepackages_lock_json. Apackages.lock.jsonnever names a patch (a hosted pin there is a hash), so thenuget_configof everynuget.from_lockcall is searched for the id of the patch as well (decision 2). Each call has to give it as one plain label of the main repository, or not at all.The sentence that says what to run moved from
commands/vex.rsinto the table, one per hub. For npm, PyPI and RubyGems it is the same bytes as before, and their calls are judged one at a time as before.Limits
affectedonly where the committed files show it. It keeps main'snot_affectedstatement for:crate.from_cargowith a cargo-bazel lock (lockfile) or withoutcargo_lockfile;nuget_configof anynuget.from_lockcall names the patch, is not a plain label or cannot be read, or in which such a call passes*or**;*or**;WORKSPACEfile that defines repositories, or a workspace withoutMODULE.bazel.Cargo.lock. A member of a Cargo workspace whose lock lies above it keeps its statement (see "Code review").Contents
Two commits on
52bbeaa95, a merge of main (fab1270fd) and one commit of review fixes: 11 files, +1287 / -192 against main. The merge had two conflicts: the RubyGems row of the support table is main's and the VEX cells of Cargo and NuGet are this change's; thebazelline budget is the count of the merged tree.Cell::patches_outsideandCell::builds_anewin the harness, and the two workflows.tests/e2e_bazel_build/cargo.rs+176 / -30,nuget.rs+180 / -23,harness.rs+58 / -0bazel-cargo.yml+25 / -18,bazel-nuget.yml+36 / -25bazel/built.rs+422 / -43: +144 / -35 outside its tests (251 to 317 code lines), +278 / -8 unit testscommands/vex.rs+14 / -13, no more code linesbazel/cargo.rs+1 / -1 (one constant becomes visible to the table)tests/e2e_bazel_build/cargo.rs+122 / -28,nuget.rs+123 / -23,harness.rs+38 / -0 (Cell::keeps_its_statement, the body the two tests without Bazel share)docs/ecosystems.md+64 / -24,CLI_CONTRACT.mdone rowbazel-cargo.yml+18 / -5,bazel-nuget.yml+9 / -5scripts/loc-budget.json:bazel/3615 (main) to 3681 after the mergebazel/built.rs+74 / -32: +67 / -31 outside its tests, +7 / -1 unit testsdocs/ecosystems.md+10 / -5,CLI_CONTRACT.mdone row, one comment intests/e2e_bazel_build/nuget.rsscripts/loc-budget.json:bazel/3681 to 3704One existing unit test changed:
the_workspace_of_a_project_on_disk_is_readtook a Cargo purl as its example of a package with no hub. Cargo has a hub now, so it takes a Go module. No assertion of the hosted and vendored flows of either ecosystem is edited.What it proves, and where
Real Bazel 9.3.2, 8.8.2 and 7.7.2 on macOS arm64, run the way the workflow's run step runs it. Cargo with rules_rust 0.75.0 (Bazel 9 and 8) and 0.56.0 (Bazel 7), NuGet with rules_csharp 0.5.0.
cargo::agent_from_cargo_with_configcargo::hosted_from_cargo_with_configcargo::hosted_from_cargo_without_configcargo::vendored_from_cargo_with_confignuget::agent_from_locknuget::hosted_from_locknuget::hosted_no_configA Cargo leg takes 170 to 196 s and a NuGet leg 18 to 20 s.
In the two agent flows the build prints the upstream marker before and after
scan --mode agent, and on the second machine. The run,apply,vexandvex --no-verifyeach write oneaffectedstatement, andvex --no-bazelwrites onenot_affectedstatement. On Bazel 7.7.2 the patched crate is the copy the hub's own cargo unpacked in the cargo home.Other checks:
not_affectedexpectation (NuGet in 14 s, Cargo in 92 s with an empty repository cache), and the tests without Bazel pass.nuget.from_lockcalls on Bazel 9.3.2 (a run that is not a committed test): two projects under one root, the hub readsbwith its lock and config, thenawith its lock. Afterscan --mode hostedinbthe build prints the patched marker. Afterscan --mode agentina,vexinaexits 0 with onenot_affectedstatement, and the build still prints the patched marker. A unit test and a test without Bazel hold the same state.--no-bazelbeside the hub, under hubs that decide nothing (one over another directory's files, a cargo-bazel lock, a config that is not written out, a second NuGet call whose config names the patch, is computed or is passed with**), under aWORKSPACEfile alone, and without a Bazel workspace, for Cargo and for NuGet. The exit code, both streams with and without--json, and the document without its id and times were equal every time.*/**check, only the first config searched, the doubt check of the two extension files, the*/**check applied to every hub), and each fails a test. Thirteen others were tried before the last change to that function.cli,cli_parse_vex,e2e_bazel_build,e2e_bazel_maven_beside,e2e_embedded_vex,e2e_vex,e2e_vex_lockfile,e2e_vex_redirect,e2e_vex_vendor,spawn_env_hygiene,vex) pass. So do both clippy runs,cargo check --all-targets --all-features, the script tests, actionlint and the tool-pin check on the two workflows.After the merge of main (the tree the pull request shows):
cargo checkon all targets, both clippy runs, the Bazel and VEX unit tests (1,080 in core, 51 in the CLI), the hermetic VEX test targets named above ande2e_bazel_buildwithout Bazel pass; on real Bazel 9.3.2,nuget::passes 3 of 3 andcargo::4 of 4. Bazel 8.8.2 and 7.7.2 were not run again after the merge; the CI legs run them.After the review fixes (the head):
cargo checkon all targets, both clippy runs, the Bazel and VEX unit tests (1,080 in core, 51 in the CLI), the test targetscli(118),e2e_bazel_buildwithout Bazel (69, 30 ignored),e2e_bazel_maven_beside(24),e2e_vex(28) andvex(29), the script tests (423) and the line budget pass. On real Bazel 9.3.2nuget::passes 3 of 3 (20 s).cargo::was not run on real Bazel again after the fixes: the machine was at its free-disk limit, and a Cargo leg takes about 3.6 GB at its peak. The tests without Bazel hold the Cargo documents on the head, and the CI legs run all three releases.Not covered
not_affected. The VEX section lists it.feedsorlocal_package_dirs, andnuget.from_lockcalls of another Bazel module of the build. There the document can sayaffectedand name this project's lock while the hub builds something else.not_affectedthere, as on main.crate.from_specs, through acrates_repositoryof aWORKSPACEfile, or from a hub with a cargo-bazel lock.Code review
A review of the diff against main gave eight findings. Each was checked against the code.
Fixed in the last commit:
nuget.from_lock(packages_lock_json = "//src/App:packages.lock.json")with a run at the root of the solution keptnot_affected, although agent mode, hosted mode andvextreat every lock below the run as the project's. The NuGet hub now takes a lock at or below the directory of the run (decision 5). A unit test holds the root of the solution, a directory beside the lock, one below it and one whose name only starts the same.nuget_configis a plain label was a comparison of two counts: the labels that resolve and the calls that give the attribute. A dict counts once as given and once per key as resolved, so the counts could be equal while a config was not read. Each call is now asked on its own: nonuget_config, or one string that names a file of the main repository. rules_csharp takes no dict there, so no workspace that Bazel loads changes. A unit test holds a call with a one-key dict, which the counts let through.uv_lock,lockfile) were one list for every hub. Each hub has its own now. No ruleset here has the other's attribute, so nothing changes for a workspace that Bazel loads, and no test was added.Without the first two fixes their assertions fail (tried on the final tree).
Not fixed:
not_affected, because theCargo.locklies above it. ACargo.lockdoes not say which directories its workspace has, so taking it for the lock of a member means reading the workspace's manifests (the member globs,exclude, a member with a lock of its own). That is a change of its own. Hosted and vendored mode refuse to run in a member and name the workspace root, where the document is right. "Limits" names the case, and the list of statements that stay wrong indocs/ecosystems.mdnames it now too.lockfile) is passed over while the Cargo cell of the table says "Supported". Such a hub builds what that file names until a repin, and nothing holds it toCargo.lock, so sayingaffectedthere takes reading that file. The legend of "Supported" for VEX sends the reader to the VEX section for the limits, and that section names this one, as it namesnpm_package_lock,yarn_lockanduv_lockfor the hubs whose cell says the same.🤖 Generated with Claude Code