Issue Using PostSharp 3.x with NuGet Auto Restore
Asked Answered
D

1

1

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?

Desai answered 15/8, 2014 at 17:18 Comment(1)
Possible duplicate of NuGet restoring packages before the build beginsDrank
D
2

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.

Dorser answered 15/8, 2014 at 17:35 Comment(3)
The problem is that even though I can see these files have already been imported in my packages folder. Rebuilding again after receiving the message does not correct the issue either. Even if the project using PostSharp was still on the MSBuild integrated version (which I am pretty sure it is not) I would expect the second rebuild to work. Seems like PostSharp30Imported is always empty no matter what.Desai
Strange. That value is taken from the PostSharp.tasks file, which is imported by the PostSharp.targets file. Also I tried PostSharp version 3.1.48Dorser
This is strange indeed. You may try to enable Diagnostic build output in the VS options -> "Build and Run", look for PostSharp30Imported, and see if there are any clues. One possible reason is different PostSharp versions in the Import directive and in the Error condition, but your example shows 3.1.46 in both places.Surbeck

© 2022 - 2024 — McMap. All rights reserved.