Skip to content

Fix previous-nuget under native AOT: read NuGet feed JSON with System.Text.Json - #63

Merged
Mpdreamz merged 3 commits into
mainfrom
fix/previous-nuget-aot
Nov 12, 2026
Merged

Mpdreamz merged 3 commits into
mainfrom
fix/previous-nuget-aot

Conversation

@Mpdreamz

@Mpdreamz Mpdreamz commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #62

The previous-nuget provider crashed in the native-AOT build (0.18.0 onwards) with JsonSerializationException: Unable to find a constructor to use for type NuGet.Packaging.PackageDependencyGroup.

Root cause

PreviousNugetLocator calls PackageMetadataResource.GetMetadataAsync, and NuGet.Protocol deserialises the registration pages it fetches with Newtonsoft.Json by reflection. In the AOT binary, trimming removes the constructors that reflection binds to. The csproj already rooted NuGet.Protocol and Newtonsoft.Json (TrimmerRootAssembly), but PackageDependencyGroup lives in NuGet.Packaging, which was still trimmed.

Fix

Rather than root another assembly, this turns on the feature switch that NuGet documents for AOT consumers:

<RuntimeHostConfigurationOption Include="NuGet.UseSystemTextJsonDeserialization" Value="true" Trim="true" />

With the switch on, NuGet.Protocol reads feed JSON through its source-generated System.Text.Json path, and Trim="true" lets the trimmer drop the Newtonsoft.Json path. That removes the reflection path instead of keeping it alive:

  • the two TrimmerRootAssembly items are removed;
  • NuGet.Protocol's IL2026 trim warnings (ServiceIndexResourceV3, RepositorySignatureResource, plugins) are gone from the AOT publish. The only IL warnings left come from the vendored JustDecompile assemblies;
  • the osx-arm64 binary shrinks from 26 MB to 18 MB.

The switch also applies to the framework-dependent any build, so both builds run the same deserialisation code.

Regression check

A new ./build.sh smokeaot target publishes the native-AOT binary for the current machine and runs diff 'previous-nuget|Proc|0.18.0' 'nuget|Proc|0.20.0' --target Proc. It fails unless the output shows the previous release (0.17.0) and a suggested version change. CI's aot-pack job runs it after packing, on every RID it builds. AGENTS.md now describes the switch in place of the old rooting note.

Not in this PR

#57 is a separate bug in NuGetAssemblyProvider, not the deserialisation path. Both providers unpack into differ/nuget/<id>/<spec version>, and previous-nuget uses the current version as that folder name. So previous-nuget|X|v and nuget|X|v end up side by side in one folder. NuGetPackage then treats each as a dependency of the other, and CopyAssemblies copies one version's dlls over the other's lib/, which is why the diff comes back empty. I reproduced it here: previous-nuget|Proc|0.20.0 against nuget|Proc|0.20.0 reports "No diff" with this fix in place. Keyed by provider name, the folders would no longer collide. It is left for its own PR.

Verify (local, macOS arm64, .NET SDK 11.0.100)

  • Reproduced previous-nuget crashes in 0.18: no constructor for PackageDependencyGroup #62 with the AOT publish of origin/main (dotnet publish -f net10.0 -r osx-arm64): previous-nuget|Proc|0.20.0 threw the reported exception.
  • With the fix, the same binary finds 47 versions of Proc and installs 0.18.0, the previous listed release, without error. previous-nuget|Proc|0.18.0 against nuget|Proc|0.20.0 prints the real API changes and Suggested version change: Major.
  • ./build.sh smokeaot -s passes. To check the check, I restored the old csproj and re-ran it: it failed with the previous-nuget crashes in 0.18: no constructor for PackageDependencyGroup #62 exception, then passed again once the fix was back.
  • mise run ci: build, pack, validate and the API diff (which runs previous-nuget on the JIT build with the switch on) all pass, and curb check reports 0 changes.
  • mise run check (curb, zizmor): no findings. actionlint passed, run with shellcheck disabled because shellcheck is not installed here.
  • Not verified locally: the smoke step on the linux and windows runners. It only runs there in CI.

🤖 Generated with Claude Code

Mpdreamz and others added 2 commits November 12, 2026 10:17
…der AOT

NuGet.Protocol deserialises registration pages with Newtonsoft.Json by
reflection. In the native-AOT binary, trimming removes the constructors
that reflection binds to, so previous-nuget failed with "Unable to find a
constructor to use for type NuGet.Packaging.PackageDependencyGroup".
Rooting NuGet.Protocol and Newtonsoft.Json did not cover it, because the
type lives in NuGet.Packaging.

Turn on NuGet's NuGet.UseSystemTextJsonDeserialization feature switch
instead, which NuGet documents for AOT consumers: it uses its
source-generated System.Text.Json path, and Trim="true" lets the trimmer
drop the Newtonsoft.Json path. The two TrimmerRootAssembly items are no
longer needed, NuGet's IL2026 trim warnings are gone, and the osx-arm64
binary shrinks from 26 MB to 18 MB.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A new smokeaot build target publishes the AOT binary for the current
machine and diffs previous-nuget|Proc|0.18.0 against nuget|Proc|0.20.0
with it, failing unless it finds 0.17.0 and suggests a version change.
The aot-pack job runs it on every RID, since the framework-dependent
build cannot catch what trimming breaks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Mpdreamz Mpdreamz added the bug A defect fixed (patch) label Nov 12, 2026
Make.Fs 0.25 accepts the bare flag as well, so this works before and
after the dependency bump.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Mpdreamz
Mpdreamz merged commit 971a240 into main Nov 12, 2026
5 checks passed
@Mpdreamz
Mpdreamz deleted the fix/previous-nuget-aot branch November 12, 2026 09:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug A defect fixed (patch)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

previous-nuget crashes in 0.18: no constructor for PackageDependencyGroup

1 participant