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.73k stars 1.07k forks source link

Multi-targeting causes ResolvePackageDependencies exception #16666

Open anwell opened 3 years ago

anwell commented 3 years ago

When adding a 2nd TargetFramework in the simple csproj file below, Visual Studio shows the following exception. When using a single TargetFramework, this error goes away.

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

  <PropertyGroup>
    <OutputType>Exe</OutputType>

    <!-- Multi-targeting causes ResolvePackageDependencies exception in Visual Studio -->
    <TargetFrameworks>net5.0;net462</TargetFrameworks>

    <!-- Single-targeting will not have any exception in Visual Studio -->
    <!-- <TargetFrameworks>net462</TargetFrameworks> -->
  </PropertyGroup>

  <ItemGroup>
    <!-- This package version doesn't exist, triggering the warning -->
    <PackageReference Include="System.Interactive.Async" Version="3.0.1000.0" />
  </ItemGroup>

</Project>
Error   MSB4018 The "ResolvePackageDependencies" task failed unexpectedly.
System.ArgumentException: Invalid framework version '4.6.2/win7-x86'.
   at NuGet.Frameworks.NuGetFramework.ParseFrameworkNameParts(IFrameworkNameProvider mappings, String[] parts, String& framework, Version& version, String& profile)
   at NuGet.Frameworks.NuGetFramework.ParseFrameworkName(String frameworkName, IFrameworkNameProvider mappings)
   at NuGet.Frameworks.NuGetFramework.Parse(String folderName, IFrameworkNameProvider mappings)
   at Microsoft.NET.Build.Tasks.ResolvePackageDependencies.<GetPackageAndFileDefinitions>b__48_5(String tg)
   at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
   at System.Linq.Enumerable.Contains[TSource](IEnumerable`1 source, TSource value, IEqualityComparer`1 comparer)
   at System.Linq.Enumerable.Contains[TSource](IEnumerable`1 source, TSource value)
   at Microsoft.NET.Build.Tasks.ResolvePackageDependencies.<>c__DisplayClass48_1.<GetPackageAndFileDefinitions>b__3(IAssetsLogMessage log)
   at System.Linq.Enumerable.WhereListIterator`1.MoveNext()
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source)
   at Microsoft.NET.Build.Tasks.ResolvePackageDependencies.<GetPackageAndFileDefinitions>g__GetPackageDiagnosticLevel|48_0(LockFileLibrary package)
   at Microsoft.NET.Build.Tasks.ResolvePackageDependencies.GetPackageAndFileDefinitions()
   at Microsoft.NET.Build.Tasks.ResolvePackageDependencies.ExecuteCore()
   at Microsoft.NET.Build.Tasks.TaskBase.Execute()
   at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
   at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext() ConsoleApp1 C:\Program Files\dotnet\sdk\5.0.201\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets    186 

image

It looks like the problem is that while this code is reading a warning log message, NugetFramework.Parse() hits an exception because it cannot parse framework version in the format "v4.6.2/win7-x86":

https://github.com/dotnet/sdk/blob/cf2f60514e8e6f16a67ca6cd10c52b52bf23ac68/src/Tasks/Microsoft.NET.Build.Tasks/ResolvePackageDependencies.cs#L253-L263

image

Full callstack from debugging:

