Search code examples
nugetnuget-package

How to transitively copy files to output across NuGet packages?


I have a PackageA that needs a file to be copied to the output of the final application to work as expected. I include the needed file into my PackageA's project with these lines:

  <ItemGroup>
    <Content Include="test.txt">
      <PackageCopyToOutput>true</PackageCopyToOutput>
    </Content>
  </ItemGroup>

If I consume the generated package directly, the file test.txt is copied to the output.

Now, if I reference this PackageA from another PackageB, and reference only the PackageB rather than PackageA, the file test.txt is not copied at all to the output while I expected this file to be transitively copied.

I tried digging into the issues of the NuGet/Home repository and found that we should play with the PrivateAssets or something like that (Issue on github and all its siblings). But I never succeeded in making this work.

I made a github repository to show off the issue I'm facing.

I also know that it is possible to copy the file by including .props or a .targets file doing so with a pre-build or post-build action, but I find this solution quite hacky, not elegant, and hard to set up and maintain over time. Moreover, I do have the feeling that NuGet is supposed to support this kind of usage quite easily, so maybe I have missed something somewhere.

On StackOverflow, there are also many posts about transitive copy. However, as far as I dig, it always talks about packages that are directly consumed by a project, or a tree of projects. My issue is a package referenced by another package itself referenced by a project.


Solution

  • From what I understand, you could use the dependency control flow by adding metadata to your project reference https://learn.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files#controlling-dependency-assets

    In PackageB.csproj

    <ProjectReference Include="../PackageA/PackageA.csproj">
        <PrivateAssets>analyzers;build</PrivateAssets>
    </ProjectReference>
    

    contentFiles should then be in the included assets