Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to copy contentFiles to executable project's output only? #9627

Closed
hirschmann opened this issue May 29, 2020 · 3 comments
Closed
Labels
Area:ContentFiles PackageReference contentFiles folder Functionality:Restore Resolution:Duplicate This issue appears to be a Duplicate of another issue Type:Feature

Comments

@hirschmann
Copy link

Let's assume I have the following project structure:

ExecutableProject
│
└── Project A
    │
    └── Project B
        │
        └── ContentFilesNugetPackage

The NuGet package contains some assets which are required by Library B at runtime.
The nuspec file for the package looks like this:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
    <metadata>
        <id>test.package</id>
        <version>1.0.0</version>
        <description>Test content</description>
        <authors>demo</authors>
		
        <contentFiles>
            <files include="**/*" buildAction="content" copyToOutput="true" flatten="false" />
        </contentFiles>
    </metadata>
</package>

This setup actually works. The assets will be copied to the executable project's output path on build.

the problem

The assets will not only be copied to the ExecutableProject's output path, but also to the output paths of Project A and Project B.
This is a problem in case there are a lot of nuget packages, there are a lot of asset files and the project dependency graph is deep, because it increases the build time.

Is it possible to build the nuget package in a way so that the contentFiles are copied to executable project's output only?
I'm thinking of a behavior like runtime files, but I want to be able to include any file (not just assemblies) and keep the folder hierarchy.

I have tried different combinations of buildAction, copyToOutput and PrivateAssets (in Project B) but I couldn't get it to work.
Either the assets are copied to all related project's output paths, or the assets aren't copied at all.

Is the scenario I describe supported by NuGet?

demo project

I have built a small projects which demonstrates the problem:
nuget-issue-demo.zip

tools versions

nuget.exe: 5.6.0.6591
dotnet.exe: 3.1.201
Visual Studio: 16.5.4

@msedi
Copy link

msedi commented Jul 16, 2020

Is there any progress on this?

@nkolev92
Copy link
Member

Hey @hirschmann,

In PackageReference projects, the content files are by default not transitive.
Refer to https://docs.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files#controlling-dependency-assets.

For solving the general problem of controlling what gets included from transitive packages, please refer to #6720.

@nkolev92 nkolev92 added Area:ContentFiles PackageReference contentFiles folder Functionality:Restore Resolution:Duplicate This issue appears to be a Duplicate of another issue Type:Feature labels Jul 20, 2020
@hirschmann
Copy link
Author

@nkolev92
Thanks for your answer!

I got it to work by adding a buildTransitive .targets file which copies the content files only if the project is an executable project:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="15.0">

  <!-- copy content to build output for executable projects only -->  
  <Target Name="AfterBuild" Condition="$(OutputType) == 'Exe'">
    <ItemGroup>
      <_ContentFiles Include="$(MSBuildThisFileDirectory)..\contentFiles\any\any\**\*" />
    </ItemGroup>
  
    <Copy SourceFiles="@(_ContentFiles)" DestinationFolder="$(OutDir)\%(RecursiveDir)" SkipUnchangedFiles="true" OverwriteReadOnlyFiles="true" />
   </Target>
</Project>

Additionally I set copyToOuptut in the .nuspec file to false to stop msbuild from copying the content files into every library project output folder.

If anyone else encounters the same problems - this is my fixed demo project:
nuget-issue-demo-fixed.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area:ContentFiles PackageReference contentFiles folder Functionality:Restore Resolution:Duplicate This issue appears to be a Duplicate of another issue Type:Feature
Projects
None yet
Development

No branches or pull requests

3 participants