Skip to content

Bug: ExcludeAssets="contentFiles" on <ProjectReference> does not prevent CopyToOutputDirectory items from being copied in .NET SDK 9.0.200 #48945

@GPManuel

Description

@GPManuel

Describe the bug

When using <ProjectReference> with <ExcludeAssets>contentFiles</ExcludeAssets>, content files (items with <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>) from the referenced project are still being copied to the output directory of the referencing project. This behavior is observed with .NET SDK 9.0.200 (dotnet --version).

The ExcludeAssets="contentFiles" metadata is recognized by MSBuild on the resolved project reference item during the build of the referencing project. However, the content files from the referenced project still appear in internal MSBuild item lists (such as _TransitiveItemsToCopyToOutputDirectory) and are subsequently copied to the output.

To Reproduce

  1. Set up a minimal reproduction:
    A minimal reproduction repository can be found at: [repository]

    • Alternatively, follow these manual steps:
  2. Create a new directory for the test (e.g., ExcludeAssetsRepro).

  3. Inside ExcludeAssetsRepro, create a class library project (e.g., ReferencedLib):

    • Command: dotnet new classlib -o ReferencedLib -f net8.0
    • Replace ReferencedLib\ReferencedLib.csproj with:
      <Project Sdk="Microsoft.NET.Sdk">
        <PropertyGroup>
          <TargetFramework>net8.0</TargetFramework>
        </PropertyGroup>
        <ItemGroup>
          <Content Include="MyScript.sql">
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          </Content>
        </ItemGroup>
      </Project>
    • Create an empty file named MyScript.sql in the ReferencedLib directory.
  4. Inside ExcludeAssetsRepro, create a console application project (e.g., MainApp) that references the class library:

    • Command: dotnet new console -o MainApp -f net8.0
    • Replace MainApp\MainApp.csproj with:
      <Project Sdk="Microsoft.NET.Sdk">
        <PropertyGroup>
          <OutputType>Exe</OutputType>
          <TargetFramework>net8.0</TargetFramework>
        </PropertyGroup>
        <ItemGroup>
          <ProjectReference Include="..\ReferencedLib\ReferencedLib.csproj">
            <ExcludeAssets>contentFiles</ExcludeAssets>
          </ProjectReference>
        </ItemGroup>
      </Project>
  5. Build the MainApp project:

    • Navigate to the MainApp directory: cd MainApp
    • Build the project: dotnet build

Expected behavior:
The file MyScript.sql should NOT be present in the output directory of MainApp (e.g., MainApp\bin\Debug\net8.0\).

Actual behavior:
The file MyScript.sql IS present in the output directory of MainApp (e.g., MainApp\bin\Debug\net8.0\MyScript.sql).

Exceptions (if any)

None. The build succeeds without errors, but the output content is incorrect.

Further technical details

dotnet --info output:

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions