Skip to content

Read and splice nuget.config through formats::nuget in hosted, vendored and restore #594

Open

Description

[agent] Filed by the scheduled architecture audit routine (ecosystems and formats). Register: E11.

Kind: refactor. Source: review §3.7 #3, Part 3.4 and Part 5.4; register E11 (the NuGet half of E10).

Problem

nuget.config is read and spliced by six private code paths with four different XML rules. Only one of them is a real tokenizer.

Readers of the <packageSources> keys:

Splice anchors and removers:

Config file names are spelled four times: redirect::NUGET_CONFIG_FILE_NAMES,`` vendor::nuget_config::CONFIG_NAMES, `hosted/memory/roots.rs` and `formats/registry.rs`. They haven't drifted yet.

The code paths themselves have drifted. The vendored writer is comment-safe; the hosted writer and reader are not. The reader that upstream restore uses (parse_config) and the one the hosted rewriter uses disagree about the same file.

Symptoms

Impact

Every hosted NuGet bug about comments, self-closing sections or attribute spelling has to be fixed up to three times, and the reader each writer trusts is not the one restore and VEX trust. Size: about 250 production lines of regex and substring scanning.

Proposed change

  1. Extend formats::nuget with a span view: parse_config_spans(text) -> Option<NugetConfigSpans> records the byte offsets that every writer needs (the <configuration> open-tag end, the <packageSources> open/close or self-closing span, the <packageSourceMapping> ditto, the last <clear/> end in each, and each <add>/<packageSource> element span with its key). It is computed by the same tokenizer as parse_config, so comments and CDATA are never anchors.
  2. Hosted add_nuget_source and rewrite_nuget take the keys and anchors from it. Delete nuget_package_source_keys, the NUGET_PACKAGE_SOURCES_REGION_RE / NUGET_ADD_KEY_RE statics, the regexes in insert_nuget_source / nuget_mapping_open_end / add_nuget_source, and nuget_after_last_clear's comment masker.
  3. Hosted restore remove_source and vendored excise_source_mapping / parse_config_source_keys use the element spans. Delete both private attr_values and blank_comments (if no other caller remains).
  4. One formats::nuget::CONFIG_FILE_NAMES; delete the other three lists.

This can land as two PRs if it's too big: (a) the span view plus hosted (closes #561 and #585), (b) vendored and restore.

Size and scope

formats/nuget/mod.rs, patch/redirect/mod.rs (NuGet section only), patch/redirect/upstream/nuget.rs, vendor/nuget_feed.rs, vendor/nuget_config.rs, hosted/memory/roots.rs. About +200 / −300 production lines. Out of scope: Maven/Gradle XML (rest of E10) and the packages.lock.json walks (#593).

Acceptance criteria

Dependencies

Supersedes the narrower fix in #561 (either can land first; if #561 lands first, this deletes its remaining regex). Touches rewrite_nuget like #593, so sequence the two. Blocks the Maven/Gradle half of E10.

Activity

  1. added
    pm:nuget
    arch-audit
    refactor
    on Oct 2, 2026
  2. added a commit that references this issue on Oct 2, 2026
  3. mikolalysenko commented on Oct 2, 2026

    @mikolalysenko
    CollaboratorAuthor

    [agent] Triage: priority:p3 (NuGet refactor). The hosted half (part (a): span view + hosted rewriter, closing #561 and #585) is addressed by open PR #597. Vendored and restore (part (b)) remain.


    Generated by Claude Code

  4. mikolalysenko commented on Oct 3, 2026

    @mikolalysenko
    CollaboratorAuthor

    [agent] Shares root cause with #685: the vendored nuget.config writer (vendor/nuget_feed.rs) matches section close tags as literal substrings, so </packageSources > gets a duplicate section. Routing it through formats::nuget as proposed here fixes that symptom too. Will be fixed together.


    Generated by Claude Code

  5. mikolalysenko commented on Oct 5, 2026

    @mikolalysenko
    CollaboratorAuthor

    [agent] Re-checked on main @ 4646693. The hosted part is done: #597 deleted nuget_package_source_keys, and hosted routing and splice anchors now go through formats::nuget::parse_config (used at redirect/mod.rs#L5084, restore and VEX).

    What remains is the vendored side. vendor/nuget_feed.rs still keeps its own reader, parse_config_source_keys,`` and its own find-based splice anchors in `build_config_edit` (`visible.find("")`, the self-closing span at L1114). It doesn't use the insertion spans that #597 added to the shared model. This issue now covers moving those onto `formats::nuget` and deleting the vendored reader.


    Generated by Claude Code

  6. mikolalysenko commented on Oct 9, 2026

    @mikolalysenko
    CollaboratorAuthor

    [agent] Claiming this issue for the architecture refactor routine (highest leverage: the vendored nuget.config writer is the last reader outside formats::nuget::parse_config in a free file; routing it through the shared tokenizer deletes blank_comments, parse_config_source_keys, attr_value and self_closing_package_sources and fixes #685). Slice: vendored writer and its idempotence check only. Hosted restore remove_source (upstream/nuget.rs, in a maintainer PR) and the vendored revert excision remain. Branch: arch-refactor/594-vendored-nuget-config. Claim-ID: 2026-10-09T14:56:13Z-ead28c


    Generated by Claude Code

  7. mikolalysenko commented on Oct 9, 2026

    @mikolalysenko
    CollaboratorAuthor

    [agent] Draft PR for the vendored slice (it also fixes #685): #1288.


    Generated by Claude Code

  8. mikolalysenko commented on Oct 12, 2026

    @mikolalysenko
    CollaboratorAuthor

    [agent] Janitor: releasing the stale claim Claim-ID: 2026-10-09T14:56:13Z-ead28c. slices merged, last #1288 on 2026-10-09 19:35Z; no open PR for the rest of this issue, and the claimer has been silent since 2026-10-09T15:01:02Z (more than 48h). Removing agent:claimed so another fixer can pick up what remains.


    Generated by Claude Code

  9. added a commit that references this issue on Nov 26, 2026
  10. mikolalysenko commented on Nov 28, 2026

    @mikolalysenko
    CollaboratorAuthor

    [agent] The restore half that is left of this issue now has an input on which the copies disagree (lead L75 of the architecture refactor routine). Ran on main at cfc870f.

    The input: a nuget.config whose <packageSources> holds the Socket source before a <clear />. v4.0.0's hosted scan writes exactly that in any project whose config starts its sources with <clear />:

    <packageSources>
      <add key="socket-patch-67016c7e-…" value="…/patch-registry/nuget/…/index.json" />
      <clear />
      <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    </packageSources>
    <packageSourceMapping>
      <packageSource key="socket-patch-67016c7e-…">
        <package pattern="Daqu2.Waab9" />
      </packageSource>
    </packageSourceMapping>

    What each copy says of it, with the benchmark harness's NuGet project (socket-patch-bench serve nuget/hosted), hosted by the v4.0.0 binary and then run with main:

    $ socket-patch list            # and rollback, and remove <purl>: the same message, exit 1
    Error: nuget.config wire(s) Socket-hosted patches that cannot be attributed to one package version […]
    (patched_ref_invalid: nuget.config: <packageSourceMapping> routes packages to socket-patch-67016c7e-…,
    which no <packageSources> entry defines; NuGet cannot restore from it); reconcile the lockfiles
    (re-run `socket-patch scan --mode hosted`) or restore them from version control (`git checkout -- nuget.config`)
    $ socket-patch scan            # the remedy the message names
    Switched 1 package to hosted patches; rewrote 1 file.                    # exit 0
    $ socket-patch list
      Mode: hosted (wired in nuget.config)                                   # exit 0
    $ socket-patch rollback
    Error: Cannot restore pkg:nuget/daqu2.waab9@0.14.6 to its upstream registry entry: nuget.config: the config
    does not define the Socket source socket-patch-67016c7e-… exactly once; restore it from version control
    instead (`git checkout -- nuget.config`)                                 # exit 1
    

    The scan left the file with the source twice and the mapping twice:

    <packageSources>
      <add key="socket-patch-67016c7e-…" value="…" />
      <clear />
      <add key="socket-patch-67016c7e-…" value="…" />
      <add key="nuget.org" … />
    </packageSources>
    <packageSourceMapping>
      <packageSource key="socket-patch-67016c7e-…"><package pattern="Daqu2.Waab9" /></packageSource>
      <packageSource key="socket-patch-67016c7e-…"><package pattern="Daqu2.Waab9" /></packageSource>
      <packageSource key="nuget.org"><package pattern="*" /></packageSource>
    </packageSourceMapping>

    So the three copies of "which <add> defines the Socket source" give three answers:

    • formats::nuget::parse_config, which list, vex and the undo commands find pins through (crates/socket-patch-core/src/vex/discover/nuget.rs, socket_sources, lines 167 to 195), honors <clear /> and counts two identical definitions as one. It refuses the first file and accepts the second.
    • The hosted rewriter adds its source after the last <clear /> and a new mapping element. It neither removes the cleared <add> nor reuses the mapping that is already there.
    • The restore's remove_source (crates/socket-patch-core/src/patch/redirect/upstream/nuget.rs, lines 52 to 72) counts every <add> of the key in the raw text and refuses unless there is exactly one. It refuses the second file, the one the scan has just written.

    The result for the user: no command of main undoes this project. rollback and remove refuse and name a rescan, the rescan writes a file they refuse again, and the committed version git checkout brings back is the first file. The second file is also checked by nothing but this tool: this sandbox has no dotnet, so I could not run a restore over the doubled mapping.

    Reach: I found the first file only as v4's output (it writes its <add> as the first child of <packageSources>, main writes after the <clear />). NuGet ignores a source defined before <clear />, so that project's restore was already failing under v4. That makes this low in harm, and it is the migration case: someone who upgrades to get out of that state. A hand edit or a merge that leaves the source on both sides of a <clear /> gets the same refusal.

    What would end the class is what this issue already proposes for restore: remove_source reading the config through parse_config's spans instead of its own regex, removing every definition of the pin's key, and the rewriter replacing a cleared definition and reusing an existing mapping element instead of adding beside them.


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent:triagedarch-auditpm:nugetpriority:p3refactor

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions