8

If I have a project (P1) that builds a nuget package, and I make it depend on a project (P2) that doesn't build a nuget package, the generated package will still reference P2 as a nuget package.

Steps to reproduce

  • Create a solution with 2 C# projects (P1 and P2)
  • Make P1 depend on P2.
  • Add the following line to P1.csproj (to make it build a nuget package)

    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>

  • Build the solution
  • Open the generated P1.nupkg in, e.g., NuGet Package Explorer

Note that the NuGet package depends on another NuGet package called P2. However, that package does not exist, and will never exist, as I have not told the P2 project to build a nuget package.

Question

How do I force P2.dll to be included in P1.nupkg's lib folder? Ideally it will force it only for references that don't create a nuget package themselves.

Other questions

RB.
  • 36,301
  • 12
  • 91
  • 131
  • Is `P1` a library or an application project? Does it have a `nuspec` file or is the package configured via the project properties? For NuGet packages using a `nuspec` file, you can read here how to include additional files: https://learn.microsoft.com/de-de/nuget/reference/nuspec#including-assembly-files (but note that - contrary to what Microsoft writes - the `` element does not support wildcards!). With the new format without a nuspec file, I don't know how to include additional files. – Nicolas Mar 14 '19 at 13:58
  • Lets assume both `P1` and `P2` are libraries (but does it make a difference?). It doesn't currently have a nuspec file as I am using the new "lean" project format, which should make this broadly redundant. Please also note that I am expecting to do the build via Visual Studio or `dotnet build` - I'm not actually using `nuget pack` or similar... – RB. Mar 14 '19 at 14:01
  • 1
    The difference is, that an application project is not supported for the new format and still uses the old project format therefore for sure. But as you're using the new format, you have three options: a) bring it to work automatically, b) downgrade the project format and use a `nuspec` to include `P2` manually, or c) try including `P2` manually, using the new format - I found this, but have not tried it so far: https://tyrrrz.me/Blog/Additional-NuGet-files-in-new-csproj – Nicolas Mar 14 '19 at 14:18

2 Answers2

10

One workaround is to mark any assets you don't want to include as <PrivateAssets>all</PrivateAssets>, and then include the below code in your project file.

See https://github.com/nuget/home/issues/3891#issuecomment-459848847 for more information

<ItemGroup>
  <ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj">
    <PrivateAssets>all</PrivateAssets>
  </ProjectReference>
</ItemGroup>

<!--
  The following solves the problem that 'dotnet pack' does not include the DLLs from referenced projects.
  See https://github.com/NuGet/Home/issues/3891 for a description of the problem
  and for newer versions / workarounds / built-in methods.
-->
<PropertyGroup>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
  <!-- include PDBs in the NuGet package -->
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup>
<Target Name="CopyProjectReferencesToPackage" DependsOnTargets="ResolveReferences">
    <ItemGroup>
        <BuildOutputInPackage Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')->WithMetadataValue('PrivateAssets', 'all'))" />
    </ItemGroup>
</Target>
RB.
  • 36,301
  • 12
  • 91
  • 131
2

If I understood correctly, the referenced project assemblies are not getting included in the nuget package.

You can try below command while generating nuget package by using below command:

nuget pack projectfile.csproj -IncludeReferencedProjects

This should include P2.dll in the nuget. Refer this MSDN page for more details.

Hope this helps.

Manoj Choudhari
  • 5,277
  • 2
  • 26
  • 37
  • 1
    I've just updated my answer to explain why this doesn't apply - I'm not using `nuget pack` (but I'm upvoting you anyway because this is a good answer for the question as originally written - sorry...) – RB. Mar 14 '19 at 14:03
  • Problem is nuget pack doesn't support embedded views out of the box without additional work. – Michael Aug 23 '19 at 01:58