>   NuGet.Frameworks.dll!NuGet.Frameworks.NuGetFramework.ParseFrameworkNameParts Line 243   C#
    NuGet.Frameworks.dll!NuGet.Frameworks.NuGetFramework.ParseFrameworkName Line 206    C#
    NuGet.Frameworks.dll!NuGet.Frameworks.NuGetFramework.Parse Line 63  C#
    Microsoft.NET.Build.Tasks.dll!Microsoft.NET.Build.Tasks.ResolvePackageDependencies.GetPackageAndFileDefinitions.AnonymousMethod__48_5   C#
    System.Core.dll!System.Linq.Enumerable.WhereSelectListIterator<string, string>.MoveNext Line 486    C#
    System.Core.dll!System.Linq.Enumerable.Contains<string> Line 1374   C#
    System.Core.dll!System.Linq.Enumerable.Contains<string> Line 1367   C#
    Microsoft.NET.Build.Tasks.dll!Microsoft.NET.Build.Tasks.ResolvePackageDependencies.GetPackageAndFileDefinitions.AnonymousMethod__3 Line 257 C#
    System.Core.dll!System.Linq.Enumerable.WhereListIterator<NuGet.ProjectModel.IAssetsLogMessage>.MoveNext Line 232    C#
    System.Core.dll!System.Linq.Enumerable.Any<NuGet.ProjectModel.IAssetsLogMessage> Line 1291  C#
    Microsoft.NET.Build.Tasks.dll!Microsoft.NET.Build.Tasks.ResolvePackageDependencies.GetPackageAndFileDefinitions.__GetPackageDiagnosticLevel|48_0 Line 265   C#
    Microsoft.NET.Build.Tasks.dll!Microsoft.NET.Build.Tasks.ResolvePackageDependencies.GetPackageAndFileDefinitions Line 194    C#
    Microsoft.NET.Build.Tasks.dll!Microsoft.NET.Build.Tasks.ResolvePackageDependencies.ExecuteCore Line 169 C#
    Microsoft.NET.Build.Tasks.dll!Microsoft.NET.Build.Tasks.TaskBase.Execute Line 38    C#
    Microsoft.Build.dll!Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute Line 567   C#
    Microsoft.Build.dll!Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask Line 809    C#
    Microsoft.Build.dll!Microsoft.Build.BackEnd.TaskBuilder.InitializeAndExecuteTask Line 656   C#
    Microsoft.Build.dll!Microsoft.Build.BackEnd.TaskBuilder.ExecuteBucket Line 424  C#
    Microsoft.Build.dll!Microsoft.Build.BackEnd.TaskBuilder.ExecuteTask Line 325    C#
    Microsoft.Build.dll!Microsoft.Build.BackEnd.TaskBuilder.ExecuteTask Line 177    C#
    Microsoft.Build.dll!Microsoft.Build.BackEnd.TargetEntry.ProcessBucket Line 818  C#
    Microsoft.Build.dll!Microsoft.Build.BackEnd.TargetEntry.ExecuteTarget Line 501  C#
    Microsoft.Build.dll!Microsoft.Build.BackEnd.TargetBuilder.ProcessTargetStack Line 467   C#
    Microsoft.Build.dll!Microsoft.Build.BackEnd.TargetBuilder.BuildTargets Line 162 C#
    Microsoft.Build.dll!Microsoft.Build.BackEnd.RequestBuilder.BuildProject Line 1085   C#
    Microsoft.Build.dll!Microsoft.Build.BackEnd.RequestBuilder.BuildAndReport Line 721  C#
    Microsoft.Build.dll!Microsoft.Build.BackEnd.RequestBuilder.RequestThreadProc Line 690   C#
    Microsoft.Build.dll!Microsoft.Build.BackEnd.RequestBuilder.StartBuilderThread.AnonymousMethod__45_2 Line 625    C#
    mscorlib.dll!System.Threading.Tasks.Task<System.Threading.Tasks.Task>.InnerInvoke Line 680  C#
    mscorlib.dll!System.Threading.Tasks.Task.Execute Line 2498  C#
    mscorlib.dll!System.Threading.Tasks.Task.ExecutionContextCallback Line 2861 C#
    mscorlib.dll!System.Threading.ExecutionContext.RunInternal Line 954 C#
    mscorlib.dll!System.Threading.ExecutionContext.Run Line 902 C#
    mscorlib.dll!System.Threading.Tasks.Task.ExecuteWithThreadLocal Line 2827   C#
    mscorlib.dll!System.Threading.Tasks.Task.ExecuteEntry Line 2756 C#
    mscorlib.dll!System.Threading.Tasks.TaskScheduler.TryExecuteTask Line 458   C#
    Microsoft.Build.dll!Microsoft.Build.BackEnd.RequestBuilder.DedicatedThreadsTaskScheduler.InjectThread.AnonymousMethod__6_0 Line 1317    C#
    mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context Line 74  C#
    mscorlib.dll!System.Threading.ExecutionContext.RunInternal Line 954 C#
    mscorlib.dll!System.Threading.ExecutionContext.Run Line 902 C#
    mscorlib.dll!System.Threading.ExecutionContext.Run Line 891 C#
    mscorlib.dll!System.Threading.ThreadHelper.ThreadStart Line 111 C#

