运行 Nuget 还原时出现 NU1202 错误

2023-11-29

我有一个 .NET Framework 解决方案,正在尝试使用 Azure DevOps 上的管道进行设置。我在尝试恢复包时遇到错误:

NU1202: Package AppCenter.Analytics.Metrics 1.1.0 is not compatible with net40 (.NETFramework,Version=v4.0). Package AppCenter.Analytics.Metrics 1.1.0 supports: netstandard1.0 (.NETStandard,Version=v1.0)

还有很多这样的。没错 - 该软件包与 net40 不兼容,但这应该不重要。解决方案中有几个项目,但最旧的.NET版本是4.5,所以我无法弄清楚上面的v4.0来自哪里。其他可能相关的事情:

  1. 生成输出报告 NuGet 版本为 5.3.1.6268,MSBuild 版本为 16.3.2.50909。
  2. 该解决方案在本地正确恢复和构建。
  3. 我注意到 .sln.metaproj 文件中有一个“v4.0”:<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>。这似乎相关,但我不知道它来自哪里或如何更改它。 Azure devops 看不到这个文件,因为它被 git 忽略了,但我想知道是什么导致了 4.0 的出现。

以下是 pipeline.yaml 中的片段:

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release 2019'

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    command: 'restore'
    restoreSolution: '$(solution)'
    feedsToUse: 'select'
    vstsFeed: '#####'

