microsoft / MSBuildSdks

MSBuild project SDKs
MIT License
456 stars 81 forks source link

CentralPackageVersions in .NET 6 SDK projects leads to a build warning #347

Open ojanacek opened 2 years ago

ojanacek commented 2 years ago

After updating .NET SDK used in global.json from 5 to 6 all the projects started reporting the following warning.

C:\Program Files\dotnet\sdk\6.0.200\Sdks\Microsoft.NET.Sdk\Sdk\Sdk.props(40,3): warning MSB4011: "C:\Program Files\dotnet\sdk\6.0.200\Current\Microsoft.Common.props" cannot be imported again. It was already imported at "C:\Users\xyz.nuget\packages\microsoft.build.centralpackageversions\2.1.3\Sdk\Sdk.props (15,3)". This is most likely a build authoring error. This subsequent import will be ignored.

Didn't manage to get rid of this with an appropriate in Directory.Build.targets.

jeffkl commented 2 years ago

I'm using .NET 6 in a lot of projects but have not seen this issue. Can you please attach a small repro that I can use to investigate?

ojanacek commented 2 years ago

Sorry, I should have done that right from the start. I peeked in a verbose MSBuild log to find out when that message appears precisely and it is always after calling this target GenerateTemporaryTargetAssembly. That helped me create a minimalistic working sample of this issue. Just try changing .NET SDK in global.json back to 5 something to see that it wasn't showing the same warning as with .NET SDK 6.

TestProjectA.zip

jeffkl commented 2 years ago

Well now its even more interesting, it doesn't repro for me.

C:\Users\jeffkl\Downloads\TestProjectA\TestProjectA>msbuild /t:Restore TestProjectA.csproj
Microsoft (R) Build Engine version 17.1.0+ae57d105c for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 3/25/2022 9:09:40 AM.
Project "C:\Users\jeffkl\Downloads\TestProjectA\TestProjectA\TestProjectA.csproj" on node 1 (Restore target(s)).
_GetAllRestoreProjectPathItems:
  Determining projects to restore...
Restore:
  Restoring packages for C:\Users\jeffkl\Downloads\TestProjectA\TestProjectA\TestProjectA.csproj...
  Generating MSBuild file C:\Users\jeffkl\Downloads\TestProjectA\TestProjectA\obj\TestProjectA.csproj.nuget.g.props.
  Generating MSBuild file C:\Users\jeffkl\Downloads\TestProjectA\TestProjectA\obj\TestProjectA.csproj.nuget.g.targets.
  Writing assets file to disk. Path: C:\Users\jeffkl\Downloads\TestProjectA\TestProjectA\obj\project.assets.json
  Restored C:\Users\jeffkl\Downloads\TestProjectA\TestProjectA\TestProjectA.csproj (in 322 ms).

  NuGet Config files used:
      C:\Users\jeffkl\AppData\Roaming\NuGet\NuGet.Config
      C:\Program Files (x86)\NuGet\Config\Microsoft.VisualStudio.Offline.config

  Feeds used:
      https://api.nuget.org/v3/index.json
      C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\
Done Building Project "C:\Users\jeffkl\Downloads\TestProjectA\TestProjectA\TestProjectA.csproj" (Restore target(s)).

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:02.44

C:\Users\jeffkl\Downloads\TestProjectA\TestProjectA>type ..\global.json
{
  "sdk": {
    "version": "6.0.101",
    "rollForward": "latestFeature"
  },
  "msbuild-sdks": {
    "Microsoft.Build.CentralPackageVersions": "2.1.3"
  }
}
C:\Users\jeffkl\Downloads\TestProjectA\TestProjectA>tree /f ..
Folder PATH listing
Volume serial number is 000000BF 4465:8221
C:\USERS\JEFFKL\DOWNLOADS\TESTPROJECTA
│   global.json
│
└───TestProjectA
    │   TestProjectA.csproj
    │   Window1.xaml
    │   Window1.xaml.cs
    │
    └───obj
            project.assets.json
            project.nuget.cache
            TestProjectA.csproj.nuget.dgspec.json
            TestProjectA.csproj.nuget.g.props
            TestProjectA.csproj.nuget.g.targets

C:\Users\jeffkl\Downloads\TestProjectA\TestProjectA>dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.201
 Commit:    ef40e6aa06

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19044
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.201\

Host (useful for support):
  Version: 6.0.3
  Commit:  c24d9a9c91

.NET SDKs installed:
  6.0.201 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 6.0.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download

Can you build TestProjectA.csproj with the binary logger and attach msbuild.binlog?

msbuild /t:Restore TestProjectA.csproj /bl`
ojanacek commented 2 years ago

I could but I could also just tell you that the target I mentioned is not called during restore but during build :) Please, try "dotnet build" if you haven't.

jeffkl commented 2 years ago

You're right, sorry I missed the fact that the warning only happens when building the project. This looks like a "bug" in the WPF build logic. Its generating a temp project on the fly and building it. The temp project has some interesting build logic that ends up importing Microsoft.Build.CentralPackageVersions first and Microsoft.NET.Sdk.WindowsDesktop second. Microsoft.NET.Sdk.WindowsDesktop then imports Microsoft.NET.Sdk which tries to import Microsoft.Common.props again unconditionally (it should be checking '$(MicrosoftCommonPropsHasBeenImported)' != 'true').

A workaround is to change how Microsoft.Build.CentralPackageVersions is imported.

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup Label="Globals">
    <SccProjectName>SAK</SccProjectName>
    <SccProvider>SAK</SccProvider>
    <SccAuxPath>SAK</SccAuxPath>
    <SccLocalPath>SAK</SccLocalPath>
  </PropertyGroup>

  <PropertyGroup>
    <TargetFrameworks>net472;net5.0-windows</TargetFrameworks>
    <UseWPF>true</UseWPF>
  </PropertyGroup>
  <Import Project="Sdk.targets" Sdk="Microsoft.Build.CentralPackageVersions" />
</Project>
MeikTranel commented 2 years ago

XamlPrecompile Steps really are the devil. I wonder if there's any remaining interest left at Microsoft to move it into a roslyn generator.

ojanacek commented 2 years ago

@jeffkl Thanks, that helped.