NuGet / Home

Repo for NuGet Client issues
Other
1.5k stars 252 forks source link

'ProjectReference' not working properly in a multi-target project #10813

Open daxian-dbw opened 3 years ago

daxian-dbw commented 3 years ago

Details about Problem

Problem

Hi all, I'm working on a test project that has multiple targets netcoreapp3.1, net5.0, and net6.0.

So the .csproj file for the test project looks like this (see the the complete version of this file here, in my temp branch):

<Project Sdk="Microsoft.NET.Sdk">  <PropertyGroup>
  <PropertyGroup>
    <!-- other properties -->
    <OutputType>Exe</OutputType>
    <TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
  </PropertyGroup>

  <ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
    <ProjectReference Include="../../../src/Microsoft.PowerShell.SDK/Microsoft.PowerShell.SDK.csproj" />
    <ProjectReference Include="../../../src/Microsoft.PowerShell.Commands.Diagnostics/Microsoft.PowerShell.Commands.Diagnostics.csproj" />
  </ItemGroup>

  <ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
    <PackageReference Include="Microsoft.PowerShell.SDK" Version="7.1.3" />
  </ItemGroup>

  <ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
    <PackageReference Include="Microsoft.PowerShell.SDK" Version="7.0.6" />
  </ItemGroup>
</Project>

It looks simple, but dotnet restore fails with the following errors:

error NU1201: Project Microsoft.PowerShell.Commands.Diagnostics is not compatible with netcoreapp3.1 (.NETCoreApp,Version=v3.1).
    Project Microsoft.PowerShell.Commands.Diagnostics supports: net6.0 (.NETCoreApp,Version=v6.0)
error NU1201: Project Microsoft.PowerShell.Commands.Management is not compatible with netcoreapp3.1 (.NETCoreApp,Version=v3.1)
    Project Microsoft.PowerShell.Commands.Management supports: net6.0 (.NETCoreApp,Version=v6.0)
error NU1201: Project Microsoft.PowerShell.Commands.Utility is not compatible with netcoreapp3.1 (.NETCoreApp,Version=v3.1).
    Project Microsoft.PowerShell.Commands.Utility supports: net6.0 (.NETCoreApp,Version=v6.0)
error NU1201: Project Microsoft.PowerShell.ConsoleHost is not compatible with netcoreapp3.1 (.NETCoreApp,Version=v3.1).
    Project Microsoft.PowerShell.ConsoleHost supports: net6.0 (.NETCoreApp,Version=v6.0)
error NU1201: Project Microsoft.PowerShell.Security is not compatible with netcoreapp3.1 (.NETCoreApp,Version=v3.1).
    Project Microsoft.PowerShell.Security supports: net6.0 (.NETCoreApp,Version=v6.0)
error NU1201: Project System.Management.Automation is not compatible with netcoreapp3.1 (.NETCoreApp,Version=v3.1).
    Project System.Management.Automation supports: net6.0 (.NETCoreApp,Version=v6.0)

======= Same errors repeated for net5.0 ========

(NOTE: the projects Microsoft.PowerShell.Commands.Management, Microsoft.PowerShell.Commands.Utility, and etc are dependencies of Microsoft.PowerShell.SDK.csproj, and they are all targeting net6.0 currently)

The ProjectReference to Microsoft.PowerShell.SDK.csproj and Microsoft.PowerShell.Commands.Diagnostics.csproj are grouped in a ItemGroup with the condition "'$(TargetFramework)' == 'net6.0'", so why was dotnet restore checking compatibility for those project references with netcoreapp3.1 and net5.0? Can someone please suggest how to make this work?

Repro steps and/or sample project

The complete version of this project can be found here, in my temp branch.

Verbose Logs

Here is the ZIP package that contains binlog files from running dotnet restore /bl and dotnet build -f net6.0 -c release /bl respectively.

binlog.zip

FYI, this is for a benchmark project. We want to run the benchmarks easily against our current code base (net6.0) and our shipped NuGet packages within their corresponding target runtimes (net5.0 or netcoreapp3.1).

daxian-dbw commented 3 years ago

The issue is transferred from https://github.com/dotnet/sdk/issues/17013, as suggested by @nkolev92.