Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 880
Loïc Morvan Avatar asked Nov 15 '25 21:11

Loïc Morvan


1 Answers

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

like image 66
Nicolas Durin Avatar answered Nov 18 '25 19:11

Nicolas Durin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!