NuGet restoring PostSharp package before the build begins

此生再无相见时 提交于 2019-12-23 09:21:03

问题


I am using PostSharp and I have the following target description in my project file:

<Target Name="EnsurePostSharpImported" BeforeTargets="BeforeBuild" Condition="'$(PostSharp30Imported)' == ''">
  <Error Condition="!Exists('..\..\packages\PostSharp.3.1.33\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.33\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>

As far as I understand, this is added to the project when PostSharp is referenced through NuGet, and the error conditions check the following:

  • The first error condition breaks the build when PostSharp is not available (i.e. NuGet did not restore it successfully).
  • The second error condition breaks the build when PostSharp was successfully restored by NuGet on the last build but was therefore not included in the project, so therefore a rebuild is necessary.

BUT, if I have the following configuration in NuGet.Config and .csproj file, is the second error condition even necessary?

NuGet.Config file:

<configuration>
  <packageRestore>
    <!-- Allow NuGet to download missing packages -->
    <add key="enabled" value="True" />
    <!-- Automatically check for missing packages during build in Visual Studio -->
    <add key="automatic" value="True" />
  </packageRestore>
  ...
</configuration>

.csproj file:

<RestorePackages>true</RestorePackages>

As far as I understand, NuGet will then restore the missing packages BEFORE the build even starts. The second error condition will essentially break the build for no reason at all.

Note: I am using Visual Studio 2013 and NuGet 2.8.


回答1:


It depends on how the restore is done and which version of NuGet you have installed. It looks like the error messages are trying to cover three scenarios:

  1. Building without the MSBuild based package restore enabled (which is configured inside Visual Studio by right clicking the solution and selecting Enable Package restore).
  2. Building outside of Visual Studio when the MSBuild based package restore is not enabled.
  3. Building with Visual Studio using an old version of NuGet which does not support the automatic restore before a build.

If you are using the MSBuild based package restore then the restore will occur during the build and the PostSharp files will not be imported at this point so the $(PostSharp30Imported) will be empty and the second error message will be displayed. At least I suspect that is the case.

If you building from the command line and not using the MSBuild based package restore then you would see the first error message if the NuGet packages were missing.

If you are not using the MSBuild based package restore, and are building from within Visual Studio with a recent version of NuGet, then you are correct that the packages will be restored before anything is built at all. So the PostSharp imports should be available to MSBuild before it is even executed.




回答2:


As PostSharp dlls are required during msbuild loading (so targets referencing this dlls are available during build) they must be available during final call to msbuild.

While in VS it is acceptable to click build twice, I was using PostSharp in CI environment, and requirement to call build on solution two times was frustrating (first build restore nugets but also failed build due to error).

I ended up with separate build steps:

  1. Restore nuget Packages (this downloads PostSharp packages and return success code to environment): NuGet.exe restore SolutionWithProjectsUsingPostSharp.sln
  2. Build solution.



回答3:


You need to edit the second error condition in the target in the csproj

<Target Name="EnsurePostSharpImported" BeforeTargets="BeforeBuild" Condition="'$(PostSharp30Imported)' == ''">

<Error Condition="!Exists('....\packages\PostSharp.3.1.33\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.33\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>>

I have answered in detail in a different post at SO




回答4:


We are using 'old' MSBuild-Integrated package restore ( .nuget\NuGet.targets file is present) and normally do not store in source control packages, but rely on build to restore them for each build.

But for PostSharp on TeamCity build server I am getting error :

The build restored NuGet packages. Build the project again to include these packages in the build.

The simplest way is to explicitly include in source control packages\PostSharp.VerXXX.

Alternatively solution could be migrating to automatic package restore,
as it was advised in Issue Using PostSharp 3.x with NuGet Auto Restore




回答5:


Right click on the solution, Manage Nuget packages; and remove the packages you dont want .

This error also shows up , when trying to restore the packages from the web. Just connect your self to the internet and then try opening the project.

The errors went away for me on following the above steps.



来源:https://stackoverflow.com/questions/23557909/nuget-restoring-postsharp-package-before-the-build-begins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!