This issue is mostly comsetic, because I can still restore/build using dotnet restore and dotnet build from the command line.

dotnet-issue-labeler[bot] commented 3 years ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

joeloff commented 3 years ago

There doesn't seem to be a 3.0.100.0 version of that package. There is a 3.1, but that only supports netstandard 1.6. Try switching to the 5.0.0 version of the package, and also try to reload your project after updating the package version.

anwell commented 3 years ago

@joeloff Thanks, yes I know there's no 3.0.100.0 version of the package, this is mainly an example. When single-targeting the build will give a warning that this package cannot be found, and uses the 3.1 version. When multi-targeting it show this warning and also the ResolvePackageDependencies error from the issue. This issue is mainly about the difference in behavior in how the warning gets escalated to an error when adding a 2nd TargetFramework.

maciej-izak commented 1 year ago

Starting with .NET SDK 7.0.200 (I also tested 7.0.201), I encountered the same error even when using dotnet restore or dotnet build from the command line. This issue seems to occur when an older version of .NET Framework is mixed with newer versions of .NET / .NET Core. However, multi-targeting works fine with older SDK versions.

ARamsden commented 1 year ago

I have also experienced the same thing as @maciej-izak. This no longer seems to be just a "cosmetic issue" as the build breaks. I have had to rollback to a previous version of the SDK.

WolfgangHG commented 11 months ago

I ran into a similar error with .NET SDK 7.0.404 (different stacktrace, but also inside "ResolvePackageAssets"):

The "ResolvePackageAssets" task failed unexpectedly.
System.ArgumentException: Invalid framework version '4.8/win7-x86'.
   at NuGet.Frameworks.NuGetFramework.ParseFrameworkNameParts(IFrameworkNameProvider mappings, String[] parts, String& framework, Version& version, String& profile)
   at NuGet.Frameworks.NuGetFramework.ParseFrameworkName(String frameworkName, IFrameworkNameProvider mappings)
   at Microsoft.NET.Build.Tasks.ResolvePackageAssets.CacheWriter.<>c__DisplayClass59_0.<WritePackageDependenciesDesignTime>b__6(String tg)
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
   at System.Linq.Enumerable.WhereListIterator`1.MoveNext()
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source)
   at Microsoft.NET.Build.Tasks.ResolvePackageAssets.CacheWriter.<WritePackageDependenciesDesignTime>g__GetPackageDiagnosticLevel|59_2(LockFileLibrary package)
   at Microsoft.NET.Build.Tasks.ResolvePackageAssets.CacheWriter.WritePackageDependenciesDesignTime()
   at Microsoft.NET.Build.Tasks.ResolvePackageAssets.CacheWriter.WriteItemGroup(Action writeItems)
   at Microsoft.NET.Build.Tasks.ResolvePackageAssets.CacheWriter.WriteItemGroups()
   at Microsoft.NET.Build.Tasks.ResolvePackageAssets.CacheWriter.Write()
   at Microsoft.NET.Build.Tasks.ResolvePackageAssets.CacheWriter.WriteToCacheFile()
   at Microsoft.NET.Build.Tasks.ResolvePackageAssets.CacheReader.CreateReaderFromDisk(ResolvePackageAssets task, Byte[] settingsHash)
   at Microsoft.NET.Build.Tasks.ResolvePackageAssets.CacheReader..ctor(ResolvePackageAssets task)
   at Microsoft.NET.Build.Tasks.ResolvePackageAssets.ReadItemGroups()
   at Microsoft.NET.Build.Tasks.ResolvePackageAssets.ExecuteCore()
   at Microsoft.NET.Build.Tasks.TaskBase.Execute()
   at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
   at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext()

The reason seems to be this package reference:

    <PackageReference Include="Azure.Identity" Version="1.6.0" />

It should report a warning "NU1903 Package 'Azure.Identity' 1.10.0 has a known high severity vulnerability". With recent package "1.10.4", the error does not happen.

The project has target frameworks net48;net6.0-windows.

Unfortunately, I could not reproduce it with a simple sample project. Is it related to the initial bugreport?

WolfgangHG commented 11 months ago

I could reproduce it, see attached sample. NugetTest.zip

Seems to happen only if the project has type "WinExe", and I defined .NET 7 SDK in "global.json". With SDK8, this does not happen, so this would be a possible workaround for me.