Skip to content

Analyzers flow through ProjectReference into Csc despite ExcludeAssets="analyzers" (non-deterministic) #57448

Done

Description

@sy11a

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 direct PackageReference,
  • ExcludeAssets="analyzers" on the ProjectReference itself,
  • (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 run

Observed 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:

  1. the consuming project's project.assets.json has no analyzers section for
    ModelContextProtocol.Core/2.4.1 (restore honored the exclusion),
  2. Task "ResolvePackageAssets" output item ResolvedAnalyzers= nevertheless
    lists <NuGet cache>/modelcontextprotocol.core/2.4.1/analyzers/dotnet/cs/ModelContextProtocol.Analyzers.dll,
  3. ResolveLockFileAnalyzers adds it to @(Analyzer),
  4. the consuming project's Csc task parameter Analyzers= 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" +
    OutputItemType metadata, SkipGetTargetFrameworkProperties="true", replacing
    the ProjectReference with an in-proc MSBuild task that builds the referenced
    project first (2/5), and with the referenced project's PrivateAssets="analyzers"
    (1/5 still flowed). Only removing the ProjectReference entirely 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

  1. added
    untriaged
    on Dec 5, 2026
  2. locked and limited conversation to collaborators on Dec 5, 2026
  3. unlocked this conversation on Dec 5, 2026
  4. removed
    untriaged
    on Dec 5, 2026
  5. github-actions commented on Dec 5, 2026

    @github-actions
    Contributor

    🎯 Agentic Issue Triage

    Triage Assessment:

    • 🏷️ Labels: Area-NuGet Bug
    • 💻 Assignment: @zivkan | @dotnet/nuget-team
    • 🟨 Confidence: medium - 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.

    Generated by 🏷️ Issue Triage for #57448 · copilot · auto (Claude Sonnet 5.5) · ~19 AIC · ⌖ ~2 AIC · ⊞ 15.5K · ◷

  6. dotnet-policy-service commented on Dec 6, 2026

    @dotnet-policy-service
    Contributor

    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!

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions