Repository navigation
Withhold a hosted NuGet pin that a rules_csharp hub would not take - #2189
Mikola Lysenko (mikolalysenko) wants to merge 4 commits into
Conversation
… 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>
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>
…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>
PR SummaryMedium Risk Overview Adds Tests and CI: new real-Bazel and hermetic flows, unit coverage, docs/CLI contract, and Reviewed by Cursor Bugbot for commit 0b29a0b. Configure here. |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 0b29a0b. Configure here.
| } | ||
| Some(doc) => { | ||
| first |= nuget_lock_entries(doc).any(|e| e.id.eq_ignore_ascii_case(id)); | ||
| continue; |
There was a problem hiding this comment.
Earlier other-version lock skips later mismatch
Medium Severity
Hub::against treats an other tag's lock as establishing the hub pin when it merely names the same package id at any version. That sets first and, when tags are ordered, skips a later lock that holds the patched version with another contentHash. Hosted mode then writes the pin. The project's own locks set first only via version-aware pinned, and the lock warning is defined as firing when another lock holds that same version with a different hash. A workspace with one project on another version ahead of two projects on the patched version can therefore re-pin only the scanned lock; the hub still holds that version to the unre-pinned lock and the Bazel build fails on contentHash.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit 0b29a0b. Configure here.


In a Bazel workspace whose rules_csharp
nuget.from_lockhub would not take a hosted NuGet pin,scan --mode hostedandget --mode hostednow write nothing for the package and say how to get it patched, where they used to exit 0 and leave a build that fails.Based on main: two commits, a merge of main, and one commit with the fixes of a code review (see the end). The first commit is tests only and records what main does today; the second is the change, with the expectations of the first changed to match.
Decisions to confirm
nuget.from_lockis in a comprehension or a condition, passes**or has a computednuget_config, hosted mode writes nothing and says so. The other choice is to write as before, which keeps a patch for a workspace that names the right file through a constant and leaves the broken build for one that does not. A part ofMODULE.bazelthat the reader cannot read, beside tags that plainly show the config, does not withhold: such a workspace builds patched today, and the unread part can only hide another tag, which is a miss this change accepts everywhere else.local_package_dirsare not opened. A bare tag with them is withheld with a note; a tag that names the config is pinned as today, which fails the build when a directory holds the upstream package. Looking for the package file in those directories would close that, at the price of reading paths outside the checkout thatMODULE.bazelnames.feeds, a config above the project and the directory's spelling, and the three cases the code review added. Each answers a workspace in which a smaller version ended in a failed build or took a working patch away.What the code does today
rules_csharp's
nuget.from_lockdownloads every package ofpackages.lock.jsonand fails the build on a file that does not match the lock'scontentHash. All thenuget.from_locktags of a workspace make one hub (rules_csharp 0.5.0):feedsin the place of that list, and then the sources of thenuget.configit names withnuget_config. So the last tag that has either decides.contentHashof the first lock that names it.Hosted mode re-pins the
contentHashof a patched package in the project's locks and names the Socket source of the patch in the project'snuget.config. In each of these workspacessocket-patch scan --mode hosted --yesexits 0 and reports the patch applied, and the nextbazel buildfails withthe downloaded package does not match its contentHash in packages.lock.json(Bazel 9.3.2):nuget_config, which is the shape of the examples in the ruleset's own repository, with or withoutfeedsorlocal_package_dirs. The hub reads the re-pinned lock and not the config, and downloads the upstream package. The scan creates anuget.configthat nothing of the build reads.The first commit adds
nuget::hosted_no_configand records the first case on Bazel 9.3.2, asserting the failed build by its message. The second commit has to change that expectation.What changes
Hosted mode reads the
nuget.from_locktags of the Bazel workspace the project is in (the rootMODULE.bazeland the files it includes, through the existing reader inbazel/workspace.rs) and the locks they name. Nothing is evaluated and no Bazel is run.When a tag reads one of the project's locks, it writes nothing for a NuGet package that the hub would not take: no
nuget.config, no re-pinned lock entry. The package is askipped/redirect_unconfirmedevent, 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. They show it when everynuget.from_lockis a plain call of its own and the last one that has anuget_configorfeedsnames the project's config as a label in a plain string. Where the tags are in more than one file their order is not known, and every tag that has either must name it.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_filesentry. The warning has another way on where the attribute alone would not do:nuget.configfor several projects): run the command in that directory (--cwd), which writes the file the tag reads. Otherwise: the attribute, and the package sources of the other file to name in the project's config, because the hub takes its sources from one file.feeds: the attribute, and those feeds to name in the config, whose sources stand in the place offeeds.feeds, the tags show the project's config, and that config names no package source: the hub takes a config's sources in place of its feeds only once the config has any, so the Socket source would drop the feeds. Nothing is written for any NuGet package of the run, and the warning says to name the feeds in the config.**, or has anuget_configthat is no plain string: write it out.local_package_dirs: the warning adds that the hub asks those directories before every source, so the upstream package must not lie in one.The config is named as the project directory spells it (
//:NuGet.Configfor a file of that name), so that the label also names the file on a disk that does not fold case. Where the tag does not name its lock in a plain string, or the config lies outside the package of the lock's label, the warning names the file and not a label, whose package would be a guess.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 anothercontentHash, or cannot be read. A tag that does not name its lock in a plain string, beside one that names a lock of the project, counts as such a lock, and its warning says to write the label out. Where the tags are in one file, only a lock that the hub reads before the project's own stands against the pin. The way on: run the command in a directory above both locks that holds a solution file or anuget.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 following the other warning cannot end in the failed build.Nothing changes:
nuget.from_lockcall names, which Bazel does not build;MODULE.bazelthat cannot be read (abazel_depwhoserepo_nameis an expression): such a part can only hide a tag;The first three and the fifth were held to a binary built from the base, byte for byte (see below).
Contents
Two commits, a merge of main and the review's commit.
bazel/nuget.rs(new), the call inhosted/engine.rs, the withhold inrewrite_nuget, onepub modlinescripts/loc-budget.py(279 inbazel/nuget.rs)tests/e2e_bazel_build/nuget.rs, unit tests inbazel/nuget.rsandpatch/redirect/mod.rsBazel workspaces: NuGetindocs/ecosystems.md, a note in the NuGet row of the mode matrix, two rows inCLI_CONTRACT.md.github/workflows/bazel-nuget.yml: the NuGet legs now also start for the workspace reader andbazel/nuget.rsscripts/loc-budget.json: thebazelgroup follows the code, 2486 to 2766How it is built:
bazel/nuget.rsreads 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 already 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_nugetasks it about each package it would otherwise pin, with the package's version and patchedcontentHash; a package it refuses for another reason keeps that reason's code.What it proves, and where
nuget::hosted_no_config, in the legs ofbazel-nuget.yml): the scan writes nothing and warns,bazel buildandbazel runstill pass on the upstream package with no file changed; then the attribute and the same scan, and the build prints the patched marker with the package fetched from the stand-in patch host.nuget::hosted_from_lock, the existing flow for a tag that names the config, is unchanged. Both together take 13 to 22 seconds per release locally. No leg asks a public registry.appliedis 1), and so do the two hermetic tests.nuget::only_a_tag_that_reads_the_lock_without_its_config_is_withheld: what the command prints for a person, its JSON, its exit code and the files it leaves are the same in a checkout that is no Bazel workspace, in one whose tag names the config, and in one whose tag reads another project's lock; only the tag that reads this lock without a config is withheld.nuget::the_pin_is_written_where_the_hub_takes_it: nine workspaces of a hub with more than one tag or another kind of tag, each either pinned with the files named or withheld with the code and the words of its row.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. With a binary of the base, the two-lock workspace and a bare tag withlocal_package_dirsended in the failed build.feeds, a tag whose lock is not named, a spelling that is not NuGet's) are held by unit tests and were read in the ruleset's source at 0.5.0. None was built on Bazel.Local gate, after the merge of main and again after the review's commit: clippy for the workspace and for the test target with
-D warnings,cargo check --workspace --all-targets --all-features, the NuGet unit tests of the core, the existing test targets that exercise hosted NuGet (no existing expectation changed),e2e_bazel_buildwithout Bazel,cli,spawn_env_hygiene, the line budget, and the two NuGet flows on real Bazel 9.3.2. Before the merge, on the second commit: the whole core unit suite (8296 passed), the scripts' unit tests,actionlintand the tool pin check on the workflow, and the flows on Bazel 8.8.2 and 7.7.2 as well. One run ofe2e_vex_lockfileafter the merge failed in a test helper that this change does not touch (a rename of a temporary Maven settings file); the same target passed on the next run.Not covered
MODULE.bazelhides; and what the command line or an rc file tells Bazel.local_package_dirsholds. With the upstream package in one and a tag that names the config, the scan pins and the build fails as it does today (Bazel 9.3.2). The directories are named by paths outside the checkout and are not opened.nuget.configand reads another lock that holds the package.feeds = [], neither of which replaces the hub's sources; a lock of another tag that cannot be read (over 16 MiB, or no JSON).socket-patch rollbacktakes it out, and the attribute makes the build read it.rollbackstill leaves anuget.configthat hosted mode created, holding nuget.org alone, and says so.nameofnuget.from_locka hub of its own; no release has that yet, and the tags are read as the one hub of 0.5.0.Code review
A review of the whole diff gave nine findings. Five are fixed in the last commit; four are not, for the reasons below.
Fixed:
feeds. The tags showed the config, so hosted mode wrote; the Socket source would have been the file's first source and taken the place of the feeds. Now withheld for every NuGet package of the run, with the feeds to name.Nuget.Config). A tag naming it passed as naming the config, while the rewriter writes anuget.configbeside it. The directory's spelling is now taken only where the rewriter's own name finds the file. Its test branch runs on Linux.//<directory>:<config>, which is no label when that directory is not a package. It now names the file.Not fixed:
name. True of 0.5.0, the newest release. The ruleset's main branch separates hubs by name; there is no release to tell apart yet, so it is in the docs and under "Not covered" instead of behind a version check.MODULE.bazel. No caller records today. A comment says so.🤖 Generated with Claude Code