dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.66k stars 1.06k forks source link

`dotnet build` fails with error NU1008 when directory tree contains multiple Directory.Packages.props files. #40445

Open voroninp opened 4 months ago

voroninp commented 4 months ago

Describe the bug

I have a repository with multiple Directory.Packages.props files. I want to exclude some projects (tools directory) from central package management. I can build solution in VS Version 17.9.6 (fails with Version 17.10.0 Preview 5.0) without issues, but dotnet build command fails with this error:

error NU1008: Projec ts that use central package version management should not define the version on the PackageReference items but on the P ackageVersion items: coverlet.collector;Microsoft.NET.Test.Sdk;xunit;xunit.runner.visualstudio.

It looks like the closest Directory.Packages.props file is ignored.

To Reproduce

Here is the solution to reproduce the issue (sln file is in Json.TestLogger directory): CliBug.zip

Further technical details

Runtime Environment: OS Name: Windows OS Version: 10.0.22631 OS Platform: Windows RID: win-x64 Base Path: C:\Program Files\dotnet\sdk\8.0.300-preview.24203.14\

.NET workloads installed: [android] Installation Source: SDK 8.0.300-preview.24203, VS 17.10.34818.151, VS 17.9.34728.123 Manifest Version: 34.0.95/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.android\34.0.95\WorkloadManifest.json Install Type: FileBased

[ios] Installation Source: SDK 8.0.300-preview.24203, VS 17.10.34818.151, VS 17.9.34728.123 Manifest Version: 17.2.8053/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.ios\17.2.8053\WorkloadManifest.json Install Type: FileBased

[maccatalyst] Installation Source: SDK 8.0.300-preview.24203, VS 17.10.34818.151, VS 17.9.34728.123 Manifest Version: 17.2.8053/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maccatalyst\17.2.8053\WorkloadManifest.json Install Type: FileBased

[maui-windows] Installation Source: SDK 8.0.300-preview.24203, VS 17.10.34818.151, VS 17.9.34728.123 Manifest Version: 8.0.7/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maui\8.0.7\WorkloadManifest.json Install Type: FileBased

[wasm-tools] Installation Source: SDK 8.0.300-preview.24203, VS 17.10.34818.151, VS 17.9.34728.123 Manifest Version: 8.0.4/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.workload.mono.toolchain.current\8.0.4\WorkloadManifest.json Install Type: FileBased

Host: Version: 8.0.4 Architecture: x64 Commit: 2d7eea2529

.NET SDKs installed: 7.0.408 [C:\Program Files\dotnet\sdk] 8.0.204 [C:\Program Files\dotnet\sdk] 8.0.300-preview.24203.14 [C:\Program Files\dotnet\sdk]

.NET runtimes installed: Microsoft.AspNetCore.All 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.27 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.29 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 7.0.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 7.0.18 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 8.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 8.0.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.27 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.29 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.18 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 8.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 8.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.27 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.29 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 7.0.16 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 7.0.18 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 8.0.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 8.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found: x86 [C:\Program Files (x86)\dotnet] registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables: Not set

global.json file: Not found

Learn more: https://aka.ms/dotnet/info

Download .NET: https://aka.ms/dotnet/download


I've put `global.json` in root folder and it fixed the cli, but VS Version 17.10.0 Preview 5.0 still does not build.

{ "sdk": { "version": "8.0.204" } }

marcpopMSFT commented 4 months ago

Without a global.json, build in VS works but dotnet build fails but if you pin to the 8.0.204 SDK, the VS build fails but the dotnet build succeeds? What folder are you running dotnet build in? In VS, what project/solution are you opening?

The logic for how to load the directory.build.props file comes from the common props in msbuild. CC @dotnet/kitten to comment on the best way to get the experience you want.

voroninp commented 4 months ago

In preview VS it fails always. In stable version VS it works irrespective of global.json

dotnet build fails without global.json, but succeeds when pinned to 8.0.204

rainersigwald commented 4 months ago

This is a behavior change caused by https://github.com/NuGet/NuGet.Client/pull/5572.

@JonDouglas called it out as a breaking change in https://github.com/NuGet/Home/issues/11834 but I'm not sure that's documented as such yet. @jeffkl fyi.

voroninp commented 4 months ago

@rainersigwald , how do I disable it for the subtree then (tools solution)?

Directory.Build.props
Directory.Package.props
src
tools
     | 
     - Directory.Build.props
     - tools.sln
rainersigwald commented 4 months ago

Explicitly set

<PropertyGroup>
  <ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
</PropertyGroup>

in the nested Directory.Packages.props.

voroninp commented 4 months ago

Ah, so I need to create it as well. Ok, thanks.

omer-za commented 4 months ago

Is there a way to keep it default as false? I have a generated project using openapi generator that doesn't set it to false and every time I generate files I need to modify manually all csproj files :(

amis92 commented 3 months ago

I've created empty Directory.Packages.props to disable CPM at some level.

image