如何修复丢失的 NuGet 包错误?

How to fix a missing NuGet package error?

我在 Git 下的 Visual Studio 中有一个 C++ 控制台应用程序,其中我有一个名为 LibrarySubmoduleSolution 的子模块静态库。此 LibrarySubmoduleSolution 使用 Windows 实施库 NuGet 包。

在我的 VCPPConsoleApplicationSolution 主解决方案中下载 Git 子模块并在解决方案中恢复 NuGet 包后,我收到以下错误:

Severity Code Description Project File Line
Suppression State Error This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is ..\packages\Microsoft.Windows.ImplementationLibrary.1.0.200519.2\build\native\Microsoft.Windows.ImplementationLibrary.targets. LibrarySubmoduleSolution
D:\Projects\Tests\VCPPConsoleApplicationSolution\LibrarySubmoduleSolution\LibrarySubmoduleSolution\LibrarySubmoduleSolution.vcxproj 166 Severity Code Description Project File Line
Suppression State Error C1083 Cannot open include file: 'wil/resource.h': No such file or directory
VCPPConsoleApplicationSolution
D:\Projects\Tests\VCPPConsoleApplicationSolution\LibrarySubmoduleSolution\LibrarySubmoduleSolution\SomeClass.h 3

我在 Google 和 Whosebug 上搜索了答案,甚至在我编辑 LibrarySubmoduleSolution.vcxproj 文件并将包文件夹从 ..\packages 更改为 $(SolutionDir)\packages 之后:

  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
  <ImportGroup Label="ExtensionTargets">
    <Import Project="$(SolutionDir)\packages\Microsoft.Windows.ImplementationLibrary.1.0.200519.2\build\native\Microsoft.Windows.ImplementationLibrary.targets" Condition="Exists('$(SolutionDir)\packages\Microsoft.Windows.ImplementationLibrary.1.0.200519.2\build\native\Microsoft.Windows.ImplementationLibrary.targets')" />
  </ImportGroup>
  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
    <PropertyGroup>
      <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
    </PropertyGroup>
    <Error Condition="!Exists('$(SolutionDir)\packages\Microsoft.Windows.ImplementationLibrary.1.0.200519.2\build\native\Microsoft.Windows.ImplementationLibrary.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\packages\Microsoft.Windows.ImplementationLibrary.1.0.200519.2\build\native\Microsoft.Windows.ImplementationLibrary.targets'))" />
  </Target>

我仍然无法构建错误:

Severity Code Description Project File Line Suppression State Error C1083 Cannot open include file: 'wil/resource.h': No such file or directory VCPPConsoleApplicationSolution D:\Projects\Tests\VCPPConsoleApplicationSolution\LibrarySubmoduleSolution\LibrarySubmoduleSolution\SomeClass.h 3

只有在我的主控制台应用程序项目中安装 Windows 实现库 NuGet 包后,它才能开始构建,没有错误。

快速重现错误的主要和子模块解决方案如下: https://github.com/KulaGGin/VCPPConsoleApplicationSolution https://github.com/KulaGGin/LibrarySubmoduleSolution

How to fix a missing NuGet package error?

我从 github 下载了您的项目,遇到了与您描述的相同的问题。

主要问题是您的 nuget 包已损坏或您的项目引用 Nuget 包有问题。

请按照以下步骤操作,我通过它们解决了问题。

建议

1) clean all nuget caches 或直接删除 C:\Users\xxx(current user name)\.nuget\packages

下的所有文件

2) 关闭 VS 实例,删除 .vs 解决方案文件夹下的隐藏文件夹和任何输出文件夹,如 x64 文件夹

3) 运行 Tools-->Nuget下的命令update-package -reinstall包管理器--> 包管理器控制台 以在您的项目中重新安装该包。

然后,错误就会消失。我跟着这些终于搞定了。

更新 1

=============================

很抱歉没有仔细观察您的问题。问题出在 xxx.vcxproj 文件中。

问题在:

<Error Condition="!Exists('$(SolutionDir)\packages\Microsoft.Windows.ImplementationLibrary.1.0.200519.2\build\native\Microsoft.Windows.ImplementationLibrary.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\packages\Microsoft.Windows.ImplementationLibrary.1.0.200519.2\build\native\Microsoft.Windows.ImplementationLibrary.targets'))" />

 <Import Project="$(SolutionDir)\packages\Microsoft.Windows.ImplementationLibrary.1.0.200519.2\build\native\Microsoft.Windows.ImplementationLibrary.targets" Condition="Exists('$(SolutionDir)\packages\Microsoft.Windows.ImplementationLibrary.1.0.200519.2\build\native\Microsoft.Windows.ImplementationLibrary.targets')" />

我想知道你为什么要使用 $(SolutionDir) 而不是 ..

在你的问题中,$(SolutionDir)\packages表示xxx(SolutionName)\packages

$(SolutonDir) 表示 xxxx\LibrarySubmoduleSolution\ 并且它已经包含 \.

解决方案

请删除$(SolutionDir)packages之间的\并改为:

$(SolutionDir)packages\Microsoft.Windows.ImplementationLibrary.1.0.200519.2\build\native\Microsoft.Windows.ImplementationLibrary.targets 在两个 xml 节点中。

================================

更新 2

此外,如果你的主工程VCPPConsoleApplicationSolution引用了LibrarySubmoduleSolution并使用了它的文件,你也应该在主工程中包含这样的库路径,否则, 它不会在主项目 VCPPConsoleApplicationSolution.

中找到 resource.h

实际上,当您在主项目VCPPConsoleApplicationSolution中引用LibrarySubmoduleSolution中的someclass.h时,someclass.h文件将被复制到VCPPConsoleApplicationSolution,在这样的环境下,你在LibrarySubmoduleSolution安装它时没有这样的库wil/resource.h。在主项目中,它没有安装那个nuget包(自动包含存在的include路径resource.h),所以会出现错误。

作为建议,

您可以右键单击 VCPPConsoleApplicationSolution 属性--> VC++ Dirctories--> 将 xxxx\VCPPConsoleApplicationSolution\packages\Microsoft.Windows.ImplementationLibrary.1.0.200519.2\include 添加到 Include Directories.

在主项目中安装该 nuget 包以自动包含库路径。