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.61k stars 1.04k forks source link

MAUI is not compatible with ManagePackageVersionsCentrally #27840

Open aelij opened 1 year ago

aelij commented 1 year ago

Describe the bug

When using NuGet Central Package Management and MAUI installed, we get the following error:

error NU1507: There are 2 package sources defined in your configuration. When using central package management, please map your package sources with package source mapping (https://aka.ms/nuget-package-source-mapping) or specify a single package source. The following sources are defined: https://pkgs.dev.azure.com/..., C:\Program Files\dotnet\library-packs

To Reproduce

  1. Install MAUI
  2. Enable CPS by adding a Directory.Packages.props file with <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
  3. dotnet restore

Workaround

<DisableImplicitLibraryPacksFolder>true</DisableImplicitLibraryPacksFolder>

Further technical details

.NET SDK (reflecting any global.json):
 Version:   6.0.400
 Commit:    7771abd614

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

global.json file:
  C:\src\azdo\ADP\global.json

Host:
  Version:      6.0.8
  Architecture: x64
  Commit:       55fb7ef977

.NET SDKs installed:
  3.1.422 [C:\Program Files\dotnet\sdk]
  5.0.408 [C:\Program Files\dotnet\sdk]
  6.0.101 [C:\Program Files\dotnet\sdk]
  6.0.108 [C:\Program Files\dotnet\sdk]
  6.0.303 [C:\Program Files\dotnet\sdk]
  6.0.400 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 3.1.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.1.28 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.28 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
KalleOlaviNiemitalo commented 1 year ago

May be a duplicate of https://github.com/NuGet/Home/issues/11951.

nickrandolph commented 1 year ago

@jeffkl any thoughts on this? If related to https://github.com/NuGet/Home/issues/11951, when are we likely to see this fix being widely available?

jeffkl commented 1 year ago

@nickrandolph the change shipped with Visual Studio 2022 17.4 and .NET SDK 6.0.4xx

KalleOlaviNiemitalo commented 1 year ago

This issue is still reproducible with .NET SDK 6.0.402, which includes NuGet 6.3.1-rc.1+01bc4df1ef99c9c213f892ec8b25e46b23c7cfb1.01bc4df1ef99c9c213f892ec8b25e46b23c7cfb1 (see https://github.com/NuGet/Home/issues/11974 for the strange versioning). https://github.com/NuGet/NuGet.Client/blob/01bc4df1ef99c9c213f892ec8b25e46b23c7cfb1/src/NuGet.Core/NuGet.Commands/RestoreCommand/RestoreCommand.cs#L466 does not include the changes that were made in https://github.com/NuGet/NuGet.Client/pull/4764. @jeffkl, did you mean some other change?

jeffkl commented 1 year ago

@KalleOlaviNiemitalo okay sorry at some point NuGet stopped inserting into .NET 6. So I'd imagine the change is only in .NET 7 RC1 and above.

ganeshkhatri commented 8 months ago

The issue is again raised in dotnet 8.0. Works fine with dotnet 7.0.

Sen-Gupta commented 5 months ago

Issue occurs in .Net 8!

Sen-Gupta commented 5 months ago

Workaround:

Add following to your MAUI Project file, until the issue is fixed.

<PropertyGroup>
  <ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
</PropertyGroup>
james-world commented 5 months ago

Workaround:

Add following to your MAUI Project file, until the issue is fixed.

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

It's not enough to do this if you actually want CPM in your projects; you need the undocumented DisableImplicitLibraryPacksFolder property added to your Directory.Packages.props (or other project file). This is mentioned in https://github.com/dotnet/sdk/issues/28642.

Personally, I've never used MAUI - think I may have inadvertently installed it on my Mac as part of Visual Studio for Mac (now uninstalled since it's been discontinued) but it's left detritus on my machine I'm not sure how to clean properly. This fixed it for me:

<PropertyGroup>
  <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
  <DisableImplicitLibraryPacksFolder>true</DisableImplicitLibraryPacksFolder>
</PropertyGroup>
epsmae commented 4 months ago

The workaround mentioned by @james-world works for me, below you see the snippets. I can use central package management like that in the referenced non maui and the test projects. Not what I desired but hopefully this gets soon fixed.

Directory.Packages.props

<Project>
  <PropertyGroup>
    <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>

    <!-- See Maui Issue: https://github.com/dotnet/sdk/issues/27840 -->
    <DisableImplicitLibraryPacksFolder>true</DisableImplicitLibraryPacksFolder>
  </PropertyGroup>

  <!-- Microsoft Package Versions -->
  <ItemGroup>
    <PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
    ...
  </ItemGroup>

  ...
</Project>

Maui Project

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

  <PropertyGroup>
    <TargetFrameworks>net8.0-android;net8.0-ios</TargetFrameworks>
    <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks>
    <TargetFrameworks Condition="!$([MSBuild]::IsOSPlatform('android')) And !$([MSBuild]::IsOSPlatform('ios'))">$(TargetFrameworks);net8.0</TargetFrameworks>

    <!-- See Maui Issue: https://github.com/dotnet/sdk/issues/27840 -->
    <ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Maui.Controls" Version="8.0.6" />
  </ItemGroup>

  ...

</Project>