这是有问题的 csproj 文件之一。针对多个版本的 Revit 并查找 Rhino dll 的位置有一些有趣的事情,但我看不到任何会导致尝试安装 .NET v4.0 包的内容:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Rhino7DefaultInstallDir>$([MSBuild]::GetRegistryValueFromView('HKEY_LOCAL_MACHINE\SOFTWARE\McNeel\Rhinoceros\7.0\Install', 'Path', null, RegistryView.Registry64))</Rhino7DefaultInstallDir>
    <Rhino7DebugInstallDir>$([MSBuild]::GetRegistryValueFromView('HKEY_CURRENT_USER\Software\McNeel\Rhinoceros\7.0-WIP-Developer-Debug-trunk\Install', 'Path', null, RegistryView.Registry64))</Rhino7DebugInstallDir>
    <Rhino7InstallDir>$([MSBuild]::ValueOrDefault('$(Rhino7DebugInstallDir)', '$(Rhino7DefaultInstallDir)'))</Rhino7InstallDir>
    <Rhino7DefaultPluginsDir>$([MSBuild]::GetRegistryValueFromView('HKEY_LOCAL_MACHINE\SOFTWARE\McNeel\Rhinoceros\7.0\Install', 'Default Plug-ins Folder', null, RegistryView.Registry64))</Rhino7DefaultPluginsDir>
    <Rhino7DebugPluginsDir>$([MSBuild]::GetRegistryValueFromView('HKEY_CURRENT_USER\Software\McNeel\Rhinoceros\7.0-WIP-Developer-Debug-trunk\Install', 'Default Plug-ins Folder', null, RegistryView.Registry64))</Rhino7DebugPluginsDir>
    <Rhino7PluginsDir>$([MSBuild]::ValueOrDefault('$(Rhino7DebugPluginsDir)', '$(Rhino7DefaultPluginsDir)'))</Rhino7PluginsDir>
  </PropertyGroup>
  <Import Project="..\..\packages\Costura.Fody.3.3.3\build\Costura.Fody.props" Condition="Exists('..\..\packages\Costura.Fody.3.3.3\build\Costura.Fody.props')" />
  <PropertyGroup Condition="$(Configuration.Contains('2018'))">
    <TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
    <RevitVersion>2018</RevitVersion>
  </PropertyGroup>
  <PropertyGroup Condition="$(Configuration.Contains('2019'))">
    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
    <RevitVersion>2019</RevitVersion>
  </PropertyGroup>
  <PropertyGroup Condition="$(Configuration.Contains('2020'))">
    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
    <RevitVersion>2020</RevitVersion>
  </PropertyGroup>
  <PropertyGroup>
    <!-- Common ruleset shared by all projects -->
    <CodeAnalysisRuleset>$(SolutionDir)solution.ruleset</CodeAnalysisRuleset>
  </PropertyGroup>
  <ItemGroup>
    <AdditionalFiles Include="$(SolutionDir)stylecop.json" />
  </ItemGroup>
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProductVersion>9.0.30729</ProductVersion>
    <SchemaVersion>2.0</SchemaVersion>
    <ProjectGuid>{D6C256D4-B518-464F-9E68-CB282202E846}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>Namespace</RootNamespace>
    <AssemblyName>AssemblyName</AssemblyName>
    <TargetFrameworkProfile />
    <NuGetPackageImportStamp>
    </NuGetPackageImportStamp>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug 2018|AnyCPU'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\Debug 2018\</OutputPath>
    <DefineConstants>TRACE;DEBUG;REVIT_2018</DefineConstants>
    <DebugType>full</DebugType>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
    <LangVersion>7.3</LangVersion>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release 2018|AnyCPU'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\Release 2018\</OutputPath>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DefineConstants>REVIT_2018</DefineConstants>
    <Optimize>true</Optimize>
    <LangVersion>7.3</LangVersion>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug 2019|AnyCPU'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\Debug 2019\</OutputPath>
    <DefineConstants>TRACE;DEBUG;REVIT_2018; REVIT_2019</DefineConstants>
    <DebugType>portable</DebugType>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
    <LangVersion>7.3</LangVersion>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release 2019|AnyCPU'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\Release 2019\</OutputPath>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DefineConstants>REVIT_2018; REVIT_2019</DefineConstants>
    <Optimize>true</Optimize>
    <LangVersion>7.3</LangVersion>
    <DebugType>portable</DebugType>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug 2020|AnyCPU'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\Debug 2020\</OutputPath>
    <DefineConstants>TRACE;DEBUG;REVIT_2018; REVIT_2019; REVIT_2020</DefineConstants>
    <DebugType>full</DebugType>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
    <LangVersion>7.3</LangVersion>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release 2020|AnyCPU'">
    <DebugSymbols>true</DebugSymbols>
    <OutputPath>bin\Release 2020\</OutputPath>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DefineConstants>REVIT_2018; REVIT_2019; REVIT_2020</DefineConstants>
    <Optimize>true</Optimize>
    <LangVersion>7.3</LangVersion>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="Microsoft.VisualBasic" />
    <Reference Include="PresentationCore" />
    <Reference Include="PresentationFramework" />
    <Reference Include="System" />
    <Reference Include="System.Data" />
    <Reference Include="System.Drawing" />
    <Reference Include="System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll</HintPath>
    </Reference>
    <Reference Include="System.Windows.Forms" />
    <Reference Include="System.Xml" />
    <Reference Include="WindowsBase" />
  </ItemGroup>
  <ItemGroup>
    ... Files ...
  </ItemGroup>
  <ItemGroup>
    <EmbeddedResource Include="Properties\Resources.resx">
      <SubType>Designer</SubType>
      <Generator>ResXFileCodeGenerator</Generator>
      <LastGenOutput>Resources.Designer.cs</LastGenOutput>
    </EmbeddedResource>
  </ItemGroup>
 ... More Files in ItemGroups ...
  <ItemGroup>
    ... Project References ...
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="AppCenter.Analytics.Metrics">
      <Version>1.1.0</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.AppCenter.Analytics">
      <Version>2.6.2</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.AppCenter.Crashes">
      <Version>2.6.2</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers">
      <Version>2.9.8</Version>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="StyleCop.Analyzers">
      <Version>1.1.118</Version>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>
  <Choose>
    <When Condition="$(Configuration.Contains('Debug'))">
      <ItemGroup>
        <Reference Include="Eto">
          <HintPath>$(Rhino7InstallDir)Eto.dll</HintPath>
          <Private>False</Private>
        </Reference>
        <Reference Include="RhinoCommon">
          <HintPath>$(Rhino7InstallDir)RhinoCommon.dll</HintPath>
          <Private>False</Private>
        </Reference>
        <Reference Include="Rhino.UI">
          <HintPath>$(Rhino7InstallDir)Rhino.UI.dll</HintPath>
          <Private>False</Private>
        </Reference>
        <Reference Include="GH_IO">
          <HintPath>$(Rhino7PluginsDir)Grasshopper\GH_IO.dll</HintPath>
          <Private>False</Private>
        </Reference>
        <Reference Include="Grasshopper">
          <HintPath>$(Rhino7PluginsDir)Grasshopper\Grasshopper.dll</HintPath>
          <Private>False</Private>
        </Reference>
        <Reference Include="RevitAPI">
          <HintPath>C:\Program Files\Autodesk\Revit $(RevitVersion)\RevitAPI.dll</HintPath>
          <Private>False</Private>
        </Reference>
        <Reference Include="RevitAPIUI">
          <HintPath>C:\Program Files\Autodesk\Revit $(RevitVersion)\RevitAPIUI.dll</HintPath>
          <Private>False</Private>
        </Reference>
        <Reference Include="AdWindows">
          <HintPath>C:\Program Files\Autodesk\Revit $(RevitVersion)\AdWindows.dll</HintPath>
          <Private>False</Private>
        </Reference>
      </ItemGroup>
    </When>
    <Otherwise>
      <ItemGroup>
        <PackageReference Include="RhinoCommon" Version="7.0.19274.12465-wip" IncludeAssets="compile; build" />
        <PackageReference Include="Grasshopper" Version="7.0.19274.12465-wip" IncludeAssets="compile; build" />
        <PackageReference Include="Revit_All_Main_Versions_API_x64" Version="$(RevitVersion).0.*" IncludeAssets="build; compile" />
      </ItemGroup>
    </Otherwise>
  </Choose>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <PropertyGroup>
    <ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>None</ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>
  </PropertyGroup>
