dotnet pack recently failing

Viewed 1478

We have been getting NU5118 errors for a long time in Azure Devops but the pack step was passing

Starting: Pack
==============================================================================
Task         : .NET Core
Description  : Build, test, package, or publish a dotnet application, or run a custom dotnet command
Version      : 2.167.1
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/build/dotnet-core-cli
==============================================================================
C:\windows\system32\chcp.com 65001
Active code page: 65001
e:\vsts\a\_tool\dotnet\dotnet.exe pack e:\vsts\a\4674\s\src\Bentley.Interoperability.APM\Bentley.Interoperability.APM.csproj --configuration Release -o e:\vsts\a\4674\a/nuget /p:Version=1.0.86.0 --no-build --no-restore
Microsoft (R) Build Engine version 16.5.0+d4cbfca49 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

e:\vsts\a\_tool\dotnet\sdk\3.1.201\Sdks\NuGet.Build.Tasks.Pack\build\NuGet.Build.Tasks.Pack.targets(198,5): error NU5118: File 'C:\Users\prgremotebuilder\.nuget\packages\bentley.interoperability.identityservice.oracle\1.0.69\contentFiles\any\netstandard2.0\scripts\oracle\Create-Identity.sql' is not added because the package already contains file 'content\Create-Identity.sql' [e:\vsts\a\4674\s\src\Bentley.Interoperability.APM\Bentley.Interoperability.APM.csproj]
e:\vsts\a\_tool\dotnet\sdk\3.1.201\Sdks\NuGet.Build.Tasks.Pack\build\NuGet.Build.Tasks.Pack.targets(198,5): error NU5118: File 'C:\Users\prgremotebuilder\.nuget\packages\bentley.interoperability.identityservice.oracle\1.0.69\contentFiles\any\netstandard2.0\scripts\oracle\Create-Identity.sql' is not added because the package already contains file 'contentFiles\any\netstandard2.1\Create-Identity.sql' [e:\vsts\a\4674\s\src\Bentley.Interoperability.APM\Bentley.Interoperability.APM.csproj]
e:\vsts\a\_tool\dotnet\sdk\3.1.201\Sdks\NuGet.Build.Tasks.Pack\build\NuGet.Build.Tasks.Pack.targets(198,5): error NU5118: File 'C:\Users\prgremotebuilder\.nuget\packages\bentley.interoperability.identityservice.oracle\1.0.69\contentFiles\any\netstandard2.0\scripts\oracle\Identity-SP.sql' is not added because the package already contains file 'content\Identity-SP.sql' [e:\vsts\a\4674\s\src\Bentley.Interoperability.APM\Bentley.Interoperability.APM.csproj]
e:\vsts\a\_tool\dotnet\sdk\3.1.201\Sdks\NuGet.Build.Tasks.Pack\build\NuGet.Build.Tasks.Pack.targets(198,5): error NU5118: File 'C:\Users\prgremotebuilder\.nuget\packages\bentley.interoperability.identityservice.oracle\1.0.69\contentFiles\any\netstandard2.0\scripts\oracle\Identity-SP.sql' is not added because the package already contains file 'contentFiles\any\netstandard2.1\Identity-SP.sql' [e:\vsts\a\4674\s\src\Bentley.Interoperability.APM\Bentley.Interoperability.APM.csproj]
  Successfully created package 'e:\vsts\a\4674\a\nuget\Bentley.Interoperability.APM.1.0.86.nupkg'.
Info: Azure Pipelines hosted agents have been updated to contain .Net Core 3.x (3.0 and 3.1) SDK/Runtime along with 2.1. Unless you have locked down a SDK version for your project(s), 3.x SDK might be picked up which might have breaking behavior as compared to previous versions. 
Some commonly encountered changes are: 
If you're using `Publish` command with -o or --Output argument, you will see that the output folder is now being created at root directory rather than Project File's directory. To learn about more such changes and troubleshoot, refer here: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/build/dotnet-core-cli?view=azure-devops#troubleshooting
Finishing: Pack

But now it is failing:

It has the same error messages, but also now has 'Dotnet command failed with non-zero exit code on the following project...'

Starting: Nuget: Pack
==============================================================================
Task         : .NET Core
Description  : Build, test, package, or publish a dotnet application, or run a custom dotnet command
Version      : 2.173.0
Author       : Microsoft Corporation
Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/build/dotnet-core-cli
==============================================================================
C:\windows\system32\chcp.com 65001
Active code page: 65001
Info: .NET Core SDK/runtime 2.2 and 3.0 are now End of Life(EOL) and have been removed from all hosted agents. If you're using these SDK/runtimes on hosted agents, kindly upgrade to newer versions which are not EOL, or else use UseDotNet task to install the required version.
e:\vsts\b\_tool\dotnet\dotnet.exe pack e:\vsts\b\6232\s\src\Bentley.Interoperability.APM\Bentley.Interoperability.APM.csproj --configuration Release -o e:\vsts\b\6232\a/nuget /p:Version=1.0.244.0 --no-build
Microsoft (R) Build Engine version 16.7.0-preview-20360-03+188921e2f for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

