ProjectReference is not copied to output as part of parent NuGet package

1.2k Views Asked by At

I have 2 csproj projects: Child.csproj and Parent.csproj. Child.csproj is referenced by Parent.csproj through ProjectReference:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\Child.csproj" />
  </ItemGroup>

</Project>

Child project also can be provided either as x86 dll or x64 dll. And it is being packaged as NuGet package as well as Parent project. In order to package 2 different versions of Child dlls was used next props and targets files:

Child.props

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <Reference Include="Child" Condition="'$(Platform)' == 'x86'">
          <HintPath>$(MSBuildThisFileDirectory)..\lib\x86\Child.dll</HintPath>
        </Reference>
        <Reference Include="Child" Condition="'$(Platform)' == 'x64'">
          <HintPath>$(MSBuildThisFileDirectory)..\lib\x64\Child.dll</HintPath>
        </Reference>
      </ItemGroup>
    </Project>

Child.targets

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="PlatformCheck" BeforeTargets="BuildOnlySettings"
    Condition="(('$(Platform)' != 'x86') AND  ('$(Platform)' != 'x64'))">
    <Error  Text="$(MSBuildThisFileName) does not work correctly on '$(Platform)' platform. You need to specify platform (x86 or x64)." />
  </Target>
</Project>

Also, Child project is built with next nuspec:

<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
    <metadata>
        <id>Child</id>
    </metadata> 

    <files>
        <file src="bin\x64\Release\netstandard2.0\Child.dll" target="lib\x64" />
        <file src="bin\x86\Release\netstandard2.0\Child.dll" target="lib\x86" />
        <file src="build\Child.props" target="build" />
        <file src="build\Child.targets" target="build" />
    </files>
</package>

Both of these projects are being packaged as NuGet packages. When I reference Child package directly, it's dll is being copied to output folder. However, when I reference Parent package, I expect Child.dll to be copied with Parent.dll as well into output folder of the project, which references Parent but it is (Child.dll) not copied.

2

There are 2 best solutions below

0
rbennett485 On

You need to also specify your dependency in your nuspec file https://learn.microsoft.com/en-us/nuget/reference/nuspec#dependencies-element

Alternatively, don't use a nuspec at all and define your properties in one place in your project file - this is the recommended way to publish NuGet packages now https://learn.microsoft.com/en-us/nuget/reference/msbuild-targets#pack-target. With this setup the fact that you have a project reference in your csproj is enough to have the dependency properly added to your package

0
Marc Gravell On

Transitive dependencies are not usually resolved until an application/executable project is built - because until that point: there isn't enough information to know which actual dlls to include. If you're seeing this when building a library project (that references Parent), then: that is normal and expected, and does not represent a problem in any way.