.Net Core編譯時產生MSB4018: The "GenerateDepsFile" task failed unexpectedly. 錯誤

  • 985
  • 0
  • 2019-07-20

MSB4018

近期在編譯.Net Core時發生

Microsoft.NET.Sdk.targets(129, 5): [MSB4018] /usr/local/share/dotnet/sdk/2.2.301/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(129,5): error MSB4018: The "GenerateDepsFile" task failed unexpectedly.
System.ArgumentException: An item with the same key has already been added. Key: xxxx.xxx
   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey](TSource[] source, Func`2 keySelector, IEqualityComparer`1 comparer)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey](IEnumerable`1 source, Func`2 keySelector, IEqualityComparer`1 comparer)
   at Microsoft.Extensions.DependencyModel.DependencyContextWriter.WritePortableTarget(IReadOnlyList`1 runtimeLibraries, IReadOnlyList`1 compilationLibraries)
   at Microsoft.Extensions.DependencyModel.DependencyContextWriter.WriteTargets(DependencyContext context)
   at Microsoft.Extensions.DependencyModel.DependencyContextWriter.Write(DependencyContext context)
   at Microsoft.Extensions.DependencyModel.DependencyContextWriter.Write(DependencyContext context, Stream stream)
   at Microsoft.NET.Build.Tasks.GenerateDepsFile.ExecuteCore()
   at Microsoft.NET.Build.Tasks.TaskBase.Execute()
   at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
   at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask)


stackflow上面有人寫將nuget cache清空

也有人寫將.net core SDK更新

也有人說是MacOS底下才會產生的錯誤

要改MsBuild.dll

(https://rider-support.jetbrains.com/hc/en-us/articles/360004180039)

但實際試過後都沒有效果

最後發現在整個方案下的出錯的那個專案底下的csproj裡面

用文件編輯器打開來看多了一項

<HintPath>..\xxxx\bin\Release\netcoreapp2.1\xxxx.Api.dll</HintPath>

然後把這行註解或是刪除掉

就可以重新編譯了