Skip to content

KTSU0006 fails the build for System.Text.Json and other framework assemblies when an old package pulls them in transitively, though the compiler uses the shared framework's copy #99

Open

Description

@matt-edmondson

What happens

KTSU0006 treats a type as coming from a transitive package even when the shared framework supplies that assembly and won conflict resolution. So on net8.0 or net9.0, using System.Text.Json (or System.Memory, Microsoft.Bcl.*, …) fails the build as soon as any dependency has an older package of it anywhere in its graph.

The only way to clear the error is to add a direct PackageReference at the version the old dependency declared. That reference has no effect on compilation, because the framework's copy still wins. It does go into the nuspec, though, so every consumer then inherits a dependency on an old and often vulnerable package version.

Repro (ktsu.Sdk 2.31.3-pre.1)

<Project Sdk="Microsoft.NET.Sdk">
  <Sdk Name="ktsu.Sdk" />
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <TargetFrameworks></TargetFrameworks>
    <!-- only to get past the audit warnings for the old transitive System.Text.Json -->
    <NoWarn>$(NoWarn);NU1903;NU1902;NU1901;NU1904;CS1591</NoWarn>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
    <PackageReference Include="Polyfill" Version="11.4.5" PrivateAssets="all" />
  </ItemGroup>
</Project>
namespace ktsu.Repro;

public static class Probe
{
	public static string Write(int value) => System.Text.Json.JsonSerializer.Serialize(value);
}

dotnet build:

Repro/Class1.cs(5,55): error KTSU0006: Type or member from transitive package 'System.Text.Json' is used directly; add a PackageReference to 'System.Text.Json'

But the assembly the compiler actually references is the framework's:

$ dotnet msbuild -t:FindReferenceAssembliesForReferences -getItem:ReferencePathWithRefAssemblies | grep System.Text.Json
  "FullPath": ".../microsoft.netcore.app.ref/8.0.33/ref/net8.0/System.Text.Json.dll"

and obj/Debug/net8.0/ktsu.transitive-package-map.g.txt still maps it to the package:

System.Text.Json|System.Text.Json|6.0.0

If you remove the Microsoft.Extensions.Configuration.Json reference, the same code builds cleanly. Adding <PackageReference Include="System.Text.Json" Version="6.0.0" /> also clears the error, and it publishes a dependency on System.Text.Json 6.0.0, which has known advisories.

Why

_KtsuGenerateTransitivePackageInputs (Sdk/Sdk.targets:227-228) builds the assembly-to-package map from @(ResolvedCompileFileDefinitions), which lists every NuGet compile asset before ResolvePackageFileConflicts runs. When the framework's copy wins, the package's copy is dropped from the references, but it stays in the map. The analyzer (Sdk.Analyzers/TransitivePackageUsedDirectlyAnalyzer.cs:127) matches only on the assembly's simple name (assembly.Identity.Name), so the framework's System.Text.Json gets attributed to the package.

On net10.0 the same project probably stays quiet, because NuGet package pruning removes framework-supplied packages from the graph before they reach ResolvedCompileFileDefinitions. That leaves exactly the lower targets of the SDK's default net10.0;net9.0;net8.0;netstandard2.0;netstandard2.1 list exposed. Since #44, those are the targets where adding such a reference can also override the framework the wrong way.

Suggested fix

Build the map from the references that survived conflict resolution instead of from the raw NuGet assets. Two options:

  • Run the target after ResolvePackageFileConflicts (or ResolveAssemblyReferences) and take @(Reference) / @(ReferencePath) items that still carry NuGetPackageId. A framework-won assembly then has no package metadata, so it never enters the map.
  • Or, in the analyzer, compare the symbol's assembly identity version and path against the map entry, and skip the match when the referenced assembly isn't the package's file.

The first option is simpler, and it keeps the analyzer a pure name lookup.

Acceptance criteria

  • The repro above builds with no KTSU0006 on net8.0.
  • A package that does win conflict resolution (for example System.Text.Json 10.x pulled in transitively on net8.0) is still reported.
  • An AnalyzerTriggerTests-style case, or a new example, covers the framework-wins case, so this can't regress.

Activity

  1. added
    bug
    on Nov 5, 2026
  2. matt-edmondson commented on Nov 5, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    Category: Bug
    Priority: High
    Area: Sdk analyzers: Sdk/Sdk.targets (_KtsuGenerateTransitivePackageInputs) and Sdk.Analyzers/TransitivePackageUsedDirectlyAnalyzer.cs
    Suggested assignment: Sdk analyzers maintainer, the same owner as the KTSU0006 work in #44

    Why High: The error fails the build, not just warns, on net8.0 and net9.0, which are default targets of the SDK. It triggers for common framework assemblies (System.Text.Json, System.Memory, Microsoft.Bcl.*) as soon as any dependency has an older package of them in its graph. The only workaround makes things worse: a direct PackageReference at the old version goes into the nuspec, so every consumer inherits a dependency on a vulnerable package. Any ktsu-dev library that picks up such a dependency will either break or publish that dependency.

    Duplicates / related:

    In progress? No open PR covers this. Sdk #96–#98 are unrelated.

    Notes: The issue includes a minimal repro, a root-cause analysis and acceptance criteria, so it is ready to pick up. The first suggested fix fits best: build the map from references that survived ResolvePackageFileConflicts (@(ReferencePath) items that still carry NuGetPackageId), which keeps the analyzer a pure name lookup. The regression test should cover both directions: framework wins (no diagnostic), and a newer package wins on net8.0 (still reported).


    Generated by Claude Code

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    analyzersbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions