Skip to content

Commit 83d2b95

Browse files
Test NuGet hosted mode under rules_csharp on real Bazel (#2140)
socket-patch has no Bazel-specific code for NuGet, and no test ran its NuGet rewrite in a Bazel workspace. This adds the NuGet module of the real-Bazel tests, its fixture and its workflow, and makes one edit to the shared harness: NETRC is removed from every Bazel child (below). No file under crates/*/src changes. The flow holds hosted mode for a package that rules_csharp 0.5.0 reads through `nuget.from_lock(packages_lock_json, nuget_config)` under Bzlmod, on Bazel 9.3.2, 8.8.2 and 7.7.2. One test, `nuget::hosted_from_lock`, in a fresh output base: - builds the workspace and reads the unpatched marker from `bazel run //:consumer`; the upstream package came from the stand-in; - runs `scan --mode hosted --yes`, which rewrites nuget.config (the Socket source of the patch, ahead of nuget.org) and packages.lock.json (the contentHash of the patched package), and nothing else; - runs the same two Bazel commands and reads the patched marker, which holds an id made for the run: the package came from the stand-in patch host, and Bazel rewrote MODULE.bazel.lock and no other file; - runs `rollback --yes`: the two files are the ones of before at once, and after the next build so is every file of the checkout, and the build prints the unpatched marker again; - on a second machine, where the patch host serves other bytes than the grant pins, the build fails in both states of the checkout: over the contentHash of packages.lock.json while Bazel's lock is stale, and over the sha256 in MODULE.bazel.lock once that lock pins the granted package. There Bazel also refuses the copy on nuget.org, which the hub tries next. Three outcomes the flow asserts are gaps of the product, not its contract. They are asserted as the release has them, so that the measurement does not drift; the module docs name them under one heading, and each is marked KNOWN GAP where it is asserted, with what a fix would change in the test: - Under Bazel alone socket-patch does not find NuGet packages from packages.lock.json. `scan` finds a NuGet package only where a restore unpacked it: a folder that `obj/project.assets.json` records, the `packages/` folder of a packages.config project, or NuGet's global packages folder. In a checkout without a restore record, on a machine whose global packages folder does not hold the package (a runner that only ever ran Bazel), `scan --mode hosted --yes` exits 0, finds no package and writes no file: hosted mode does nothing. The flow asserts that case. Its patched path exists only because the test then stages the global folder by hand, as a restore leaves it; no dotnet runs. - `vex` on the machine that ran the scan exits 1 and writes no document (`not_applied`) while Bazel builds the patched package: it reads the unpatched copy in NuGet's folder. Where the folder holds nothing, `vex` attests the package from the pin in the two files. - rules_csharp asks the Socket source for every package of the lock. For the package the source does not hold, the stand-in answers 404, and Bazel prints a warning with the URL it asked, which holds the access token of the grant. The package then comes from nuget.org as before. What the flow asserts beside that, because a user meets it: - The scan warns `redirect_nuget_stale_global_package`: the folder still holds the unpatched copy. That copy is what `dotnet restore` would go on using; no Bazel build reads it. - The scan leaves MODULE.bazel.lock stale, and so does the rollback: a `--lockfile_mode=error` build fails ("MODULE.bazel.lock is no longer up-to-date") until a build that may update the lock has run. - A second `scan` reports the patch applied again and rewrites no file. The names of the two fixture packages end in an id made for the run. nuget.org has no scopes, so an id that is committed here could be pushed there by anyone; an id that is new for every run cannot. The committed project, BUILD file and lock hold `@RUN@` where a checkout holds that id, and NuGet takes no `@` in a package id, so those files name no package that could exist either: a hermetic test reads every id the three files name and holds it to that. The downloader config of the test sends every URL of api.nuget.org and of patch.socket.dev to the loopback stand-in and blocks both hosts, so no build asks either. The lock of a checkout pins bytes that the run made, so no cache holds a package under test. The consumer is a genrule over the assembly that the hub's label (`@nuget//:<package>`, what a C# rule names in deps) hands out. The fixture's assembly is a line of text, no C# rule is built, and no .NET SDK is downloaded. With an empty repository cache a run fetched 0.7 MB on Bazel 9.3.2, 0.4 MB on 7.7.2 and 13.0 MB on 8.8.2, from bcr.bazel.build, github.com and release-assets.githubusercontent.com (macOS arm64). The rules_csharp archive is 142 kB of that; Bazel 8.8.2 fetches protobuf 29.0, rules_python, rules_java and rules_cc for its own modules, as it does in a workspace without rules_csharp. The fixture has one era, for Bazel 7.1.0 and later: the hub of rules_csharp 0.5.0 downloads with `block = False`, which Bazel has from 7.1.0. Only the three releases above ran. The harness edit. Bazel's `http_archive` and its downloader read the credentials file that NETRC names, and rules_csharp reads it for every package it downloads; the harness passed the variable of the caller's shell on to every Bazel child. With NETRC naming a file that does not exist, the NuGet flow failed at its first fetch, in Bazel's own `http_archive` (FileNotFoundException). With a file that holds a login for 127.0.0.1, the stand-in received that login with each package download; a login for api.nuget.org in the same file did not reach it. NETRC now joins the variables removed from every Bazel child, for every ecosystem's flow, and the hermetic test that guards that list holds the name. - crates/socket-patch-cli/tests/e2e_bazel_build/nuget.rs: the packages, the stand-in (the Socket API, nuget.org's flat container, the Socket source of a patch), the staged packages folder and the flow; two hermetic tests hold the fixture, the downloader config and the path lists of the workflow. - crates/socket-patch-cli/tests/e2e_bazel_build/harness.rs: NETRC, in the list of what every Bazel child loses and in the list of the test that guards it. - crates/socket-patch-cli/tests/fixtures/bazel/rules_csharp/bzlmod: the workspace, with a placeholder where a checkout holds the id of its run and the contentHash of its packages. - .github/workflows/bazel-nuget.yml: three Linux legs on a pull request that touches these files or a NuGet file the flow runs through, on a main push that also touches the shared engine code, nightly and on dispatch; one macOS leg nightly and on dispatch. Not required for merge. Not covered: what the real patch host answers for a package it does not hold (the stand-in answers 404), signed packages, a real `dotnet restore`, a C# compile, vendored and agent mode, rules_dotnet, and a hub without a nuget.config. Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 3f92b8b commit 83d2b95

9 files changed

Lines changed: 1156 additions & 0 deletions

File tree

‎.github/workflows/bazel-nuget.yml‎

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
name: Bazel patch compatibility (NuGet)
2+
3+
# The NuGet flow of the real-Bazel tests (`nuget::` in `e2e_bazel_build`):
4+
# hosted mode for a package that rules_csharp's `nuget.from_lock` reads from
5+
# `packages.lock.json` and `nuget.config`, on each Bazel release below.
6+
# bazel-compatibility.yml holds the legs and what every ecosystem shares;
7+
# this file says when the NuGet legs run and on which releases.
8+
#
9+
# A NuGet leg downloads no .NET SDK and needs none on the runner: the
10+
# fixture's consumer is a `genrule` over the assembly that the hub hands to
11+
# a C# rule, and no C# rule is built. Beyond what every leg needs, a leg
12+
# downloads the rules_csharp archive (about 140 kB) and nothing else, so
13+
# its repository cache holds little more than what Bazel resolves for
14+
# itself: about 1 MB on Bazel 9, half of that on Bazel 7, and about 13 MB
15+
# on Bazel 8, which fetches protobuf for its own modules, as it does in a
16+
# workspace without rules_csharp. No leg asks nuget.org: the test's
17+
# downloader config sends every URL of that host to the loopback stand-in
18+
# and blocks the host. A package under test never comes from the
19+
# repository cache: the test builds the packages of a run with an id of
20+
# that run in their names and in their bytes, so no earlier run saved a
21+
# file with their checksums.
22+
#
23+
# Not required for merge: no `merge_group` trigger, and no job here is a
24+
# `needs` of ci.yml's `ci-ok`.
25+
26+
on:
27+
pull_request:
28+
types: [opened, synchronize, reopened, ready_for_review]
29+
# What a pull request run covers: the NuGet flow and its fixture, the
30+
# harness and the workflows that run it, the Bazel installer and its
31+
# pins, and the NuGet files of the product that `scan --mode hosted`,
32+
# `vex` and `rollback` run through in the fixture, each by its own
33+
# path: the readers of `packages.lock.json` and `nuget.config` and the
34+
# `contentHash` of a package (formats/nuget), the NuGet crawler, which
35+
# finds a package in the global packages folder, the reader of the
36+
# NuGet settings a project inherits (vendor/nuget_config.rs), the
37+
# discovery of a hosted pin in the two files, the upstream restore of
38+
# `rollback`, and the hosted scan's check of that folder.
39+
# vendor/nuget_feed.rs is the vendored backend; it is here for the
40+
# version and folder helpers that every one of those files calls.
41+
# Nothing another ecosystem's flow owns is here, so a change to one
42+
# ecosystem starts that ecosystem's legs only.
43+
#
44+
# What it does not cover: shared engine code. The hosted rewrite itself
45+
# is there (`rewrite_nuget` in patch/redirect/mod.rs, the file of every
46+
# ecosystem's rewriter), and so are patch/redirect/grant.rs,
47+
# hosted/engine.rs, commands/scan/hosted.rs and the rest of vex/, the
48+
# shared test helpers and the toolchain files. A change to one of those
49+
# gets its run on the push to main (when the file is in the list below)
50+
# or in the nightly run, not on the pull request: that is the price of
51+
# not running every ecosystem's legs on every shared change. The flow
52+
# does not run agent mode (patch/sidecars/nuget.rs), which is in neither
53+
# list. On demand, workflow_dispatch runs the legs on any branch.
54+
paths:
55+
- '.github/workflows/bazel-nuget.yml'
56+
- '.github/workflows/bazel-compatibility.yml'
57+
- 'scripts/install-bazel.sh'
58+
- 'scripts/bazel-pins.txt'
59+
- 'crates/socket-patch-cli/tests/e2e_bazel_build/nuget.rs'
60+
- 'crates/socket-patch-cli/tests/e2e_bazel_build/harness.rs'
61+
- 'crates/socket-patch-cli/tests/fixtures/bazel/rules_csharp/**'
62+
- 'crates/socket-patch-core/src/formats/nuget/**'
63+
- 'crates/socket-patch-core/src/crawlers/nuget_crawler.rs'
64+
- 'crates/socket-patch-core/src/vendor/nuget_config.rs'
65+
- 'crates/socket-patch-core/src/vendor/nuget_feed.rs'
66+
- 'crates/socket-patch-core/src/vex/discover/nuget.rs'
67+
- 'crates/socket-patch-core/src/patch/redirect/upstream/nuget.rs'
68+
- 'crates/socket-patch-cli/src/commands/scan/hosted/nuget.rs'
69+
push:
70+
branches: [main]
71+
# The pull request paths, and what only a push to main covers: the
72+
# root of the test target (it changes when an ecosystem's module is
73+
# added; ci.yml builds the target and runs its hermetic tests on every
74+
# pull request), the shared engine code and test helpers the flow runs
75+
# through, `vex`, whose verdicts the flow asserts, and the toolchain
76+
# files. A main push outside this list waits for the nightly run below.
77+
paths:
78+
- '.github/workflows/bazel-nuget.yml'
79+
- '.github/workflows/bazel-compatibility.yml'
80+
- 'scripts/install-bazel.sh'
81+
- 'scripts/bazel-pins.txt'
82+
- 'crates/socket-patch-cli/tests/e2e_bazel_build/nuget.rs'
83+
- 'crates/socket-patch-cli/tests/e2e_bazel_build/harness.rs'
84+
- 'crates/socket-patch-cli/tests/fixtures/bazel/rules_csharp/**'
85+
- 'crates/socket-patch-core/src/formats/nuget/**'
86+
- 'crates/socket-patch-core/src/crawlers/nuget_crawler.rs'
87+
- 'crates/socket-patch-core/src/vendor/nuget_config.rs'
88+
- 'crates/socket-patch-core/src/vendor/nuget_feed.rs'
89+
- 'crates/socket-patch-core/src/vex/discover/nuget.rs'
90+
- 'crates/socket-patch-core/src/patch/redirect/upstream/nuget.rs'
91+
- 'crates/socket-patch-cli/src/commands/scan/hosted/nuget.rs'
92+
- 'crates/socket-patch-cli/tests/e2e_bazel_build/main.rs'
93+
- 'crates/socket-patch-core/src/patch/redirect/**'
94+
- 'crates/socket-patch-core/src/hosted/**'
95+
- 'crates/socket-patch-core/src/vex/**'
96+
- 'crates/socket-patch-cli/src/commands/**'
97+
- 'crates/socket-patch-cli/tests/common/**'
98+
- 'crates/socket-patch-cli/tests/hosted_memory_common/**'
99+
- 'Cargo.lock'
100+
- 'Cargo.toml'
101+
- 'crates/*/Cargo.toml'
102+
- 'rust-toolchain.toml'
103+
- '.cargo/**'
104+
schedule:
105+
# Nightly, so a main change outside the push list still gets a run
106+
# within a day. The macOS leg runs here and on demand only.
107+
- cron: '7 5 * * *'
108+
workflow_dispatch:
109+
110+
permissions:
111+
contents: read
112+
113+
# The nightly run has a group of its own. A group holds one pending run, so
114+
# in the group of main's pushes a second push would replace a nightly that
115+
# waits behind the first, and the nightly is the only run with the macOS leg.
116+
concurrency:
117+
group: bazel-nuget-${{ github.event.pull_request.number || github.ref }}${{ github.event_name == 'schedule' && '-nightly' || '' }}
118+
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
119+
120+
jobs:
121+
nuget:
122+
uses: ./.github/workflows/bazel-compatibility.yml
123+
with:
124+
eco: nuget
125+
fixtures: rules_csharp/bzlmod
126+
# One leg per line: a Bazel release, and the era of the fixture that
127+
# `nuget::FIXTURE` gives it.
128+
releases: >-
129+
[
130+
{"bazel": "9.3.2", "era": "e9"},
131+
{"bazel": "8.8.2", "era": "e9"},
132+
{"bazel": "7.7.2", "era": "e9"}
133+
]
134+
macos: >-
135+
[
136+
{"bazel": "9.3.2", "era": "e9"}
137+
]

‎crates/socket-patch-cli/tests/e2e_bazel_build/harness.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ const REMOVED: &[&str] = &[
4545
// Bazel: `TEST_TMPDIR` puts a server in test mode, where a failing
4646
// module extension on 7.x is a crash (exit 37) instead of an error.
4747
"TEST_TMPDIR",
48+
// Bazel's `http_archive` and downloader, and rules_csharp for every
49+
// package: the credentials file of the shell. A path that names no
50+
// file fails the first fetch, and a file's login for loopback, or its
51+
// `default` one, goes to a stand-in with every download sent there.
52+
"NETRC",
4853
// Any tool: an `XDG_*_HOME` directory would move what it keeps out of
4954
// the scratch home: a cache, or on Linux the telemetry counters of a Go
5055
// SDK command that a ruleset starts, which Go puts under
@@ -1051,6 +1056,7 @@ fn settings_of_a_shell() -> Vec<String> {
10511056
let names = [
10521057
// `REMOVED`.
10531058
"TEST_TMPDIR",
1059+
"NETRC",
10541060
"XDG_CACHE_HOME",
10551061
"XDG_CONFIG_HOME",
10561062
"XDG_DATA_HOME",

‎crates/socket-patch-cli/tests/e2e_bazel_build/main.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@ mod cargo;
5151
mod go;
5252
mod harness;
5353
mod npm;
54+
mod nuget;
5455
mod pypi;
5556
mod ruby;

0 commit comments

Comments
 (0)