</Project>

我无法弄清楚要检查什么或如何调试它。谢谢!


我注意到 .sln.metaproj 文件中有一个“v4.0”。我没能做到 弄清楚要检查什么或如何调试它。

一些可能有助于排除故障和解决问题的提示:

#1.我觉得.sln.metaproj在解决方案文件夹中和.csproj.metaproj项目文件夹中的内容应从源代码管理中排除。至少这些文件不建议发布到Azure Devops Repos.

检查他们的内容我们可以找到类似的内容<ProjectConfiguration Project="{xxx}" AbsolutePath="C:\Users\xxx\source\repos\...>,它是本地机器中的绝对路径。在构建管道中使用托管代理时,这些路径无效。

#2.另外,我检查了日志Nuget Restore任务,并确认内容xx.xx.metaproj文件不会影响nuget restore过程。所以我认为你的问题不是由此引起的。

#3 NU1202 表示该软件包与当前项目不兼容。检查日志Nuget Restore任务你会看到类似这样的内容:

enter image description here

错误消息将告诉我们哪个项目导致了问题。打开xx.csproj文件输入Devops Repos检查其内容。我假设项目的版本目标是 v4.0 或其 targetFrameworks 之一(多目标)是 v4.0。如果该项目是 sdk 格式的项目,您可能需要使用dotnet restore为此恢复包的任务。

Update:

See here, nuget restore命令不会接收和识别构建配置。所以对于这种情况,nuget无法访问条件为的propertyGroup$(Configuration.Contains('xxx'))。那么它相当于nuget restore xx.sln=>nuget 恢复一个项目TargetFrameworkVersion为空。然后nuget会认为该项目使用默认的4.0。所以就出现了这个问题。

重现步骤:

1.创建一个简单的.net Framework 4.7项目,将其添加到xx.csproj:

    <PackageReference Include="AppCenter.Analytics.Metrics">
      <Version>1.1.0</Version>
    </PackageReference>

2.评论<!--<TargetFrameworkVersion>v4.7</TargetFrameworkVersion>-->

3.保存全部并nuget restore xx.sln,出现同样的问题:

enter image description here

可能的解决方法:

nuget restore不会接受类似的东西nuget restore xx.sln -property:Configuration=xxx like nuget pack命令。所以这个问题总是会发生,除非我们定义valid无论哪种条件,propertyGroup 中的 TargetFramework 版本。 (我猜这不是你想要的,但这个问题在这种情况下无法避免)

所以我认为更好的选择是使用msbuild /t:restore代替nuget restore。对于 VS2017 及更高版本,msbuild 已包含restore选项本身。它适用于那些.net fx基于使用 PackageReference 格式的项目。

所以你可以使用带参数的 VS 构建任务/t:restore恢复包而不是使用 Nuget 恢复(这不适合您的自定义 csproj 脚本)。

enter image description here

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

运行 Nuget 还原时出现 NU1202 错误 的相关文章