msbuild icon indicating copy to clipboard operation
msbuild copied to clipboard

The _GetProjectReferenceTargetFrameworkProperties target doesn't work for multiple projects with same identity

Open ViktorHofer opened this issue 2 years ago • 0 comments

The _GetProjectReferenceTargetFrameworkProperties target is responsible for annotating ProjectReference items with various metadata (SetTargetFramework, UndefineProperties, ...). When specifying multiple ProjectReference items with the same identity, the metadata isn't applied which results in the wrong target framework, runtime identifier and self contained settings being passed to the project.

Example:

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

  <PropertyGroup>
    <TargetFrameworks>net7.0</TargetFrameworks>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\b\b.csproj" />
    <ProjectReference Include="..\b\b.csproj" ReferenceOutputAssemlby="false" OutputItemType="CustomAdditionalCompileInputs" />
  </ItemGroup>

</Project>

Error

As the TargetFramework global property isn't undefined, net7.0 is passed down to the child project, which only targets net6.0 and net5.0.

C:\temp\tfm\a>dotnet build
MSBuild version 17.4.0+18d5aef85 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
C:\Program Files\dotnet\sdk\7.0.101\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(267,5): error NETSDK1005: Assets file 'C:\temp\tfm\b\o
bj\project.assets.json' doesn't have a target for 'net7.0'. Ensure that restore has run and that you have included 'net7.0' in the TargetFrameworks for your project. [
C:\temp\tfm\b\b.csproj::TargetFramework=net7.0]

Build FAILED.

Minimal repro: tfm.zip

Reason for that are the '@(AnnotatedProjects)' == '%(Identity)' checks in the condition statements: https://github.com/dotnet/msbuild/blob/main/src/Tasks/Microsoft.Common.CurrentVersion.targets#L1891

It's not clear to me why these statements exist in the first place. When I remove them, everything works as expected. Those were added by @AndyGerlicher in https://github.com/dotnet/msbuild/commit/57ae27cf0812bf262c6f8be456ed527a1542dba7. I assume that those were necessary with a previous version of MSBuild and can be removed now.

cc @rainersigwald

ViktorHofer avatar Dec 29 '22 17:12 ViktorHofer