4

Is it possible for dotnet publish to copy contentFiles to output from a transitive package dependencies?

In this scenario, I have three projects:

  1. Content, which has a nuspec containing:
<contentFiles>
  <files include="any/any/someContent.txt" buildAction="None" copyToOutput="true" flatten="false"/>
</contentFiles>
  1. Lib, which has a PackageReference to Content nuget
  2. ConsoleApp, which has a PackageReference to Lib nuget

The Lib project shows the contentFiles in the project explorer and copies to output on build, but the ConsoleApp project does neither.

I need dotnet publish ConsoleApp.csproj to include someContent.txt from the Content nuget in the publish output.

Somewhat related posts for context:

Matt Mason
  • 2,676
  • 9
  • 22

1 Answers1

6

contentFiles are Private Assets by default. You need to change the PrivateAssets metadata in the reference from Lib to Content, so that it doesn't include contentFiles, like this:

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

More information here: https://learn.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files#controlling-dependency-assets

José Pedro
  • 1,097
  • 3
  • 14
  • 24
  • Would this be helpful in this situation? https://stackoverflow.com/q/65051835/227436 I tried to implement the settings, but it didn't seem to help. – Daryl Nov 30 '20 at 13:49
  • I think it should. Are you changing `PrivateAssets` in the reference from `DLaB.Xrm.Source` to `DLaB.Common.Source`? – José Pedro Nov 30 '20 at 21:01
  • No. I'm only including content – Daryl Nov 30 '20 at 21:06
  • In the `DLaB.Xrm.Source` project, you should change the reference to `DLaB.Common.Source`, like this: ``. – José Pedro Nov 30 '20 at 23:27
  • My DLaB.Xrm.Source is actually a .Net project, not a dotnetcore – Daryl Nov 30 '20 at 23:38
  • So I guess you are using nuspec, right? In that case, I compared the nuspec outputs, and I believe the equivalent there would be replacing `exclude="Build,Analyzers"` with `include="Runtime,Compile,Native,ContentFiles,BuildTransitive"`. – José Pedro Dec 01 '20 at 00:36