使用Linux Build进行构建时,Azure Devops无法在nuget包中找到文件

问题描述

我们公司使用nuget软件包分发Stylecop规则集。这个nuget包中有一个props文件,我已经对其进行了修改,使其看起来像这样:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <PropertyGroup>
    <LinuxWarningFile>~/.nuget/packages/ourcompany.rulesets/0.0.0.0/content/OurCompanyRulesAsWarning.ruleset</LinuxWarningFile>
    <IsLinux Condition = "Exists($(LinuxWarningFile))">true</IsLinux>
    <IsLinux Condition = "$(IsLinux) == ''">false</IsLinux>

    <IsWindows Condition = "$(IsLinux) == false">true</IsWindows>
    <IsWindows Condition = "$(IsLinux) == true">false</IsWindows>
  </PropertyGroup>

  <PropertyGroup Condition=" '$(Configuration)' == 'Debug' And '$(IsLinux)'">
    <CodeAnalysisRuleSet>~/.nuget/packages/ourcompany.rulesets/0.0.0.0/content/OurCompanyRulesAsWarning.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>

  <PropertyGroup Condition=" '$(Configuration)' == 'Debug' And '$(IsWindows)'">
    <CodeAnalysisRuleSet>$(UserProfile)\.nuget\packages\ourcompany.rulesets\0.0.0.0\content\OurCompanyRulesAsWarning.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>

  <PropertyGroup Condition=" '$(Configuration)' == 'Release' And '$(IsLinux)'">
    <CodeAnalysisRuleSet>~/.nuget/packages/ourcompany.rulesets/0.0.0.0/content/OurCompanyRulesAsError.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>

  <PropertyGroup Condition=" '$(Configuration)' == 'Release' And '$(IsWindows)'">
    <CodeAnalysisRuleSet>$(UserProfile)\.nuget\packages\ourcompany.rulesets\0.0.0.0\content\OurCompanyRulesAsError.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>

</Project>

(显然,在软件包创建过程中,版本控制已得到纠正,并且一切正常。)

在Azure Devops上:

  • 当“池”设置为Windows Build时,它将找到正确的规则集。
  • 当“池”设置为“ Linux构建”时,它找不到规则集。

错误:警告MSB3884:找不到规则集文件“ .nuget \ packages \ ourcompany.rulesets \ 1.0.20230.1 \ content \ OurCompanyRulesAsError.ruleset”。

给出了〜,我认为它没有在第一个逻辑块中给定的路径中找到文件,已经将IsLinux评估为false,将IsWindows评估为true,并且正在使用包含UserProfile的路径在Linux构建中不存在。

通过阅读本文档(https://docs.microsoft.com/en-us/nuget/reference/nuget-config-file),我认为nuget软件包的默认路径为:

The location of the default global packages folder. The default is %userprofile%\.nuget\packages (Windows) or ~/.nuget/packages (Mac/Linux).

如何修复props文件,以便它可以找到Linux构建的规则集?

解决方法

您是否考虑过使用MSBuildThisFileDirectory属性来避免寻找路径?如果.props文件和.ruleset文件位于同一软件包中,则可以使用MSBuildThisFileDirectory和相对路径。

假定.props文件也位于包的子目录中(.ruleset看起来位于内容子目录中):

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup Condition="'$(Configuration)' == 'Debug'">
    <CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)/../content/OurCompanyRulesAsWarning.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)' == 'Release'">
    <CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)/../content/OurCompanyRulesAsError.ruleset</CodeAnalysisRuleSet>
  </PropertyGroup>
</Project>```

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...