Repository navigation
Description
NuGet Product Used
NuGet SDK
Product Version
7.6.0+
Worked before?
6.12.1
Impact
Other
Repro Steps & Context
Since 7.x the NuGet.Core assemblies ship with IsTrimmable=True, but the default deserialization path is still Newtonsoft.Json reflection (the NuGet.UseSystemTextJsonDeserialization feature switch defaults to off). So when a consumer publishes with PublishTrimmed=true + TrimMode=partial, ILLink now trims NuGet.Protocol and strips members only Newtonsoft's reflection reaches, like the parameterless constructor on RepositoryCertificateInfo.
Any source whose service index advertises RepositorySignatures/5.0.0 (nuget.org does) hits this eagerly, since repository signature info is fetched before version/download resources are served. A trimmed app pointed at nuget.org therefore fails on basically every operation.
NuGet.Protocol.Core.Types.FatalProtocolException: Unable to get repository signature information for source https://api.nuget.org/v3-index/repository-signatures/5.0.0/index.json.
---> Newtonsoft.Json.JsonSerializationException: Unable to find a constructor to use for type NuGet.Protocol.RepositoryCertificateInfo. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'signingCertificates[0].fingerprints'.
The same app published trimmed against 6.12.1 works, because 6.x had no IsTrimmable metadata and partial trimming left it alone. Nothing changed on the consumer side except the package version.
Repro Steps
dotnet new console, replace.csprojandProgram.cswith the code belowdotnet runworks and printsAllRepositorySigned: Truedotnet publish -r win-x64 --self-contained(or whichever architecture)- Run the published exe and it throws the exception below
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<PublishTrimmed>true</PublishTrimmed>
<TrimMode>partial</TrimMode>
<NoWarn>$(NoWarn);IL2026;IL2104</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NuGet.Protocol" Version="7.6.0" />
</ItemGroup>
</Project>using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
var repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
var resource = await repository.GetResourceAsync<RepositorySignatureResource>();
Console.WriteLine($"AllRepositorySigned: {resource.AllRepositorySigned}");Expected: repository signature information deserializes, same as the untrimmed build.
Actual:
Unhandled exception. NuGet.Protocol.Core.Types.FatalProtocolException: Unable to get repository signature information for source https://api.nuget.org/v3-index/repository-signatures/5.0.0/index.json.
---> Newtonsoft.Json.JsonSerializationException: Unable to find a constructor to use for type NuGet.Protocol.RepositoryCertificateInfo. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'signingCertificates[0].fingerprints'.
Notes
The failure is identical with default full trimming (no TrimMode set). Partial mode is used above because it makes the problem clearer, the only assemblies trimmed in this app are the ones NuGet.Protocol 7.x itself declares trim-safe via IsTrimmable.
This is related to #14845 and #15008, but it is a shipped issue. The IL2026 suppression in RepositorySignatureResourceProvider says the Newtonsoft branch is unreachable when the feature switch is true. The switch defaults to false, so that branch is exactly what runs in a default trimmed publish.
The validation approach in #14845 (TrimmerRootAssembly for every NuGet assembly, like the existing NuGet.AotCompatibility.TestApp) can't catch this class of bug. Rooting preserves the reflection-only members that partial trimming strips in a real consumer app. A test app with no rooting and default switches catches it immediately.
I have a fix plus a trim-compatibility test app that fails before the fix and passes after. PR incoming.
For anyone hitting this, the workaround is to root the NuGet assemblies yourself (<TrimmerRootAssembly Include="NuGet.Protocol" /> etc.) or point at a source that doesn't advertise repository signatures. For us, pointing to the v2 endpoint worked - https://www.nuget.org/api/v2
Verbose Logs
NuGet.Protocol.Core.Types.FatalProtocolException: Unable to get repository signature information for source https://api.nuget.org/v3-index/repository-signatures/5.0.0/index.json.
---> Newtonsoft.Json.JsonSerializationException: Unable to find a constructor to use for type NuGet.Protocol.RepositoryCertificateInfo. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'signingCertificates[0].fingerprints'.
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject(JsonReader reader, JsonObjectContract objectContract, JsonProperty containerMember, JsonProperty containerProperty, String id, Boolean& createdFromNonDefaultCreator)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
at Newtonsoft.Json.Linq.JToken.ToObject(Type objectType, JsonSerializer jsonSerializer)
at Newtonsoft.Json.Linq.JToken.ToObject[T](JsonSerializer jsonSerializer)
at NuGet.Protocol.JsonExtensions.FromJToken[T](JToken jtoken)
at NuGet.Protocol.RepositorySignatureResource.<>c.<.ctor>b__9_0(JObject p)
at System.Linq.Enumerable.IEnumerableWhereSelectIterator`2.MoveNext()
at NuGet.Protocol.RepositorySignatureResource..ctor(JObject repoSignInformationContent, SourceRepository source)
at NuGet.Protocol.RepositorySignatureResourceProvider.<>c__DisplayClass2_0.<<GetRepositorySignatureResourceAsync>b__0>d.MoveNext()
--- End of stack trace from previous location ---
at NuGet.Protocol.HttpSource.<>c__DisplayClass15_0`1.<<GetAsync>b__0>d.MoveNext()
--- End of stack trace from previous location ---
at NuGet.Common.ConcurrencyUtilities.ExecuteWithFileLockedAsync[T](String filePath, Func`2 action, CancellationToken token)
at NuGet.Common.ConcurrencyUtilities.ExecuteWithFileLockedAsync[T](String filePath, Func`2 action, CancellationToken token)
at NuGet.Protocol.HttpSource.GetAsync[T](HttpSourceCachedRequest request, Func`2 processAsync, ILogger log, CancellationToken token)
at System.Runtime.ExceptionServices.InternalCalls.<RhpSfiNext>g____PInvoke|1_0(StackFrameIterator* __pThis_native, UInt32* __uExCollideClauseIdx_native, Boolean* __fUnwoundReversePInvoke_native, Boolean* __fIsExceptionIntercepted_native)
at NuGet.Protocol.RepositorySignatureResourceProvider.GetRepositorySignatureResourceAsync(SourceRepository source, ServiceIndexEntry serviceEntry, ILogger log, CancellationToken token)
--- End of inner exception stack trace ---
at NuGet.Protocol.RepositorySignatureResourceProvider.GetRepositorySignatureResourceAsync(SourceRepository source, ServiceIndexEntry serviceEntry, ILogger log, CancellationToken token)
at NuGet.Protocol.RepositorySignatureResourceProvider.TryCreate(SourceRepository source, CancellationToken token)
at NuGet.Protocol.Core.Types.SourceRepository.GetResourceAsync[T](CancellationToken token)
at NuGet.Protocol.HttpFileSystemBasedFindPackageByIdResourceProvider.TryCreate(SourceRepository sourceRepository, CancellationToken token)
at NuGet.Protocol.Core.Types.SourceRepository.GetResourceAsync[T](CancellationToken token)
at Contrast.AgentUpgrader.Service.Wrappers.SourceRepositoryWrap.GetAllVersionsAsync(String id, CancellationToken cancellationToken)
at Contrast.AgentUpgrader.Service.Core.Downloaders.NuGetDownloader.GetLatestVersion(CancellationToken cancellationToken, Nullable`1 majorVersion)
Activity