Repository navigation
Description
Describe the bug
A package's analyzers keep reaching a project's C# compilation through a
ProjectReference even when every documented exclusion lever is applied:
ExcludeAssets="analyzers"on the consuming project's directPackageReference,ExcludeAssets="analyzers"on theProjectReferenceitself,- (partially effective)
PrivateAssets="analyzers"on the package reference inside the referenced project.
The exclusion is honored in the restore graph — the consuming project's
obj/project.assets.json contains no analyzers section for the package — yet
the ResolvePackageAssets task still emits the analyzer dll as an
ResolvedAnalyzers output item, ResolveLockFileAnalyzers promotes it to
@(Analyzer), and Csc receives it in its Analyzers parameter. The effect is
non-deterministic: clean rebuilds (deleting bin/obj of both projects every
time, full restore each run) of identical inputs load the analyzer in ~2/8 to 4/5 of
builds depending on which of the levers are applied.
This fails closed policies that key on "which analyzers is this project compiled
with" — in our case a gate that refuses suppressor exemptions in any project whose
compilation includes an unsigned third-party analyzer: the analyzer appears in the
compiler invocation although the project explicitly excluded it, and the refusal
fires intermittently.
To Reproduce
Two SDK-style projects, .NET 11 (net11.0), SDK 11.0.100, linux-x64:
lib/lib.csproj (referenced project):
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net11.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ModelContextProtocol.Core" Version="2.4.1" />
</ItemGroup>
</Project>repro.csproj (referencing project — note the exclusion on both levers):
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net11.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ModelContextProtocol.Core" Version="2.4.1" ExcludeAssets="analyzers" />
<ProjectReference Include="lib/lib.csproj" ReferenceOutputAssembly="false" OutputItemType="BinaryUnderTest" ExcludeAssets="analyzers" />
</ItemGroup>
</Project>ModelContextProtocol.Core 2.4.1 is used because it ships an analyzer whose dll
name is unique (analyzers/dotnet/cs/ModelContextProtocol.Analyzers.dll), which
makes the observation easy to grep; we believe the mechanism is package-agnostic.
Then, repeatedly:
rm -r bin obj lib/bin lib/obj
dotnet restore repro.csproj
dotnet build repro.csproj -v:diag -flp:'logfile=diag.log;verbosity=diagnostic'
grep -c "ModelContextProtocol.Analyzers" diag.log # presence inside the Csc Analyzers parameter varies per runObserved across clean rebuilds: the analyzer reaches the referencing project's
Csc Analyzers parameter in 2 of 8 runs in this exact shape, 4 of 5 runs
with only the direct-PackageReference exclusion (plain ProjectReference),
2 of 5 with ExcludeAssets="analyzers" on the ProjectReference alone. In the
failing runs the diagnostic log shows:
- the consuming project's
project.assets.jsonhas noanalyzerssection for
ModelContextProtocol.Core/2.4.1(restore honored the exclusion), Task "ResolvePackageAssets"output itemResolvedAnalyzers=nevertheless
lists<NuGet cache>/modelcontextprotocol.core/2.4.1/analyzers/dotnet/cs/ModelContextProtocol.Analyzers.dll,ResolveLockFileAnalyzersadds it to@(Analyzer),- the consuming project's
Csctask parameterAnalyzers=contains it.
Expected: once the consuming project's restore graph carries no analyzers for the
package (and/or the ProjectReference excludes analyzers), no build of that
project — deterministic or not — should see the analyzer in its Csc invocation.
Further technical details
-
SDK 11.0.100 (
/usr/lib64/dotnet/sdk), NuGet assets format version 4, linux-x64. -
The nondeterminism persisted with:
ReferenceOutputAssembly="false"+
OutputItemTypemetadata,SkipGetTargetFrameworkProperties="true", replacing
theProjectReferencewith an in-procMSBuildtask that builds the referenced
project first (2/5), and with the referenced project'sPrivateAssets="analyzers"
(1/5 still flowed). Only removing theProjectReferenceentirely and having no
same-session sibling build of the referencing project produced a deterministic
"analyzer absent" result. -
Suspected race: the consumer's package resolution merges analyzer assets from the
referenced project's resolution (project-graph walk), and whether that walk sees
the exclusion depends on build scheduling. -
Included workloads: none
Activity
github-actions commentedon Dec 5, 2026
🎯 Agentic Issue Triage
Triage Assessment:
@zivkan| @dotnet/nuget-teammedium- Analyzer asset resolution relates to NuGet assets, but the ResolvePackageAssets task could also fit another area.Similar issues: #1212 (
potentially related) #50170 (potentially related)➡️ Summary: Analyzers still reach Csc non-deterministically through a ProjectReference despite ExcludeAssets="analyzers" on both references. Investigate how ResolvePackageAssets merges analyzer assets from referenced projects.
dotnet-policy-service commentedon Dec 6, 2026
Thanks for creating this issue! We believe this issue is related to NuGet tooling, which is maintained by the NuGet team. Thus, we closed this one and encourage you to raise this issue in the NuGet repository instead. Don’t forget to check out NuGet’s contributing guide before submitting an issue!
If you believe this issue was closed out of error, please comment to let us know.
Happy Coding!