Skip to content

Make vendored mode work for a NuGet package of a rules_csharp hub, or decline it - #2223

Open
Mikola Lysenko (mikolalysenko) wants to merge 6 commits into
mainfrom
bazel-vendored/nuget-feed-block
Open

Mikola Lysenko (mikolalysenko) wants to merge 6 commits into
mainfrom
bazel-vendored/nuget-feed-block

Conversation

@mikolalysenko

@mikolalysenko Mikola Lysenko (mikolalysenko) commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Vendored mode now works for a NuGet package that a rules_csharp nuget.from_lock hub builds under Bazel: it keeps the package where the hub finds it and one managed block in the root MODULE.bazel, and where the checkout does not show that the block works it declines the package with vendor_nuget_bazel_workspace and writes nothing.

Based on main, in three commits and a merge of main. 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; the third, after the merge, holds the fixes of the code review below.

All of the Bazel support, this included, is being labelled experimental in the docs by another open pull request (#2222).

Decisions for the maintainer

  1. socket-patch writes the root MODULE.bazel: the block adds a second nuget.from_lock tag. Vendored mode appends one managed block there and removes it byte for byte, as hosted mode does for Maven. Unlike the Maven block, which overrides one repository, this one is a tag of the user's own hub: its feeds name the folder of each vendored patch and then the sources the hub had. It writes no other file of Bazel's. The alternative is to decline every NuGet package of a hub.
  2. The block is written for exact releases only. rules_csharp 0.5.0, as MODULE.bazel.lock shows it, and Bazel 9.3.2, 8.8.2 or 7.7.2, named in .bazelversion or .bazeliskrc. A checkout on any other release, or one that names none, is declined. The block rests on Bazel reading a relative file: URL from the root of the workspace, which Bazel does not document, and 9.1.0 has other code for such a URL than its neighbours. Taking ranges of releases is a small change, and a risk this change does not take.
  3. A decline is a failure. failed, exit 1, like the Composer refusal; a dry run of scan or get lists the package and exits 0. The alternative is a warning with exit 0.
  4. --allow-vendored-in-bazel lifts the decline, and the Bazel build then fails. With the switch the package is vendored as in a project without Bazel, which the hub reads as a bad URL. That is for a project whose lock a tag names and which Bazel does not build. The alternative is that the switch does nothing for NuGet.
  5. A package that an earlier release vendored is not migrated. It stays as it is, without a block, and its Bazel build stays broken until vendor --revert and a new vendored run.
  6. An undo beside a block that was changed by hand fails and changes nothing. vendor --revert, rollback and remove exit 1 and name the line of the block. The alternative is to revert the lock and nuget.config and leave the block with a warning: that exits 0 and leaves a Bazel build that fails on the committed package, with no command of socket-patch that gets out of it.
  7. An earlier release of socket-patch breaks the build when it reverts what this one vendored. Its vendor --revert exits 0, restores the lock and leaves the block and the package. The docs say to undo with the release that vendored. Whether the ledger should make an earlier release refuse such an entry is open; this change does not do it.
  8. A remote downloader in an rc file declines; --nofetch in an rc file does not. The first is read in Bazel's source and not built. With the second the build lags until the project's next fetch, as after any change of a lock, so it is a documented limit and no refusal. The same holds for a vendor directory made before the patch: socket-patch declines when it sees --vendor_dir in an rc file or a directory with VENDOR.bazel at the root, and it cannot see one named on a command line. In both cases the build takes the unpatched package and exits 0 with a warning of Bazel's.
  9. Four shapes are accepted without a committed real-Bazel test. A hub with a name, a tag with feeds of its own, a tag without nuget_config, and two patches in one block. Each was built by hand on Bazel 9.3.2 and 7.7.2 and printed the patched marker; unit tests cover what is written for them. The cautious alternative is to decline them until a committed test builds each.
  10. vex does not read the block. For a vendored package whose block is not in MODULE.bazel (the switch, an earlier release, a block taken out by hand) it says not_affected while the Bazel build fails. The docs list this.
  11. The layout changes inside a hub's project only. There the vendored package lies at .socket/vendor/nuget/<uuid>/<id>/<version>/<id>.<version>.nupkg (the id in lower case), the shape of a folder feed, and nuget.config names <uuid> as before: one copy serves NuGet and Bazel. Every other NuGet project keeps the flat file. The alternative, one layout everywhere, changes what every vendored NuGet project commits.

What the code does today

scan --mode vendored, get --mode vendored and vendor commit the patched package under .socket/vendor/nuget/<uuid>/, name that folder as a package source in nuget.config and re-pin packages.lock.json. NuGet takes a folder as a source. rules_csharp's nuget.from_lock reads the same line as the URL of a feed.

So in a workspace whose hub reads the project's lock, the command exits 0 and reports the package vendored, and the next bazel build fails. Bazel 9.3.2 crashes in the module extension (exit 37, NullPointerException thrown during Starlark evaluation). Bazel 8.8.2 and 7.7.2 stop at Bad URL: .socket/vendor/nuget/<uuid>. The first commit adds a real-Bazel test that records exactly this; the second changes its expectations.

What changes

In the root of a Bazel workspace whose one nuget.from_lock reads the project's lock, the three commands write:

  • the package at .socket/vendor/nuget/<uuid>/<id>/<version>/<id>.<version>.nupkg, with the id in lower case. It is the only copy.
  • nuget.config with that directory as the source, so dotnet restore outside Bazel takes the same file, and the lock as before.
  • one block at the end of the root MODULE.bazel, after one blank line:
# >>> socket-patch: NuGet patches for Bazel (managed block: socket-patch writes and removes it) >>>
socket_patch_nuget = use_extension("@rules_csharp//csharp:extensions.bzl", "nuget")
socket_patch_nuget.from_lock(
    feeds = [
        "file:.socket/vendor/nuget/<uuid>",
        "https://api.nuget.org/v3/index.json",
    ],
    packages_lock_json = "//:packages.lock.json",
)
# <<< socket-patch <<<

The block is a second nuget.from_lock of the same hub, after every tag of the user's. Its feeds are the folder of each vendored patch, which Bazel reads from the root of the workspace, and then the sources the hub had without the block: those of the tag's nuget_config, read line by line as the ruleset reads them, or else the tag's feeds. It carries the label of the user's use_extension, their packages_lock_json, and the hub's name where the tag gives one. No call of the user's is edited, and nothing is written to .bazelrc or .bazelignore.

The build then takes the committed package and asks no host for it. vendor --check, list and vex see the package as any vendored NuGet package. A later vendored run keeps the block in step with the tag, so a source added to nuget.config reaches the hub once socket-patch has run again. A later patch of the same package takes the place of the earlier one's folder in the block.

The undo. vendor --revert, rollback and remove take the folder of a patch out of the block, and the block goes with its last folder: MODULE.bazel is then byte for byte what it was. With one vendored patch every other file is too. With several, nuget.config can keep the source mapping that the first of them created, as it does without Bazel.

The block goes last, after the lock and the config. A nuget.config that was written anew since the vendored run and still names the folder is left as it is with a warning (vendor_lock_entry_drifted), as without Bazel: its lock pin and its package stay, and so does the block, through which alone the hub takes that package.

A block that was changed by hand is not taken out, and then nothing is. The three commands fail with exit 1 (revert_failed from vendor --revert, vendor_revert_failed from rollback and remove), name MODULE.bazel:<line> and leave every file as it is: with the lock restored beside such a block the hub would hold the committed package to the upstream contentHash, and the build would fail. Put the block back as socket-patch wrote it, or delete it, and run the command again.

Where it declines. The package is refused with vendor_nuget_bazel_workspace, through the dispatch that the Composer refusal uses: before any download, failed, exit 1, nothing written. A hosted pin stays, and its eject says kept hosted: .... The message names the reason, with file and line where there is one, and --mode hosted. The reasons:

  • the project is below the root of its Bazel workspace, or another workspace lies above it in the checkout;
  • more than one nuget.from_lock, or tags that cannot be read with certainty;
  • a tag that is not a call of its own with every argument written out, or one on an isolated or dev-dependency use_extension;
  • local_package_dirs, which the hub asks before every feed, or netrc or auth_patterns;
  • a nuget_config that cannot be read, or a source that cannot stand in a string;
  • feeds beside a nuget_config that has no package source. The run names its folder in that file, rules_csharp takes the sources of a nuget_config in place of feeds, and the next run would write the block without the tag's feeds: a package that only they hold would then fail the build. Name the feeds in the file;
  • MODULE.bazel.lock does not show that Bazel selected rules_csharp 0.5.0;
  • the checkout does not name Bazel 9.3.2, 8.8.2 or 7.7.2;
  • an rc file names --repository_disable_download or --vendor_dir, or a directory at the root holds VENDOR.bazel;
  • an rc file names a remote downloader (--experimental_remote_downloader), which takes no file: feed;
  • MODULE.bazel is a symbolic link, or its block was changed by hand;
  • a lock spells the package's version otherwise than NuGet writes it (1.2.3.0 for 1.2.3): the hub asks its feeds for the lock's spelling, and the folder holds the one dotnet restore writes.

--allow-vendored-in-bazel (SOCKET_ALLOW_VENDORED_IN_BAZEL) lifts the refusal. The help text, the contract and the docs said that Composer's was the one refusal the switch lifts; they now name both. A package that is wired already passes without the switch.

Hosted mode beside the block. The block's tag is the hub's last one, so hosted mode writes nothing for another package of the project (redirect_bazel_nuget_config_undeclared). It now says to vendor that package too, or to take the vendored ones out.

Nothing changes for a NuGet project in no Bazel workspace, and for one whose locks no nuget.from_lock reads: the same files byte for byte, and no line of MODULE.bazel.

Limits

  • The block works in the root module only. In a workspace that depends on this one as a module, Bazel reads the folder from the other root and that build fails. Hosted mode works there.
  • A module this workspace depends on may have a nuget.from_lock with sources of its own. The hub takes that tag after the block, and the build fails on the re-pinned lock. Hosted mode fails the same way.
  • A build with --nofetch in an output base that built the unpatched project, and a build from a vendor directory made before the patch, build the unpatched package and exit 0, with a warning of Bazel's. Such a build still rewrites MODULE.bazel.lock to the pins of the patched package. socket-patch sees --vendor_dir and --repository_disable_download in an rc file and a vendor directory at the root; it does not see --nofetch, in an rc file or on a command line.
  • A committed package that is changed or missing fails the build, except on a machine whose repository cache holds the package under the sha256 that MODULE.bazel.lock records: Bazel builds the cached file there, which is the patched one. vendor --check catches the change on every machine.
  • A checkout that is deleted and made again at the same path under a running Bazel server does not find the committed package until the server restarts. The build fails; it does not take the upstream package.
  • MODULE.bazel.lock is stale right after the run, as after a hosted run: a build with --lockfile_mode=error fails until a build that may update the lock has run.
  • Bazel prints three WARNING: Download from file:... lines for every other package of the lock, for each vendored patch, when the module extension runs.
  • A source added to nuget.config reaches the hub only after socket-patch has run again.
  • An earlier release of socket-patch does not know the layout or the block. On a checkout that this one vendored, its vendor --check fails (vendor_check_failed), its vendored run writes a second copy of the package in the folder of the patch, and its vendor --revert exits 0, restores the lock and leaves the block and the package: the Bazel build then fails on the lock's contentHash (measured by hand on Bazel 9.3.2). Undo with the release that vendored.

Each of these is in docs/ecosystems.md, "Vendored mode under a rules_csharp hub".

Code review

A review of the whole change at the merge gave seven findings. Each was checked against the code, and those about the hub against the source of rules_csharp 0.5.0. The third commit holds the fixes.

Fixed:

  • An undo that leaves nuget.config wired took the block out. Where the config was written anew and still names the folder, the undo keeps the config, the lock pin and the package. The block went first all the same, so the hub read the folder in nuget.config as the URL of a feed, and the Bazel build failed on a package that built before the undo. The block now goes last and stays where the config is left wired. A lock that cannot be restored leaves the block too.
  • A later patch of the same package left the earlier patch's folder in the block. The block then named a folder that the run removes, and the undo of the later patch left a block behind that no command of socket-patch takes out. The folder of the earlier patch now goes out of the block when the later one is written into it.
  • A second run rewrote the block for a tag whose nuget_config has no package source. The run gives such a config nuget.org beside the folder, and the block was read before that. It is now read from the tag after the config is written.
  • A lock that spells the version otherwise than NuGet writes it was vendored, and the hub did not find the package. rules_csharp asks a feed for the lock's resolved as it is written. Such a package is declined, by the backend and by the refusal that every way in asks first. dotnet restore writes the normalized version, so this needs a lock written by hand.
  • The folder of a hub's package was written out in two places of the backend. One function now.
  • The refusal looked for the project's locks before it knew of a Bazel workspace. It asks for the workspace first, so a NuGet run outside Bazel pays no walk for it.

Fixed in part:

  • The pull request paths of the real-Bazel NuGet workflow lack vendor/path.rs and vendor/state.rs, which this change touches for the layout and the ledger record. Both are in the push list now. They are not in the pull request list: every ecosystem's vendored mode shares them, and the workflow keeps shared engine code out of that list by its own rule.

Not fixed:

  • In a Bazel workspace the gate is read again for every NuGet package, twice a package, as the rest of the backend's first step is (once to see what must be downloaded, once for the run). It costs the parsing of MODULE.bazel, its lock and the rc files. An answer kept across packages would have to be dropped after every write of the block, and a stale one writes a wrong block; the cost did not seem worth that.

Each of the four behaviour fixes has a unit test. The tests were not run against the code of before the fixes.

Contents

File What Size
bazel/nuget.rs the gate, the block's text, adding and removing a folder, what hosted mode says beside the block +525 −20 lines, code 279 → 657; unit tests +483
bazel/block.rs (new) finding, placing and cutting a managed block; shared with Maven 113 lines, 66 code
bazel/maven.rs uses block.rs; shares the label of a use_extension and the release check of a ruleset +51 −89, code 967 → 935
bazel/mod.rs the workspace above a workspace +23 −5, code 34 → 43
vendor/nuget_feed.rs the layout, the block's write and revert, the check before a revert's first write, the ledger record, the refusal +264 −18, code 1513 → 1707; unit tests +268
vendor/refusals.rs, path.rs, state.rs, mod.rs the dispatch, the nested file name, the ledger table +29 −20, code +3; unit tests +41
vex/discover/nuget.rs reads the package below <id>/<version> +31 −12, code 648 → 659; unit tests +31
args.rs help text of the switch +6 −3, no code
tests/e2e_bazel_build/nuget.rs, harness.rs the real-Bazel flow and the flow through the binary +651 −2
docs/ecosystems.md, CLI_CONTRACT.md the section, the table row, the known problems, the code's row +200 −14
bazel-nuget.yml, scripts/loc-budget.json the paths that start the legs; the bazel cap, 4041 after the merge and the fixes (main: 3615) +19 −5
third commit (the review fixes) vendor/nuget_feed.rs +169 −35 and unit tests +135; bazel/nuget.rs +35 −11 with its test; the docs, the contract row, the workflow's push list, the cap +373 −51

Implementation of the first two commits: +1042 −167 lines, 629 code lines net. Unit tests +830. The first commit is tests only (+170 −1, and 4 lines of the workflow's comments).

What it proves, and where

Everything below ran on macOS arm64, with rules_csharp 0.5.0 and packages that carry no signature. No build and no command reached nuget.org or a Socket host.

  • nuget::vendored_from_lock on real Bazel 9.3.2, 8.8.2 and 7.7.2. A checkout that names no release is declined and its build stays on the upstream package. With .bazelversion, the scan writes the layout and the block; vendor --check passes; vex names the package; bazel query //... lists what it listed. The build prints the patched marker with no request for the package, and again with the registry stand-in stopped, a new output base and the package out of the repository cache. One changed byte fails vendor --check and the build. vendor --revert restores every file and the upstream build. The two hosted NuGet flows pass beside it on the three releases.
  • The first commit's version of that test passes on main's product code on the three releases, and the final version fails there, as it should. Both were run before the last round of fixes, which changed neither that test nor the first commit.
  • nuget::vendored_mode_keeps_the_block_or_declines, through the binary without Bazel: the unchanged layout outside a hub; the block and its three undos; the three undos beside a block changed by hand (exit 1, no file changed, and the full undo once the block is deleted); the declines by scan, get, a dry run and for a person, among them the tag with feeds beside a config without a source; the eject; and the switch.
  • A comparison with a binary built from main: nine vendored commands in six project shapes outside a hub, as a script and as a person, gave 216 equal outputs and file trees.
  • Unit tests hold the gate row by row with its reasons, the block's text in four line-ending shapes, the byte-exact undo, the ledger record, the revert beside a changed block wherever its record stands in the ledger, and the path git is asked about for a hub's package.
  • Deliberate breaks of the new code. Five, one for each behaviour the last round added, were run on the final code and each failed a test. Thirty more were run on earlier versions of these files and each failed a test; they were not run again.
  • By hand on Bazel 9.3.2 and 7.7.2, in flows that are not committed: a named hub, a tag in an included file, a tag with feeds alone, a tag without nuget_config, a patched package as a transitive dependency, a private source in nuget.config, CRLF files and two patches each built the patched package, and the upstream build came back after the undo.
  • The whole core unit suite (8422 tests), the CLI unit tests and twenty integration targets pass; no existing expectation changed.

After the merge of main and the review fixes

The merge had two conflicts: the imports of bazel/maven.rs, and the bazel cap of scripts/loc-budget.json, which is the merged count.

  • At the merge (af5dd83): cargo check of the workspace with all targets, clippy as CI runs it, 397 unit tests of the modules this change touches, ten CLI test targets, the three NuGet flows on real Bazel 9.3.2 (3 of 3) and the Maven flows on real Bazel 9.3.2 behind a proxy that logs every request (5 of 5; no request to a public Maven host).
  • At the head (fadf0e9): clippy as CI runs it; 401 unit tests of the same modules, the four new ones among them; and the CLI targets e2e_bazel_build without Bazel (66), in_process_vendor (157), e2e_vex_vendor (39), vendor_revert_tamper_e2e (25) and e2e_nuget (2). All pass.
  • Not run at the head: the real-Bazel flows. The machine ran short of disk before that step. The third commit changes when the block is read on the first run and the order of the undo, both of which nuget::vendored_from_lock runs through; the hermetic flow through the binary passes. The legs in GitHub Actions run them when the pull request leaves draft.
  • Bazel 8.8.2 and 7.7.2, the whole core unit suite and the script tests were not run again after the merge.

Not covered

  • Linux and Windows: nothing ran there. The real-Bazel target is Unix only; the Linux legs run in GitHub Actions.
  • In a committed real-Bazel test: a hub with a name, a tag with feeds of its own, a tag without nuget_config, two patches at once, a patched package with dependencies of its own. They were built by hand only.
  • A remote downloader: the decline rests on reading Bazel's source (7.7.2 hands a file: feed to the downloader; 8.8.2 and 9.3.2 download every package of the lock without it). No build ran with one.
  • The undo with several patches: that nuget.config keeps the first patch's source mapping is main's behaviour without Bazel too, and is not changed here.
  • The test targets that need a real .NET SDK and nuget.org, or Docker, were not run here. No dotnet restore of the new layout ran in a test.
  • The four tests of the third commit were not run against the code of before it.
  • The undo after a second patch of the same package. By the code, and not run: vendor --revert then puts back the config and the lock pin of the first patch, whose folder the second run removed. That is main's behaviour without Bazel too and is not changed here; with a hub, the block is gone as well.
  • A build that another workspace starts, a dependency module's own tags, and flags on Bazel's command line: the checkout does not show them (see Limits).

Generated by Claude Code

…b on real Bazel

Tests only. rules_csharp's `nuget.from_lock` downloads every package of
`packages.lock.json` from the package sources of `nuget.config` and holds
each file to the lock's `contentHash`. The real-Bazel NuGet flows ran
hosted mode in that workspace, and vendored mode in none.

nuget::vendored_from_lock records what vendored mode does there today, on
the machine that restored the project:

- `scan --mode vendored --yes` exits 0 with status `success` and reports
  the package vendored. It downloads the patched package from the patch
  service once and keeps it at
  `.socket/vendor/nuget/<uuid>/<id>.<version>.nupkg`, names that folder
  as a package source in `nuget.config`
  (`<add key="socket-patch-<uuid>" value=".socket/vendor/nuget/<uuid>" />`,
  with a source mapping) and re-pins the lock to the patched package.
  `vex` attests the package.
- The next `bazel build` fails. NuGet takes a folder as a package source;
  the hub reads the same line as the URL of a feed. Bazel 9.3.2 crashes in
  the module extension (exit 37, "NullPointerException thrown during
  Starlark evaluation"); Bazel 8.8.2 and 7.7.2 refuse the URL ("Bad URL:
  .socket/vendor/nuget/<uuid>"). Bazel asks no host for the package and
  changes no file.
- `vendor --revert` puts every file back, and the build with them.

The flow records a defect: a command that exits 0 leaves a build broken.
It is here so that the change which removes the defect shows as a change
of this test's expectations.

The grant names the patched package by its URL on the patch host, and
vendored mode downloads it itself, so the flow passes `--patch-server-url`
with the loopback stand-in, which serves the package under the path of
that URL. No build and no command reaches nuget.org or a Socket host.

Run on Bazel 9.3.2, 8.8.2 and 7.7.2 on macOS arm64: the flow passes on
each, in about ten seconds. The workflow's comments name the flow.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… decline it

Vendored mode commits the patched package under
`.socket/vendor/nuget/<uuid>/`, names that folder as a package source in
`nuget.config` and re-pins the lock. NuGet takes a folder as a source.
rules_csharp's `nuget.from_lock` reads the same line as the URL of a feed:
after `scan --mode vendored` had exited 0 and reported the package
vendored, the next `bazel build` crashed Bazel 9.3.2 in the module
extension (exit 37) and failed on 8.8.2 and 7.7.2 with `Bad URL:
.socket/vendor/nuget/...`. That is the flow of the previous commit.

What the hub does take

A feed `file:<folder>`, which Bazel reads from the root of the workspace,
with the package at `<folder>/<id>/<version>/<id>.<version>.nupkg`; and
the last `nuget.from_lock` that has `feeds` decides the hub's sources
(`_nuget_impl` in csharp/extensions.bzl at 0.5.0). So in the root of a
Bazel workspace whose one `nuget.from_lock` reads the project's lock,
`scan --mode vendored`, `get --mode vendored` and `vendor` now write:

- the package at
  `.socket/vendor/nuget/<uuid>/<idLower>/<version>/<idLower>.<version>.nupkg`,
  the one copy;
- `nuget.config` with that directory as the source, so `dotnet restore`
  outside Bazel takes the same file, and the lock as before;
- one block at the end of the root `MODULE.bazel`, after one blank line:

      # >>> socket-patch: NuGet patches for Bazel (managed block: socket-patch writes and removes it) >>>
      socket_patch_nuget = use_extension("@rules_csharp//csharp:extensions.bzl", "nuget")
      socket_patch_nuget.from_lock(
          feeds = [
              "file:.socket/vendor/nuget/<uuid>",
              "https://api.nuget.org/v3/index.json",
          ],
          packages_lock_json = "//:packages.lock.json",
      )
      # <<< socket-patch <<<

  A second tag of the same hub, after every tag of the user's: the folder
  of each vendored patch, then the sources the hub had without the block
  (those of the tag's `nuget_config`, read line by line as the ruleset
  reads them, or else its `feeds`), with the label of the user's
  `use_extension`, their `packages_lock_json`, and the hub's `name` where
  the tag gives one. No call of the user's is edited.

On Bazel 9.3.2, 8.8.2 and 7.7.2 with rules_csharp 0.5.0 the build then
prints the patched marker and asks no host for the package, also with the
stand-in stopped, a new output base and the package in no repository
cache. One changed byte of the committed package fails the build
("Checksum was ... but wanted ...") and `vendor --check`. `bazel query
//...` lists what it listed. `vendor --check`, `list` and `vex` see the
package as any vendored NuGet package, and a later vendored run keeps the
block in step with the tag (a source added to `nuget.config` reaches it).

The undo

`vendor --revert`, `rollback` and `remove` take the folder of a patch out
of the block, and the block goes with its last folder: `MODULE.bazel` is
then byte for byte what it was. With one vendored patch every other file
is too. With several, `nuget.config` can keep the source mapping that the
first of them created: once another patch has changed the file, the
config revert takes out only the two elements of its own patch, as it
does without Bazel.

A block that was changed by hand cannot be taken out. Reverting the lock
and the config beside it would leave the hub holding the committed
package to the upstream `contentHash`, and the build would fail after an
undo that exited 0. So the undo checks the block before its first write
and fails with nothing changed (`revert_failed` from `vendor --revert`,
`vendor_revert_failed` from `rollback` and `remove`, exit 1). The message
names the line of the block and the two ways on: put the block back as
socket-patch wrote it, or delete it, and run the command again.

Where it declines

The block is written only where the checkout shows that it works.
Otherwise the package is refused with `vendor_nuget_bazel_workspace`
through `vendor::refusals::bazel_workspace_refusals`, the dispatch that
Composer's refusal uses: before any download, a `failed` event, exit 1; a
dry run of `scan` or `get` lists it and exits 0; a hosted pin stays, and
its eject says `kept hosted: ...`. The detail names the reason, with file
and line where there is one, and `--mode hosted`. The reasons:

- the project is below the root of its workspace, or another workspace
  lies above it in the checkout (built from there, Bazel reads the folder
  from the other root);
- more than one `nuget.from_lock`, or tags that cannot be read with
  certainty; a tag that is not a call of its own with every argument
  written out, or on an isolated or dev-dependency `use_extension`;
- `local_package_dirs`, which the hub asks before every feed, and
  `netrc` or `auth_patterns`, which the block was not built with;
- a `nuget_config` that cannot be read, a source that cannot stand in a
  string;
- `feeds` beside a `nuget_config` that has no package source. The run
  names its folder in that file (and nuget.org, where NuGet had it by
  default), rules_csharp takes the sources of a `nuget_config` in place
  of `feeds`, and so would the next run: it would write the block
  without the tag's feeds, and a package that only they hold would fail
  the build after a run that exited 0. Hosted mode withholds in the same
  shape, with the same way on: name the feeds in the file;
- `MODULE.bazel.lock` does not show that Bazel selected rules_csharp
  0.5.0;
- the checkout does not name Bazel 9.3.2, 8.8.2 or 7.7.2 (`.bazelversion`,
  `.bazeliskrc`). The block rests on Bazel reading a relative `file:` URL
  from the root of the workspace, which Bazel does not document, and
  9.1.0 has other code for such a URL than 9.0.2 and 9.1.1
  (DownloadManager.java), so no release is taken for its neighbour's
  sake;
- an rc file names `--repository_disable_download` or `--vendor_dir`, or
  a directory at the root holds `VENDOR.bazel`;
- an rc file names a remote downloader
  (`--experimental_remote_downloader`), which takes no `file:` feed. Read
  in Bazel's source and not built: 7.7.2 hands the feed to the
  downloader, which cannot read the folder, and 8.8.2 and 9.3.2 download
  every package of the lock without it
  (GrpcRemoteDownloader.java);
- `MODULE.bazel` is a symbolic link, or its block was changed by hand.

`--allow-vendored-in-bazel` (SOCKET_ALLOW_VENDORED_IN_BAZEL) lifts the
refusal, and the package is vendored as in a project without Bazel. The
help text, the contract and the docs said that Composer's was the one
refusal the switch lifts; they now name both. A package that is wired
already passes without the switch, as Composer's does.

Nothing changes for a NuGet project in no Bazel workspace, and for one
whose locks no `nuget.from_lock` reads: the same files, byte for byte,
and no line of `MODULE.bazel`.

Hosted mode beside the block: the block's tag is the hub's last one, so
hosted mode writes nothing for another package of the project
(`redirect_bazel_nuget_config_undeclared`) and now says to vendor that
package too or to take the vendored ones out, instead of telling the user
to edit the block.

How

- bazel/nuget.rs: `vendored` reads the tags (without the block), the
  ruleset's release, the Bazel release and the rc files through the
  existing readers, and gives the block's contents or the reason;
  `with_folder` and `without_folder` edit the block.
- bazel/block.rs is new and holds what the Maven block and this one
  share, moved out of bazel/maven.rs: finding a block by its first line,
  putting one at the end of a file in the file's line break, cutting it
  out to the byte, and the file as it reads without it. `proxy` (the
  label of a `use_extension`), `asked` and the stale check of a ruleset's
  release are shared from maven.rs as they are.
- vendor/nuget_feed.rs decides the layout, writes the block after the
  lock, records it in the ledger (`nuget_bazel_feed`, which may name the
  root `MODULE.bazel` alone) and reverts it; a failed block write unwinds
  the config and the locks, and a revert asks the block before it writes.
- vex/discover/nuget.rs and vendor/path.rs read the package below
  `<idLower>/<version>` as well.

Limits, all in docs/ecosystems.md "Bazel workspaces: NuGet": the block
works in the root module only; a module this one depends on with its own
`nuget.from_lock` sources replaces the block's feeds; `--nofetch`, in an
rc file or on a command line, and a vendor directory made before the
patch, can build the unpatched package with exit 0; a machine whose
repository cache holds the package under the sha256 of Bazel's lock
builds the cached, patched file and does not read a changed or missing
copy, which `vendor --check` catches; a checkout deleted and made again
under a running Bazel server does not find the package until the server
restarts; a frozen lock fails right after the run, as in hosted mode;
three WARNING lines per other package and vendored patch when the
extension runs. An earlier release does not know the layout or the
block: on a checkout this one vendored its `vendor --check` fails, its
vendored run writes a second copy, and its `vendor --revert` exits 0 and
leaves the block and the package beside the restored lock, so the Bazel
build fails (measured on 9.3.2); undo with the release that vendored.
Built on macOS arm64, with packages that carry no signature.

"Bazel workspaces: what is supported" in the same file follows: the
NuGet row of its table says vendored mode is supported with the Bazel
release in `.bazelversion`, and VEX with a vendored patch as well; the
list of what was tested names the shape; NuGet leaves the known problem
"vendored mode ... the next Bazel build fails" for an entry of its own
(the switch, a package an earlier release vendored, and the two shapes
the checkout does not show); the build that may not fetch is listed
among those that pass on the unpatched package; and "Bazel workspaces:
VEX" names the vendored flow and the cases in which the document says
`not_affected` of a package whose block is not in `MODULE.bazel`.

Tests: nuget::vendored_from_lock now holds the flow above on real Bazel,
with one decline (a checkout that names no release: nothing written, the
build still passes on the upstream package) and the document of `vex`,
which names the package.
nuget::vendored_mode_keeps_the_block_or_declines runs the commands
through the binary without Bazel: the unchanged layout without a hub,
the block and its three undos, the three undos beside a block that was
changed by hand (exit 1, no file changed, then the undo once the block is
deleted), the declines, the eject and the switch.
Unit tests cover the gate row by row, the block's text and its byte-exact
undo, the ledger record, the revert beside a changed block wherever its
record stands, and the path git is asked about for a hub's package. The
`bazel` group of the line budget follows the code, 3337 to 3758.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
main gained the gem_checksums withhold and the scan of a pinned Maven
hub. Two conflicts:

- crates/socket-patch-core/src/bazel/maven.rs, the imports: main's
  `build_maven_purl` is kept; the line-ending helper is no longer
  imported there, because this branch moved the code of the managed
  block, its only user outside the tests, into bazel/block.rs.
- scripts/loc-budget.json: the cap of the `bazel` group is the count of
  the merged tree, 4036.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…atch and a second run

Four defects of vendored mode for a NuGet package of a rules_csharp hub,
each with a unit test.

An undo that leaves `nuget.config` wired took the block out. When the
config was written anew since the vendored run and still names the folder,
the undo keeps the config, the lock pin and the package
(`vendor_lock_entry_drifted`). The block went first all the same, so the
hub read the folder in `nuget.config` as the URL of a feed and the Bazel
build failed on a package that built before the undo. The block now goes
last, after the lock and the config, and stays where the config is left
wired. A lock that cannot be restored leaves the block too, so the build
is as it was for the retry.

A later patch of the same package left the earlier patch's folder in the
block. The config keeps the earlier patch's source with its pattern set
aside; the folder that source names is now taken out of the block when the
later patch is written into it, so the block names the later folder alone
and goes with it on the undo.

The first run for a tag whose `nuget_config` has no package source wrote
a block without nuget.org, and the second run added it: the run gives such
a config nuget.org beside the folder, and the block was read before that.
The block is now read from the tag after the config is written, so a
second run writes no file.

A lock that spells the package's version otherwise than NuGet writes it
(`1.2.3.0` for `1.2.3`) was vendored, and the hub did not find the
package: rules_csharp 0.5.0 asks a feed for the lock's `resolved` as it is
written. Such a package is declined with `vendor_nuget_bazel_workspace`,
by the backend and by the refusal every way in asks first.

The refusal looked for the project's locks before it knew that the project
is in a Bazel workspace; it asks for the workspace first.

Also: one function for the folder of a hub's package in place of the two
places of the backend that wrote it out, and the real-Bazel NuGet legs
start on a push to main that changes the ledger or the artifact paths of
vendored mode (vendor/state.rs, vendor/path.rs), which hold the record of
the block and the place of a hub's package.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@mikolalysenko
Mikola Lysenko (mikolalysenko) marked this pull request as ready for review November 24, 2026 21:33
@cursor

cursor Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

PR Summary

High Risk
Changes vendoring, MODULE.bazel, and Bazel build inputs for NuGet hubs; many refusal gates but incorrect block/feeds logic could break or silently mis-patch builds.

Overview
Vendored NuGet in rules_csharp Bazel workspaces now commits patches where the hub expects them (nested folder layout), wires nuget.config, and appends a managed nuget.from_lock block to the root MODULE.bazel with file: feeds. Where the checkout cannot prove that setup is safe—wrong Bazel/rules_csharp versions, multiple tags, bad rc flags, manual block edits, etc.—vendored mode fails with vendor_nuget_bazel_workspace (same pattern as Composer). --allow-vendored-in-bazel bypasses that and vendors without the block (Bazel build may still break).

Shared bazel/block.rs extracts managed-block find/put/cut logic from Maven; NuGet and Maven both use it. Hosted mode also recognizes vendored blocks and withholds conflicting hosted writes.

Docs/CLI describe the new refusal and layout; CI bazel-nuget paths include vendored flow files; real-Bazel and hermetic e2e tests cover vendored success, decline, revert, and tamper cases.

Reviewed by Cursor Bugbot for commit fadf0e9. Configure here.

@mikolalysenko Mikola Lysenko (mikolalysenko) added the Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review label Nov 24, 2026
The tests of a package of a rules_csharp hub start from a `nuget.config`
that names nuget.org, through a helper of the test module that was held
to Unix for the file-mode tests it was written for. The hub tests are not
held to Unix, so the test build of the core crate failed on Windows
(`cannot find function preexisting_cfg`). The helper is a string and now
builds everywhere. Test only.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@mikolalysenko

Mikola Lysenko (mikolalysenko) commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: review brief for the approver

What it does. In vendored mode, a NuGet package that a rules_csharp nuget.from_lock hub reads used to report success and then break bazel build. Now the package goes into a folder-feed layout, and the root MODULE.bazel gets a managed block: a second from_lock tag that names file: feeds. The undo removes the block byte for byte. Wherever the checkout can't show the block works (other rules_csharp or Bazel releases, more than one plain tag, a remote downloader, and so on), the package is declined with vendor_nuget_bazel_workspace and nothing is written.

Risk: medium. It writes the user's root MODULE.bazel, and some runs that used to exit 0 now exit 1 with a decline. It relies on Bazel reading a relative file: URL from the workspace root, which Bazel does not document; it is gated to rules_csharp 0.5.0 on Bazel 7.7.2, 8.8.2 and 9.3.2. Projects without a hub keep the same path.

Look here

  • bazel/nuget.rs:622-660: gate and feeds, which decide between a block, a decline and the plain vendored path. :507 lists the tag arguments that are allowed.
  • bazel/nuget.rs:822-860: sources and render, which build the block's feeds from the tag's nuget.config and feeds.
  • vendor/nuget_feed.rs:462: bazel_refusals, including the path for a package that is already wired.
  • vendor/nuget_feed.rs:1125 and [:1260-1270](https://github.com/SocketDev/socket-patch/blob/5d958bc2e3b2b88efa68c53d6008cbf55b48b660/crates/socket-patch-core/src/vendor/nuget_feed.rs#L1260-L1270): the revert checks for an edited block before any write, and the block goes last.
  • vendor/nuget_feed.rs:325-340: superseded, for a second patch of the same package.

Verified

  • I reviewed the whole diff: the CRLF and mixed line breaks in block::put and cut; the Maven code moved into bazel/block.rs (no change in behavior); state.json tampering (the record must name the root MODULE.bazel, and the folder comes from the entry's uuid); leaf_to_purl accepting only <id>/<version>/; the decline of a symlinked MODULE.bazel; and the unwind when writing the block fails. CHANGELOG.md is untouched, and I found no debug leftovers.
  • The block's tag does not copy use_public_sources or directory_packages_props. I checked rules_csharp 0.5.0's _nuget_impl (csharp/extensions.bzl): it ANDs use_public_sources across all from_lock tags, so a user's False stays in force, and the hub never reads directory_packages_props. Dropping both is correct.
  • Locally: cargo test -p socket-patch-core --lib -- bazel:: vendor::nuget_feed vendor::refusals vendor::path vendor::state vex::discover::nuget gives 400 passed, 0 failed.
  • CI on 5d958bc: ci-ok and clippy pass. "Bazel patch compatibility (NuGet)" also passes on the head. It runs the real-Bazel NuGet flows, so the description's "not run at the head" note is out of date.

Changes I made. I updated the stale agent-status block in the description. There are no code changes.

Open questions. Not blocking; the description already lists this. Make a second patch of the same package, then vendor --revert it. nuget.config goes back to the first patch's folder, which the second run removed. That is main's behavior without Bazel too, but with a hub, the next bazel build fails on that source. It may be worth a follow-up issue.

Update for 30cb8cce1. I merged main to clear the conflict with the vendored Maven hub (#2207). No other commits were added.

  • with_patch stays on the shared block module, and it is now pub because main needs that.
  • bazel_workspace_refusals dispatches the Maven hub refusals next to the Composer and NuGet ones. The environment switch still lifts only Composer and NuGet.
  • The harness keeps both uncache and main's cached.
  • The bazel line budget is now 4343, the count of the merged tree.

cargo check --workspace --all-targets and all 8547 socket-patch-core lib tests passed locally. ci-ok and clippy pass.

One leg fails: cargo / bazel (9.3.2, e9). It failed again after one re-run, both times in cargo::vendored_from_cargo_with_config on No repository visible as '@platforms'. That's the known flake that #2227 fixes. It's in the Cargo flow, so not this PR's NuGet code, and it's not a required check.


Generated by Claude Code

@mikolalysenko Mikola Lysenko (mikolalysenko) removed the Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review label Nov 25, 2026
Resolve the conflicts with the vendored Maven hub (#2207): keep both the
uncache and cached harness helpers, keep with_patch on the shared block
module and make it public as main needs, dispatch the Maven hub refusals
next to the Composer and NuGet ones, and raise the bazel line budget by
both sides' growth.

Assisted-by: Claude Code:claude-opus-5-5
@mikolalysenko Mikola Lysenko (mikolalysenko) added the Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review label Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants