问题
I have a solution with many projects, some of which use PostSharp. I've recently switched from using NuGet MSBuild integrated restore to NuGet Auto Restore. This causes all necessary packages to be restored for all packages before a build starts. This works great, except for now I come across an issue frequently where PostSharp will fail the build with the error:
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.
When I edit the project file I see the following entry:
<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>
As you can see, there is an error condition for when the NuGet package exists on my machine and also doesn't exist on my machine. It appears that these conditions are evaluated because the variable $(PostSharp30Imported) is never set. I'm assuming this something that is dependent on the MSBuild integrated version of restore, but I don't have enough MSBuild experience to know for sure.
I am able to work around the issue by simply removing the second Error condition in the project file (since I am guaranteed to have the files by the time the project builds), but it seems like any upgrades or additions of PostSharp cause the project file to revert to the old way and prevents my solution from building.
Is this a bug in PostSharp, or is there some other way I should be working with PostSharp when using NuGet auto restore that does not cause this issue?
回答1:
If you have completely migrated from the old MSBuild based package restore away then you should not see that error message if you are using a recent version of NuGet. Visual Studio checks for the existence of the .nuget/NuGet.targets file and does not use the new Visual Studio based package restore if this file exists.
The newer automatic package restore will occur when you build the project but before MSBuild is started. This means the various MSBuild properties that are defined in the PostSharp MSBuild targets file will be imported before MSBuild tries to compile your project. In this case the PostSharp30Imported should be defined so that custom target is never run. It would only be run if the PostSharp.targets file did not exist whilst MSBuild was compiling the project.
The error message is correct for the older MSBuild based package restore since the build would restore the targets file whilst MSBuild was running so they would not be available, and therefore not imported, for the first build.
Creating a new project, adding PostSharp, deleting all the packages, then recompiling the project I see no error message.
来源:https://stackoverflow.com/questions/25331059/issue-using-postsharp-3-x-with-nuget-auto-restore