Skip to content

<IsPackable>false</IsPackable> in a library's .csproj is overwritten by Sdk.targets, so dotnet pack still produces and publishes the package (same for IsPublishable on Exes) #71

Open

Description

@matt-edmondson

What's wrong

Sdk/Sdk.targets (at bafee81) derives the two flags unconditionally:

<!-- Sdk/Sdk.targets:36-37 -->
<IsPublishable Condition="$(IsExecutable)">true</IsPublishable>
<IsPackable Condition="'$(IsLibrary)' == 'true' Or '$(PackAsTool)' == 'true'">true</IsPackable>

Sdk.targets is imported after the project body and after Directory.Build.targets. So a project can't opt a library out of packing, or an Exe out of publishing. Only a global -p:IsPackable=false on the command line survives. This contradicts CLAUDE.md's "MSBuild Evaluation Order" section, which says "Properties are set conditionally to avoid overwriting user-specified values."

Repro (local feed packed from HEAD):

<!-- Foo/Foo.csproj -->
<Project Sdk="Microsoft.NET.Sdk">
  <Sdk Name="ktsu.Sdk" />
  <PropertyGroup>
    <IsPackable>false</IsPackable>
    <TargetFrameworks>net10.0</TargetFrameworks>
  </PropertyGroup>
</Project>
  • dotnet msbuild Foo/Foo.csproj -getProperty:IsPackable gives true. Expected false.
  • dotnet pack Foo/Foo.csproj -c Release -o out reports Successfully created package 'ktsu.Foo.1.0.0.nupkg', plus a .snupkg. Expected no package.
  • Setting IsPackable=false in Directory.Build.targets still evaluates to true.
  • A ktsu.Sdk.ConsoleApp project with <IsPublishable>false</IsPublishable> evaluates to IsPublishable=true.

The README's own recipe doesn't work either. At README.md:706 it is:

<IsPackable Condition="$(IsPrerelease) == 'true'">false</IsPackable>

With Version=1.0.0-pre.1 this still evaluates to IsPackable=true, for two reasons:

  • IsPrerelease is only defined in Sdk.targets, so it is empty while the project body is evaluated.
  • Line 37 would overwrite the result anyway.

Why it matters

A repository with an internal library it doesn't want published marks it IsPackable=false and reasonably expects it to stay unpublished. Examples are a shared test-helpers library, a benchmark harness, a generator consumed only through a project reference, or a sample library. The solution-level dotnet pack in CI still produces the .nupkg, and the release step pushes it to NuGet. Nothing warns about it, and a package on nuget.org can only be unlisted, never deleted.

Suggested fix

  • Derive the default only when the project hasn't chosen one. Sdk.props:609 already defaults IsPackable to false, so either:
    • leave it empty in props and use Condition="'$(IsPackable)' == '' And (...)" in targets, falling back to false; or
    • snapshot the props default in a private marker (the same pattern as _KtsuSdkDefaultTestTargetFrameworks) and only override while the value still equals that default.
  • Apply the same change to IsPublishable.
  • Fix or remove the README.md:706 example. IsPrerelease can't be read from the project body, so either document a targets-time hook or drop the example.

Acceptance criteria:

  • A library project setting IsPackable=false, in its .csproj or in Directory.Build.props/targets, evaluates to false, and dotnet pack produces no package.
  • An Exe setting IsPublishable=false keeps it.
  • Libraries and PackAsTool projects that don't set it are still packable by default.
  • Example-workspace tests cover both the opt-out and the default.

Activity

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

    @matt-edmondson
    ContributorAuthor

    Triage

    • Category: Bug
    • Priority: High. A project that explicitly opts out with IsPackable=false still gets packed and pushed to nuget.org by the shared release pipeline. That can't be undone: a published package can only be unlisted. This affects every consumer of ktsu.Sdk.
    • Area / suggested owner: Sdk/Sdk.targets derivation of IsPackable/IsPublishable, and Sdk.props defaults, plus README.md:706.
    • Duplicates: None found.
    • In progress: No open PR covers this.
    • Next step: Make the targets-time derivation conditional on the project not having set a value. Use the private-marker snapshot pattern already used for _KtsuSdkDefaultTestTargetFrameworks. Add example-workspace tests for both the opt-out and the default.

    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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions