Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Project generates a nuget package that depends on another project that does not create a nuget package

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

  • Package .NET Standard library as Nuget package and include all project dependencies / references is related, but assumes the use of nuget pack. I am using dotnet build or Visual Studio to build the nuget package, so doesn't apply.
  • This GitHub query is pretty much the same as my issue.
like image 278
RB. Avatar asked Mar 14 '19 13:03

RB.


People also ask

Where are NuGet packages referenced from?

PackageReference: this is a section right within the .csproj file, containing the list of NuGet packages referenced packages.config: a file with this name is stored within the project folder, and it’s this file that contains the list of the NuGet packages referenced Why would we care about the type of package management format our project uses?

Why does NuGet fail to resolve dependencies?

As shown below, if Package A requires exactly Package B 1.0 and Package C requires Package B >=2.0, then NuGet cannot resolve the dependencies and gives an error. In these situations, the top-level consumer (the application or package) should add its own direct dependency on Package B so that the Nearest Wins rule applies.

How to install the NuGet package in Visual Studio?

One just needs to save the file within Visual Studio in order for the new version of the .csproj file (known so far to Visual Studio only) to be committed back to disk. This concludes the process of installing the NuGet package and integrating it with the project.

How to check if a DLL is installed with NuGet dependencies?

Now if you run dotnet pack you should see any project reference DLL under the lib folder of the package, and if you inspect the nuspec file inside the package (or upload it to your package repo) you should see the nuget dependencies. Hopefully this helps someone, as there is a lot of conflicting info around.


2 Answers

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>
like image 57
RB. Avatar answered Oct 13 '22 17:10

RB.


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.

like image 23
Manoj Choudhari Avatar answered Oct 13 '22 16:10

Manoj Choudhari