Skip to content

KTSU0006's code fix can silently downgrade a consumer at runtime; add an analyzer for the framework-override case #44

Closed

Description

@matt-edmondson

What

TransitivePackageUsedDirectlyCodeFixProvider resolves KTSU0006 by adding a PackageReference (and, under central package management, a PackageVersion) at the version the analyzer observed:

// TransitivePackageUsedDirectlyCodeFixProvider.cs:82
string? versionForReference = centrallyManaged ? null : packageVersion;

For a package that also ships in the shared framework — System.Text.Json, System.Memory, and friends — that fix can produce a library that throws FileNotFoundException for every consumer on a lower target framework, and nothing in the build says so.

The mechanism

ktsu-dev/GitIntegration hit this and documented it at length. Adding

<PackageReference Include="System.Text.Json" PrivateAssets="all" />

to a project multi-targeting net10.0;net9.0 resolved the package's real 10.0.2 assembly on net9.0, overriding that framework's own 9.0.x copy. PrivateAssets="all" correctly kept the package out of the nupkg — so the consumer resolves nothing, runs on a 9.0.x shared framework carrying System.Text.Json 9.0.0.0, and meets a compiled reference to 10.0.0.0. CoreCLR rolls assembly binds forward but never backward.

The failure is invisible in the producing repo's build and in its nuspec. It only appears when something consumes the packed artifact.

Why an analyzer is the right home for this

This is a hazard the SDK's own analyzer creates. KTSU0006 demands the reference; the fixer writes it; nothing then checks whether the reference is safe. Two repositories have now had to work it out by hand, and the check that catches it is mechanical:

Read obj/<TargetFramework>/project.assets.json and look at what the reference resolves for its compile asset.

  • "compile": { "lib/net9.0/_._": {} } — an empty placeholder — means the framework wins and the reference is safe.
  • A real assembly path ("lib/net9.0/System.Text.Json.dll") means the package is overriding the framework, and the version must be pinned to what the lowest-supported target framework ships.

Sdk/Sdk.targets already computes package-graph facts and hands them to KTSU0005/KTSU0006 as AdditionalFiles (orphan list, assembly→package map, direct-package set). The same target has everything it needs to emit a fourth input: for each direct PackageReference, whether its compile asset is a placeholder or a real assembly, per target framework.

Proposal

A new diagnostic (KTSU0008) — "package reference overrides the shared framework". Reported when a direct PackageReference resolves a real compile asset on a target framework whose shared framework also supplies that assembly, and the pinned version is above what the lowest-supported framework ships.

A code fix that pins the PackageVersion down to the floor, and adds NoWarn="NU1510" on the item — NuGet's package-pruning warning fires in exactly this situation and says the opposite of what KTSU0006 says, so both diagnostics can only be satisfied together with that scoped suppression. GitIntegration's fix is the worked example: pin System.Text.Json centrally to 9.0.13, NoWarn="NU1510" on the reference.

Failing that, at minimum: make the KTSU0006 fixer add PrivateAssets="all" and emit the check as a comment or a build warning, so the person taking the fix is told there is a second question to answer.