e:\vsts\b\_tool\dotnet\sdk\3.1.401\Sdks\NuGet.Build.Tasks.Pack\build\NuGet.Build.Tasks.Pack.targets(198,5): error NU5118: File 'C:\Users\prgremotebuilder\.nuget\packages\bentley.interoperability.identityservice.oracle\1.0.69\contentFiles\any\netstandard2.0\scripts\oracle\Create-Identity.sql' is not added because the package already contains file 'content\Create-Identity.sql' [e:\vsts\b\6232\s\src\Bentley.Interoperability.APM\Bentley.Interoperability.APM.csproj]
e:\vsts\b\_tool\dotnet\sdk\3.1.401\Sdks\NuGet.Build.Tasks.Pack\build\NuGet.Build.Tasks.Pack.targets(198,5): error NU5118: File 'C:\Users\prgremotebuilder\.nuget\packages\bentley.interoperability.identityservice.oracle\1.0.69\contentFiles\any\netstandard2.0\scripts\oracle\Create-Identity.sql' is not added because the package already contains file 'contentFiles\any\netstandard2.1\Create-Identity.sql' [e:\vsts\b\6232\s\src\Bentley.Interoperability.APM\Bentley.Interoperability.APM.csproj]
e:\vsts\b\_tool\dotnet\sdk\3.1.401\Sdks\NuGet.Build.Tasks.Pack\build\NuGet.Build.Tasks.Pack.targets(198,5): error NU5118: File 'C:\Users\prgremotebuilder\.nuget\packages\bentley.interoperability.identityservice.oracle\1.0.69\contentFiles\any\netstandard2.0\scripts\oracle\Identity-SP.sql' is not added because the package already contains file 'content\Identity-SP.sql' [e:\vsts\b\6232\s\src\Bentley.Interoperability.APM\Bentley.Interoperability.APM.csproj]
e:\vsts\b\_tool\dotnet\sdk\3.1.401\Sdks\NuGet.Build.Tasks.Pack\build\NuGet.Build.Tasks.Pack.targets(198,5): error NU5118: File 'C:\Users\prgremotebuilder\.nuget\packages\bentley.interoperability.identityservice.oracle\1.0.69\contentFiles\any\netstandard2.0\scripts\oracle\Identity-SP.sql' is not added because the package already contains file 'contentFiles\any\netstandard2.1\Identity-SP.sql' [e:\vsts\b\6232\s\src\Bentley.Interoperability.APM\Bentley.Interoperability.APM.csproj]
##[error]Error: The process 'e:\vsts\b\_tool\dotnet\dotnet.exe' failed with exit code 1
Info: Azure Pipelines hosted agents have been updated to contain .Net Core 3.x (3.1) SDK/Runtime along with 2.1. Unless you have locked down a SDK version for your project(s), 3.x SDK might be picked up which might have breaking behavior as compared to previous versions. 
Some commonly encountered changes are: 
If you're using `Publish` command with -o or --Output argument, you will see that the output folder is now being created at root directory rather than Project File's directory. To learn about more such changes and troubleshoot, refer here: https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/build/dotnet-core-cli?view=azure-devops#troubleshooting
##[error]Dotnet command failed with non-zero exit code on the following projects : e:\vsts\b\6232\s\src\Bentley.Interoperability.APM\Bentley.Interoperability.APM.csproj
Finishing: Nuget: Pack
4 Answers

I can confirm that we've seen the exact same behavior, without any changes to the build pipeline or source code from us. The first failing build also had Version : 2.173.0 in the banner at the top of the task log.

In our case, the error was due to multiple dependent nuget packages that all contained a .targets file with an element as this one:

<Content Include="Deployment\**\*.*" Pack="true" PackagePath="\..\Deployment\" />

The error has been logged by the task all along, only the prior versions ignored it and still passed the build. This works since nuget pack still emits the .nupkg file despite the error.

Our workaround was to add a Remove element before the include:

<Content Remove="Deployment\**\*.*" />
<Content Include="Deployment\**\*.*" Pack="true" PackagePath="\..\Deployment\" />

The logs you provided show that the error occurrs because your .NET Core SDK/runtime version is too old.

The recommended solution is to add a UseDotNet task before your original tasks:

- task: UseDotNet@2
  inputs:
    version: {version}

You can also use self-hosted agent with the .NET Core SDK/runtime version you want instead of hosted agent or use newer versions. But both solutions significantly change the building environment, so they need to be used carefully according to your condition.

dotnet pack recently failing

According to the error message:

error NU5118: File 'Create-Identity.sql' is not added because the package already contains file 'content\Create-Identity.sql'

It seems you have IncludeSource=true in the project and there are several files with the same file name. That the reason why it said the package already contains file.

To resolve this issue, the simplest way is remove the same file or rename them if possible.

If we could not remove or rename those files, we could try to cusom a nuspec file with those files in different target path.

If your project is targeting multiple frameworks, chances are you're missing the corresponding property groups for the Razor Language Version required for each target framework. Suppose your project file is this:

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

  <PropertyGroup>
    <TargetFrameworks>netstandard2.0;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
  </PropertyGroup>

</Project>

This might generate the kind of warning you see in the build output, if you have static files in your project. To remedy this issue, you'll have to add the missing property groups for each target framework. The above project file should look like the following:

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

  <PropertyGroup>
    <TargetFrameworks>netstandard2.0;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
  </PropertyGroup>

  <PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'netcoreapp3.1'">
    <RazorLangVersion>3.0</RazorLangVersion>
  </PropertyGroup>

  <PropertyGroup Condition="'$(TargetFramework)' == 'net5.0'">
    <RazorLangVersion>5.0</RazorLangVersion>
  </PropertyGroup>

  <PropertyGroup Condition="'$(TargetFramework)' == 'net6.0'">
    <RazorLangVersion>6.0</RazorLangVersion>
  </PropertyGroup>
</Project>

Take a look at this Github issue https://github.com/NuGet/Home/issues/10253 for detailed instructions on how to reproduce it.

Related