Skip to content

Commit 85bbd4a

Browse files
Withhold a hosted NuGet pin that a rules_csharp hub would not take (#2189)
* Test hosted NuGet mode on a rules_csharp tag without nuget_config, on real Bazel rules_csharp's `nuget.from_lock` downloads each package of `packages.lock.json` and holds it to the lock's `contentHash`. It asks the package sources of the `nuget.config` that the tag names with `nuget_config`; a tag without that attribute, as in the examples of the ruleset's own repository, asks nuget.org alone. `scan --mode hosted` re-pins the `contentHash` of a patched package in the lock and names the Socket source of the patch in `nuget.config`. The one flow on real Bazel so far (`nuget::hosted_from_lock`) has the tag name that file, and there the next build is patched. This adds `nuget::hosted_no_config`: the same fixture with the `nuget_config` line taken out of the tag and no `nuget.config` in the checkout. It records what the code does today, on Bazel 9.3.2: - `scan --mode hosted --yes` exits 0 and reports the patch applied. It creates `nuget.config`, which nothing of the build reads, and re-pins the lock, which the hub reads. - The next `bazel build` fails. The hub fetches the upstream package from nuget.org, never asks the Socket source, and refuses the package: `the downloaded package does not match its contentHash in packages.lock.json`. The test asserts that failure by its message, so that the commit that fixes it has to change the expectation. The fixture's files are unchanged: the flow edits its own checkout, and the hermetic test of the fixture holds that each era's MODULE.bazel has the one line the flow takes out. Like the other NuGet flow it asks no public registry: Bazel's downloader config sends nuget.org to the loopback stand-in. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * Withhold a hosted NuGet pin that a rules_csharp hub would not take rules_csharp's `nuget.from_lock` downloads each package of `packages.lock.json` and fails the build on a file that does not match the lock's `contentHash`. All the `nuget.from_lock` tags of a workspace make one hub (`_nuget_impl` in csharp/extensions.bzl at 0.5.0): - It has one list of package sources, which it asks before nuget.org. Each tag in turn puts its `feeds` in the place of that list, and then the sources of the `nuget.config` it names with `nuget_config`. So the last tag that has either decides. - It holds a package to the `contentHash` of the first lock that names it. Hosted mode re-pins the `contentHash` of a patched package in the project's locks and names the Socket source of the patch in the project's `nuget.config`. In each of these workspaces `scan --mode hosted` exited 0 with the patch applied, and the next `bazel build` failed with "the downloaded package does not match its contentHash in packages.lock.json" (Bazel 9.3.2): - A tag that names no `nuget_config`, as in the examples of the ruleset's own repository, with or without `feeds` or `local_package_dirs`: the hub reads the re-pinned lock and not the config, and downloads the upstream package. The scan had created a `nuget.config` that nothing of the build read. This is the flow of the previous commit. - Two tags whose locks both hold the package, both naming the config of the second project, the scan run in that project: the hub downloads the patched package and holds it to the lock of the first project, which the scan did not re-pin. What changes Hosted mode reads the `nuget.from_lock` tags of the Bazel workspace the project is in (`MODULE.bazel` and the files it includes, through the reader in `bazel/workspace.rs`), and the locks they name. When a tag reads a lock of the project, it writes nothing for a NuGet package that the hub would not take: no `nuget.config`, no re-pinned lock entry. The package is a `skipped` / `redirect_unconfirmed` event, the run exits 0 like any other package that hosted mode does not pin, and one warning per package names the tag by its file and line and the way on. `redirect_bazel_nuget_config_undeclared`: the tags do not show that the hub takes its sources from the project's config. Warning: newtonsoft.json@13.0.3: MODULE.bazel:9: this `nuget.from_lock` names no `nuget_config`, so rules_csharp does not read nuget.config, where hosted mode names the Socket source of a patch; without that source rules_csharp downloads the upstream package, and the Bazel build fails on the `contentHash` that hosted mode re-pins in the lock. So hosted mode wrote nothing for it. Put `nuget_config = "//:nuget.config"` on that tag and run this command again: it then names the source in nuget.config, which it creates where there is none, and the Bazel build takes the patched package With that attribute on the tag and no other step, the same command creates the file, re-pins the lock, and the next build takes the patched package from the Socket source. The file needs no `exports_files` entry: the hub reads it by its label. `nuget::hosted_no_config` now asserts both halves on Bazel 9.3.2, 8.8.2 and 7.7.2 where it asserted the failed build: nothing written and the build still passing on the upstream package, then the attribute, the same scan and the patched build. The warning has another way on where the attribute alone would not do: - The tag that decides names another file. Where that is a NuGet config in a directory above the project, as with one `nuget.config` for several projects: run the command in that directory (`--cwd`), which writes the file the tag reads. Re-pointing the tag at a config of the project would take the other sources of that file from the hub, and failed a build that needed one of them. Otherwise: the attribute, and the sources of the other file to name in the project's config. - The tag that decides has `feeds`: the attribute, and those feeds to name in the config, whose sources stand in the place of `feeds`. - A tag is not a call of its own, passes `**`, or has a `nuget_config` that is no plain string: write it out. Nothing is evaluated, so which file the hub reads cannot be told, and a build that may break outranks a patch that is missed and explained. - A tag has `local_package_dirs`: the warning adds that the hub asks those directories before every source, so the upstream package must not lie in one. `redirect_bazel_nuget_lock_not_repinned`: another tag reads a lock that the run does not re-pin, and that lock holds the package at the patched version with another `contentHash`, or cannot be read. Where the tags are in one file, only a lock that the hub reads before the project's own stands against the pin; in more than one file their order is not known. The way on: run the command in a directory above both locks that holds a solution file or a `nuget.config`, where it re-pins the lock of every project. This is checked first, also where the hub does not read the config yet, so that the attribute of the other warning cannot lead to that failed build. Each way on was followed on Bazel 9.3.2 and ended in the patched build: the attribute with the feed named in the config; a config spelled `NuGet.Config`; the run in the directory of a root config that holds a private source; and, for two locks, the run at the root beside a solution file, then the attribute its warning names. When it writes and when it does not - Nothing changes for a project in no Bazel workspace, for one that restores into no lock file, and for one whose locks no `nuget.from_lock` call names. A tag whose `packages_lock_json` is not a label in a plain string may name any lock and counts as naming the project's. - Nothing changes for a package that no lock of the project which a tag reads holds: its pin changes no file the hub builds it from. - Hosted mode writes when every `nuget.from_lock` is a call of its own and the last one that has a `nuget_config` or `feeds` names the project's config as a label in a plain string. So two tags that name two configs, the project's last, are pinned as before and build patched. Where the tags are in more than one file, every tag that has either must name the config. - A part of `MODULE.bazel` that the reader cannot read, such as a `bazel_dep` whose `repo_name` is an expression, can hide a tag and changes nothing else: where the tags that are read show that the hub reads the config, hosted mode writes, and that build is patched. The config is the one the rewriter writes, named as the project directory spells it: a disk that folds case answers to every spelling, and the label has to name the file as a disk that does not would. A label that differs from it only in case counts as the same file, unless the directory holds both spellings. The label in the warning is in the package of the lock's own label. How `bazel/nuget.rs` reads what the hub makes of the project: why it does not read the config, if so, and the lock of each tag in the order of the tags. The disk engine asks it where it lists the locks a NuGet rewrite would re-pin, and hands the answer to the rewriter under a synthetic key, as it hands NuGet's inherited sources. `rewrite_nuget` asks it about each package it would otherwise pin, with the package's version and patched `contentHash`, and writes nothing for one the hub would not take. A package it refuses for another reason keeps that reason's code. The in-memory engine is unchanged: it finds NuGet packages in no installed tree, raises `ecosystem_unsupported_in_memory` for their files and never reaches the rewriter. Not covered - A tag of another Bazel module, whose sources and locks the hub takes as well; a lock in another repository; a tag that an unreadable part of `MODULE.bazel` hides; and what the command line tells Bazel. - What a directory of `local_package_dirs` holds. With the upstream package in one and a tag that names the config, the scan pins and the build fails as it did (Bazel 9.3.2): the directories are named by paths outside the checkout and are not opened. - Three shapes that are as they were, and by the ruleset's source fail the same way (none was built): a Bazel workspace below the directory of the run, whose tags are not read while the locks of its projects are re-pinned with the others; a project below the root of a workspace that is in no git checkout, because the workspace is looked for only up to the root of the checkout; and a project whose own locks no tag reads, while a tag names its `nuget.config` and reads another lock that holds the package. - A lock beside no project file is re-pinned only by a run in its own directory, so a package it holds first stays withheld for a run above it. - A tag whose config has no package source of its own does not replace the hub's sources, and a tag with `feeds = []` does not either: both count as tags that decide. - A pin that an earlier release wrote in such a workspace stays in the two files, and the scan reports it as applied beside the warning: `rollback` takes it out, and the attribute makes the build read it. - `rollback` still leaves a `nuget.config` that hosted mode created, holding nuget.org alone, and says so (`nuget_default_config_left`). The contract and docs/ecosystems.md describe the two codes and the rules; the NuGet workflow now also runs for the reader and `bazel/nuget.rs`, and the bazel group of the line budget follows the code (2486 to 2709). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * Close three ways a hosted NuGet pin could still break a rules_csharp build A review of the withhold found three workspaces in which `scan --mode hosted` still exited 0 with the pin written although the hub would not take it. Each is read from the ruleset's source at 0.5.0 (`_nuget_impl` in `csharp/extensions.bzl`) and held by unit tests; none was built on Bazel. - A tag has `feeds`, the tag that decides names the project's config, and that config has no package source. The hub takes a config's sources in place of its feeds only when the config has any (`if parsed_sources: feeds = parsed_sources`), so until then it downloads from the feeds. The Socket source would be the first source in the file and take their place, and a package that only the feeds hold could no longer be downloaded. Hosted mode now writes nothing for any NuGet package of the run and says to name the feeds in the config (`redirect_bazel_nuget_config_undeclared`). - A tag whose `packages_lock_json` is not a label in a plain string, beside a tag that names a lock of the project. It was taken for the project's own lock. It reads another one, which may hold the package first, and the hub holds a package to the first lock that names it. It now counts as a lock that cannot be read (`redirect_bazel_nuget_lock_not_repinned`), and the warning says to write the label out. - A config spelled in a way that is not one of NuGet's (`Nuget.Config`) on a disk that does not fold case. The rewriter writes a `nuget.config` of its own beside it, but a tag that names the other file passed as naming the project's config. The directory's spelling is now taken only where the rewriter's own name finds that file. Also: - The locks of the other tags are parsed once for a run, not once for each package. - Where the tag does not name its lock in a plain string, or the config lies outside the package of that label, the warning names the file instead of a label whose package would be a guess. - The comment on the lock check says why it is asked for every package, also where the hub does not read the config yet. - The docs say that the hub's rules are those of rules_csharp 0.5.0: a later release that gives each `name` a hub of its own is not told apart. The `bazel` group of the line budget follows the code, 2709 to 2766. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 447e822 commit 85bbd4a

9 files changed

Lines changed: 1763 additions & 15 deletions

File tree

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
name: Bazel patch compatibility (NuGet)
22

3-
# The NuGet flow of the real-Bazel tests (`nuget::` in `e2e_bazel_build`):
3+
# The NuGet flows of the real-Bazel tests (`nuget::` in `e2e_bazel_build`):
44
# 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.
5+
# `packages.lock.json` and `nuget.config`, and for a tag that names no
6+
# `nuget_config`, on each Bazel release below.
67
# bazel-compatibility.yml holds the legs and what every ecosystem shares;
78
# this file says when the NuGet legs run and on which releases.
89
#
@@ -35,11 +36,14 @@ on:
3536
# finds a package in the global packages folder, the reader of the
3637
# NuGet settings a project inherits (vendor/nuget_config.rs), the
3738
# 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+
# `rollback`, and the hosted scan's check of that folder. Then what
40+
# reads the tag in `MODULE.bazel`: the reader of the workspace files,
41+
# which every ecosystem's flow shares (bazel/mod.rs, starlark.rs and
42+
# workspace.rs), and what hosted mode makes of the tag (bazel/nuget.rs).
3943
# vendor/nuget_feed.rs is the vendored backend; it is here for the
4044
# 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.
45+
# Nothing else that another ecosystem's flow owns is here, so any other
46+
# change to one ecosystem starts that ecosystem's legs only.
4347
#
4448
# What it does not cover: shared engine code. The hosted rewrite itself
4549
# is there (`rewrite_nuget` in patch/redirect/mod.rs, the file of every
@@ -66,6 +70,10 @@ on:
6670
- 'crates/socket-patch-core/src/vex/discover/nuget.rs'
6771
- 'crates/socket-patch-core/src/patch/redirect/upstream/nuget.rs'
6872
- 'crates/socket-patch-cli/src/commands/scan/hosted/nuget.rs'
73+
- 'crates/socket-patch-core/src/bazel/mod.rs'
74+
- 'crates/socket-patch-core/src/bazel/nuget.rs'
75+
- 'crates/socket-patch-core/src/bazel/starlark.rs'
76+
- 'crates/socket-patch-core/src/bazel/workspace.rs'
6977
push:
7078
branches: [main]
7179
# The pull request paths, and what only a push to main covers: the
@@ -89,6 +97,10 @@ on:
8997
- 'crates/socket-patch-core/src/vex/discover/nuget.rs'
9098
- 'crates/socket-patch-core/src/patch/redirect/upstream/nuget.rs'
9199
- 'crates/socket-patch-cli/src/commands/scan/hosted/nuget.rs'
100+
- 'crates/socket-patch-core/src/bazel/mod.rs'
101+
- 'crates/socket-patch-core/src/bazel/nuget.rs'
102+
- 'crates/socket-patch-core/src/bazel/starlark.rs'
103+
- 'crates/socket-patch-core/src/bazel/workspace.rs'
92104
- 'crates/socket-patch-cli/tests/e2e_bazel_build/main.rs'
93105
- 'crates/socket-patch-core/src/patch/redirect/**'
94106
- 'crates/socket-patch-core/src/hosted/**'

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2572,6 +2572,8 @@ command's integration tests check its output.
25722572
| `redirect_pin_lockless` | `warnings[]` (warning) | scan/get `--mode hosted` (nuget, cargo): the pin was written without a lockfile that records its version, so `vex` cannot attest it and `rollback` / `remove` / `vendor` refuse it as unattributable. The detail names the lockfile to create (`dotnet restore --use-lock-file`, `cargo generate-lockfile`) before re-running the hosted scan. |
25732573
| `redirect_pypi_stale_install` | `warnings[]` (warning) | Hosted Python redirect: readable installed files differ from the pinned patched wheel's `RECORD` (or from the patch record, when the wheel cannot judge the copy). Read-only, repeated on re-scan, and excludes the package from same-run VEX. See the "Python stale-install guard" section. |
25742574
| `redirect_nuget_stale_global_package` / `vendor_nuget_stale_global_package` | `warnings[]` (warning, hosted) / `skipped` (warning, vendor advisory event) | scan `--mode hosted` / `vendor` / `scan --mode vendored` (nuget, v5.0 #352): NuGet's global packages folder (`NUGET_PACKAGES`, else `~/.nuget/packages`) already holds the patched package extracted from other bytes (its `.nupkg.metadata` `contentHash` is not the patched one). A patch keeps the upstream id and version and NuGet restores a package already in that folder without asking any source, so `dotnet restore` would keep the upstream bytes (silently without a lock, NU1403 against the re-pinned lock with one). The detail names the directory and the remedy: delete it, then `dotnet restore` (`dotnet nuget locals global-packages --clear` also works but empties the whole machine-wide folder, and the detail says so); CI caches of that folder must drop it too. Read-only like the gem guard (the folder is shared machine-wide), re-fired on every re-run until the copy is gone, skipped on `--dry-run`; a hosted purl it flags is excluded from the same run's `--vex` `assume_applied` and reported stale. A dir without `.nupkg.metadata` (a legacy `packages/` folder) is never judged. |
2575+
| `redirect_bazel_nuget_config_undeclared` | `warnings[]` (warning; human: `Warning: …`) | scan/get `--mode hosted` in a Bazel workspace (nuget, additive): a rules_csharp `nuget.from_lock` tag of the root `MODULE.bazel` (or of a file it includes) reads a `packages.lock.json` of the project, or may (its `packages_lock_json` is not a label in a plain string), and the tags do not show that the hub takes its package sources from the `nuget.config` hosted mode writes. The hub has one list of sources, which the last tag with a `nuget_config` or `feeds` decides: that tag names another file or has `feeds` alone, no tag has either, or a tag cannot be read with certainty (it is not a call of its own, passes `**`, or its `nuget_config` is not a label in a plain string). Also given, for every NuGet package of the run, where the tags do show it, a tag has `feeds` and the config names no package source yet: the first source in it would take the place of those `feeds`. Where the tags are in more than one file their order is unknown and every tag with a `nuget_config` or `feeds` must name the config. The hub would download the upstream package and fail the build on the re-pinned `contentHash`, so nothing is written for the package: no `nuget.config`, no lock entry. The package is a `skipped` / `redirect_unconfirmed` event; status and exit are unchanged (exit 0). One warning per package that the run would re-pin in a lock a tag reads (a package refused for another reason keeps that reason's code; a package in no such lock is pinned as without Bazel): the detail starts `<name>@<version>: <file>:<line>:`, says what stands against the pin, and names the way on: the attribute to put on the tag (`nuget_config = "//<package>:<config>"`, with the config as the project directory spells it; the file alone where the tag does not name its lock in a plain string or the config lies outside the package of that label), with the sources or `feeds` to name in that config where the tag had others, or, for a tag that names a NuGet config in a directory above the project, the directory to run the command in (`--cwd`). Never given outside a Bazel workspace, for a project without a lock, or when no tag names one of the project's locks: those runs are unchanged, as is one in a workspace where a part of `MODULE.bazel` cannot be read and the tags that are read show the config. See [Bazel workspaces: NuGet](../../docs/ecosystems.md#bazel-workspaces-nuget). |
2576+
| `redirect_bazel_nuget_lock_not_repinned` | `warnings[]` (warning; human: `Warning: …`) | scan/get `--mode hosted` in a Bazel workspace (nuget, additive): under the same condition (a `nuget.from_lock` tag reads, or may read, a lock of the project), another tag reads a `packages.lock.json` of the checkout that this run does not re-pin, and that lock holds the package at the patched version with another `contentHash`, or cannot be read; or another tag, beside one that names a lock of the project, does not name its lock in a plain string. The hub holds a package to the `contentHash` of the first lock that names it, so it would refuse the patched package: nothing is written for the package (`skipped` / `redirect_unconfirmed`, exit 0), also when the hub does not read the config yet. Not given when the tags are in one file and a lock of the project that holds the package comes before the other lock, or when the other lock holds the package at another version or already at the patched `contentHash`. The detail starts `<name>@<version>: <file>:<line>:`, names the tag and the lock, and says to run the command in a directory above both locks that holds a solution file or a `nuget.config` (`--cwd`), where the lock of every project is re-pinned; for a tag whose lock is not named, to write its `packages_lock_json` as a label in a plain string. Checked before `redirect_bazel_nuget_config_undeclared`. See [Bazel workspaces: NuGet](../../docs/ecosystems.md#bazel-workspaces-nuget). |
25752577
| `redirect_gem_stale_install` | `warnings[]` (warning) | scan / get `--mode hosted` (gem): a stale UNPATCHED materialization (installed gem, or committed archive in bundler's cache dir — `vendor/cache` unless `cache_path` moves it) that `bundle install` will reuse instead of fetching the redirected patch, and that the run did not refresh: it is in a shared gem home, or the refresh could not be done (the detail says why) or is turned off. The detail carries the remedy. A human run prints one warning for all of them. Full rules and flavors: the "Gem stale-install guard" section. |
25762578
| `redirect_gem_install_refreshed` | `warnings[]` (note) | scan / get `--mode hosted` (gem): a stale materialization the run replaced with the sha256-verified patched gem (the detail names the purl and the path). A human run prints a progress line instead. See the "Gem stale-install guard" section. |
25772579
| `redirect_gem_install_would_refresh` | `warnings[]` (note) | scan / get `--mode hosted --dry-run` (gem): a stale materialization a wet run would replace. Nothing was written or downloaded. |

0 commit comments

Comments
 (0)