Skip to content

NuGet.Protocol should not use Newtonsoft.Json when AoT trimmed - #7493

Merged
zivkan merged 4 commits into
devfrom
dev-zivkan-protocol-aot-no-newtonsoft-json
Jun 23, 2026
Merged

zivkan merged 4 commits into
devfrom
dev-zivkan-protocol-aot-no-newtonsoft-json

Conversation

@zivkan

@zivkan zivkan commented Jun 19, 2026

Copy link
Copy Markdown
Member

Bug

Fixes: follow up on other NuGet.Protocol AoT work

Description

When running dotnet publish on an AoT app, you can use /p:IlcGenerateMapFile=true /p:IlcGenerateDgmlFile=true to generate extra diagnostic output which can be used to analyze what's contributing to the size of the executable, and the dependency graph of all the methods. This was used to determine that several megabytes of Newtonsoft.Json code was being included, and what parts of NuGet.Protocol were calling it (bringing it in).

Fixing the code reduces the AoT trimmed app by about 6 MB. There's no longer any Newtonsoft.Json code in the symbol map, but there is about 4.5 KB of metadata because some of NuGet.Protocol's public APIs return types like JObject, even if those APIs are never called.

There are basically two groups of changes:

  • Data types related to plugins (credential providers) used JObject to create a json string in their ToString() implementations. These were switched over to System.Text.Json equivalents, even when the feature flag is not enabled. It's low risk because it's not parsing json provided by 3rd parties.
  • Several of the remaining methods changed had return types of Newtonsoft.Json types, like JObject. Therefore, it's not possible to create Stj and Nj methods and have the existing public API call it. Instead, a new method with a strongly typed return type needs to be created, leading to what looks like a lot of duplicated code.

PR Checklist

  • Meaningful title, helpful description and a linked NuGet/Home issue
  • Added tests N/A, more of a refactoring. Existing tests that test the feature flag should already follow the new code paths.
  • Link to an issue or pull request to update docs if this PR changes settings, environment variables, new feature, etc. N/A

@nkolev92 nkolev92 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's some compile errors, so that needs fixed.

100% of my feedback is var usage for readability. It would've made it easier to read for me.

The tests part, I was surprised to see none.

HttpStreamValidationTests could use some coverage.

DependencyInfoResourceV3 doesn't seem to have direct tests either, which is the ResolverMetadataClient.
DownloadResourceV3 similarly doesn't seem exercised mcuh, if at all.

Finally, RegistrationResourceV3 is missing some direct coverage, but maybe covered through other.

@zivkan
zivkan force-pushed the dev-zivkan-protocol-aot-no-newtonsoft-json branch from a348049 to 0a1d0e3 Compare June 21, 2026 10:37
jebriede
jebriede previously approved these changes Jun 23, 2026

@jebriede jebriede left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving with one suggestion.

@nkolev92 nkolev92 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding those additional tests.

Much more robust now.

@zivkan
zivkan merged commit 8eee940 into dev Jun 23, 2026
20 of 21 checks passed
@zivkan
zivkan deleted the dev-zivkan-protocol-aot-no-newtonsoft-json branch June 23, 2026 23:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants