【问题标题】:Issue Using PostSharp 3.x with NuGet Auto Restore将 PostSharp 3.x 与 NuGet 自动还原一起使用的问题
【发布时间】:2014-08-15 17:18:01
【问题描述】:

我有许多项目的解决方案,其中一些使用 PostSharp。我最近从使用 NuGet MSBuild 集成还原切换到 NuGet 自动还原。这会导致在构建开始之前为所有包恢复所有必需的包。这很好用,除了现在我经常遇到 PostSharp 会因错误而导致构建失败的问题:

构建恢复了 NuGet 包。再次构建项目以在构建中包含这些包。如需更多信息,请参阅http://www.postsharp.net/links/nuget-restore

当我编辑项目文件时,我看到以下条目:

  <Import Project="..\..\packages\PostSharp.3.1.46\tools\PostSharp.targets" Condition="Exists('..\..\packages\PostSharp.3.1.46\tools\PostSharp.targets')" />
  <Target Name="EnsurePostSharpImported" BeforeTargets="BeforeBuild" Condition="'$(PostSharp30Imported)' == ''">
    <Error Condition="!Exists('..\..\packages\PostSharp.3.1.46\tools\PostSharp.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://www.postsharp.net/links/nuget-restore." />
    <Error Condition="Exists('..\..\packages\PostSharp.3.1.46\tools\PostSharp.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://www.postsharp.net/links/nuget-restore." />
  </Target>

如您所见,当 NuGet 包存在于我的机器上但也不存在于我的机器上时,会出现错误情况。似乎这些条件已被评估,因为变量 $(PostSharp30Imported) 从未设置。我假设这取决于恢复的 MSBuild 集成版本,但我没有足够的 MSBuild 经验来确定。

我可以通过简单地删除项目文件中的第二个错误条件来解决该问题(因为我保证在项目构建时拥有这些文件),但似乎任何升级或添加 PostSharp 原因项目文件恢复到旧方式并阻止我的解决方案构建。

这是 PostSharp 中的错误,还是在使用不会导致此问题的 NuGet 自动恢复时我应该使用 PostSharp 的其他方式?

【问题讨论】:

标签: c# visual-studio nuget postsharp


【解决方案1】:

如果您完全没有 migrated from the old MSBuild based package restore,那么如果您使用的是最新版本的 NuGet,则不应看到该错误消息。 Visual Studio 检查 .nuget/NuGet.targets 文件是否存在,如果该文件存在,则不使用新的基于 Visual Studio 的包还原。

较新的自动包还原将在您构建项目时但在 MSBuild 启动之前发生。这意味着将在 MSBuild 尝试编译您的项目之前导入 PostSharp MSBuild 目标文件中定义的各种 MSBuild 属性。在这种情况下,应定义 PostSharp30Imported,以便从不运行自定义目标。只有在 MSBuild 编译项目时 PostSharp.targets 文件不存在时才会运行它。

错误消息对于较旧的基于 MSBuild 的包还原是正确的,因为该生成将在 MSBuild 运行时还原目标文件,因此在第一次生成时它们将不可用,因此不会导入。

创建一个新项目,添加 PostSharp,删除所有包,然后重新编译项目我没有看到错误消息。

【讨论】:

  • 问题是即使我可以看到这些文件已经被导入到我的包文件夹中。收到消息后再次重建也不能解决问题。即使使用 PostSharp 的项目仍在 MSBuild 集成版本上(我很确定它不是),我也希望第二次重建能够工作。似乎 PostSharp30Imported 无论如何都是空的。
  • 奇怪。该值取自 PostSharp.tasks 文件,该文件由 PostSharp.targets 文件导入。我也试过 PostSharp 3.1.48 版
  • 这确实很奇怪。您可以尝试在VS选项->“构建并运行”中启用诊断构建输出,查找PostSharp30Imported,看看是否有任何线索。一个可能的原因是 Import 指令和错误条件中的 PostSharp 版本不同,但您的示例在两个地方都显示 3.1.46。
猜你喜欢
  • 1970-01-01
  • 2015-06-12
  • 1970-01-01
  • 2015-06-06
  • 2018-08-27
  • 1970-01-01
  • 1970-01-01
  • 2012-05-25
  • 1970-01-01
相关资源
最近更新 更多