Related

  • The neighbouring hazard — another package in the graph pinning a lower version than CPM floats, needing VersionOverride — is the same class of bug from a different direction (GitIntegration's Testably.Abstractions.FileSystem.Interface case). If the analyzer reads the resolved graph anyway, both are detectable from the same data.
  • KTSU0007 already enforces PrivateAssets for Polyfill only; a reference added by the KTSU0006 fixer is not covered by it.

Activity

  1. added
    enhancement
    on Sep 9, 2026
  2. matt-edmondson commented on Sep 10, 2026

    @matt-edmondson
    ContributorAuthor

    Triage: Priority: High · Effort: Medium

    KTSU0006's code fix can pin a PackageReference/PackageVersion above what a lower target framework's shared framework ships, producing a package that throws FileNotFoundException for every consumer on that framework — invisible in the producing repo's own build and nuspec. This has already bitten GitIntegration in practice and is a hazard the SDK's own analyzer creates, so it belongs in the SDK rather than being rediscovered per-repo.

    Suggested next steps / acceptance criteria:

    • Extend the existing package-graph target (Sdk/Sdk.targets, alongside the KTSU0005/KTSU0006 inputs) to emit, per direct PackageReference, whether its compile asset resolves to a framework placeholder or a real assembly per target framework
    • Add a new diagnostic (proposed KTSU0008) firing when a reference overrides a shared-framework assembly above the lowest-supported framework's version
    • Add a code fix that pins the version to the floor and adds NoWarn="NU1510" on the item (NuGet's pruning warning says the opposite of KTSU0006 in exactly this case)
    • At minimum, if the full diagnostic is deferred, have the KTSU0006 fixer add PrivateAssets="all" and surface the project.assets.json check as guidance so the person taking the fix knows there's a second question to answer
    • Cover with tests against a package known to ship in-box on at least one targeted framework (e.g. System.Text.Json)

    Blockers/dependencies: none; related to KTSU0007 (currently Polyfill-only PrivateAssets enforcement) and to the neighboring VersionOverride hazard noted in the issue, which could share the same resolved-graph data if tackled together


    Generated by Claude Code

  3. matt-edmondson commented on Sep 13, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    Category: Bug
    Priority: High
    Suggested assignment: none — already in sdk (the repo whose own analyzer/code-fix creates the hazard); no other repo is implicated
    Possible duplicate: none found — no other open Sdk issue covers the KTSU0006 fixer or a framework-override diagnostic


    Generated by Claude Code

  4. matt-edmondson commented on Sep 13, 2026

    @matt-edmondson
    ContributorAuthor

    Note for anyone reading this thread: the **Triage:** comment above and the ## Triage comment below both cover this same finding — they were posted by two different automation routines (an issue-creation/review routine and the org-wide issue-triage routine) that didn't recognize each other's comment format. That gap has since been fixed so it won't recur. No action needed here; treat the two as complementary (the first has the detailed acceptance criteria, the second has the org-wide classification).


    Generated by Claude Code

  5. matt-edmondson commented on Sep 22, 2026

    @matt-edmondson
    ContributorAuthor

    Implemented in #50 as KTSU0008, with the analyzer, the code fix, and the Sdk.targets input generation the issue sketches. Two things measured along the way are worth recording here, because both changed the shape of what landed.

    The mechanism, confirmed rather than assumed

    Built a net10.0;net9.0 project referencing System.Text.Json 10.0.2 and read back what each inner build resolved:

    inner build compile asset for System.Text.Json framework ships
    net10.0 nothing — pruned 10.0.12
    net9.0 lib/net9.0/System.Text.Json.dll 9.0.20

    Build succeeded. 0 Error(s), exactly as the issue says. So the project.assets.json check the issue describes is real, and it has a cheaper source than parsing that file: @(ResolvedCompileFileDefinitions) already carries the resolved assembly with its NuGetPackageId, and @(PackageConflictOverrides)' OverriddenPackages metadata is the targeting pack's PackageOverrides.txt. Both are per-target-framework MSBuild state available after ResolveReferences, so no file parsing is needed and "the lowest supported framework" falls out for free — the inner build that has the problem is the one that reports.

    NU1510 is not the safety net it looks like

    Worth knowing, because it is the reason this rule earns its place rather than duplicating NuGet. The first version of the triggering example failed at restore, before the compiler ran:

    error NU1510: Warning As Error: PackageReference System.Text.Json will not be pruned.
    This package is automatically available and does not need to be referenced explicitly.
    

    NU1510 fires for any reference to a framework-supplied package — the safe pinned-to-the-floor version and the dangerous higher one alike. It does not compare versions. Combined with this SDK treating warnings as errors, that means a project KTSU0006 has told to declare the reference has no choice but to NoWarn it, which is the state GitIntegration's fix ended up in. Once silenced, nothing checks the version at all — which is precisely the gap. The example therefore carries NoWarn="NU1510"; without it the scenario never reaches the analyzer and the test would be a straw man.

    One deliberate narrowing, for you to confirm

    The proposal conditions on override + version above the floor. The implementation adds a third condition: the reference must be fully private.

    NuGet omits a dependency from the produced package only when every asset kind is private, and it is that omission which leaves the consumer resolving nothing and falling back to the framework's lower copy. A reference that still flows carries the higher version to the consumer and resolves there — unusual, but not broken. At Error severity, firing on it would fail builds across the organization for working code, so it is excluded.

    That matches GitIntegration's actual case (PrivateAssets="all") and the spirit of KTSU0007, which is also why "fully private" moved into a shared helper both analyzers now read instead of being spelled out twice. If you want the wider rule the issue literally describes, it is a one-line change — your call, and the easiest thing to settle in review.

    Not done here

    The neighbouring VersionOverride hazard from the Related section is not covered. It is detectable from the same data, but it is a different diagnostic with a different fix, and bundling it would have made this PR two rules instead of one. Happy to take it as a follow-up if it is wanted.


    Generated by Claude Code

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

Metadata

Metadata

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions