Nuget 包失败 - 进程无法访问文件 'D:\a\a\*.nupkg'

Nuget Pack Failure - The process cannot access the file 'D:\a\1\a\*.nupkg'

Azure Pipeline for NuGet Pack 任务出错,使用 SDK 格式 .csproj,自动生成 .nuspec 文件:

The process cannot access the file 'D:\a\a\*.nupkg' because it is being used by another process.

System.IO.IOException: The process cannot access the file 'D:\a\a\*.nupkg' because it is being used by another process.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.File.InternalDelete(String path, Boolean checkHost)
   at NuGet.Commands.PackCommandRunner.BuildPackage(PackageBuilder builder, String outputPath, Boolean symbolsPackage)
   at NuGet.Commands.PackCommandRunner.BuildFromProjectFile(String path)
   at NuGet.CommandLine.PackCommand.ExecuteCommand()
   at NuGet.CommandLine.Command.ExecuteCommandAsync()
   at NuGet.CommandLine.Command.Execute()
   at NuGet.CommandLine.Program.MainCore(String workingDirectory, String[] args))
##[error]An error occurred while trying to pack the files.

正在构建的 .csproj 文件使用 TargetsForTfmSpecificBuildOutput:

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
    <Description>Provides a .....</Description>
  </PropertyGroup>


<PropertyGroup>
    <TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
  </PropertyGroup>
  <Target Name="CopyProjectReferencesToPackage" DependsOnTargets="BuildOnlySettings;ResolveReferences">
    <ItemGroup>
      <!-- Filter out unnecessary files -->
      <_ReferenceCopyLocalPaths Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')->WithMetadataValue('PrivateAssets', 'All'))" />
    </ItemGroup>
    <!-- Print batches for debug purposes -->
    <Message Text="Batch for .nupkg: ReferenceCopyLocalPaths = @(_ReferenceCopyLocalPaths), ReferenceCopyLocalPaths.DestinationSubDirectory = %(_ReferenceCopyLocalPaths.DestinationSubDirectory) Filename = %(_ReferenceCopyLocalPaths.Filename) Extension = %(_ReferenceCopyLocalPaths.Extension)" Importance="High" Condition="'@(_ReferenceCopyLocalPaths)' != ''" />
    <ItemGroup>
      <!-- Add file to package with consideration of sub folder. If empty, the root folder is chosen. -->
      <BuildOutputInPackage Include="@(_ReferenceCopyLocalPaths)" TargetPath="%(_ReferenceCopyLocalPaths.DestinationSubDirectory)" />
    </ItemGroup>
  </Target>

由于需要构建无法自行打包的遗留 dll,因此需要对 .csproj 文件进行上述修改。但是基于这个答案:

注意:去掉自定义包步骤还是报错

在此之前还有一个错误,不确定是否与此有关。

Error NU5128: Some target frameworks declared in the dependencies group of the nuspec and the lib/ref folder do not have exact matches in the other location. Consult the list of actions below: 
- Add a dependency group for .NETStandard2.0 to the nuspec 

查看下面的任务配置:

查看 Nuget 安装程序任务,这是安装程序运行的第一个任务:

我的问题的解决方案是使用 dotnet pack(因为我正在处理 .NetStandard 和 .NetCore 项目)而不是 nuget pack

特别是启用不构建选项。作为上一步构建解决方案并在其中构建项目。

我终于找到了这个问题的“原因”。 我无法在我这边使用 dotnet cli,因为我的项目不兼容,但我找到了解决方法。

这似乎与 https://github.com/NuGet/Home/issues/8713 有关,所以我使用“Nuget 工具安装程序”强制使用版本“5.2.x”,它按预期工作。

我不明白为什么这个问题自从 5 个小版本以来就出现了!