RazorGenerator.MsBuild fails to generate when using NuGet package restore and pacakges folder not in source control


I think there is a Catch-22 when using Package Restore and the packages folder is not checked in. The initial install of RazorGenerator.MsBuild adds the "..\packages**\tools\NuGet.import.targets" import to NuGet.targets, but when the packages folder is not checked in, the RazorGenerator NuGet.import.targets file does not yet exist when the build is initiated. Package restore pulls down the RazorGenerator.MsBuild package, which brings in the import targets file, but I think it it is too late in the build process.

This can be reproduced by installing the RazorGenerator.MsBuild package, deleting the packages folder, and then building the solution - view the build output at normal verbosity and verify that the PrecompileRazorFiles target is never invoked.

One potential solution is for the NuGet installation to add the RazorGenerator.targets file directly to the MVC project itself - then package restore is only responsible for pulling down the binaries.


davidebbo wrote Dec 5, 2012 at 5:43 PM

Right, it seems like the target file needs to be brought in by NuGet and commit. Pranav is the expert on RazorGenerator.MsBuild, so I'll let him comment further.

pranavkm wrote Jan 14, 2013 at 5:17 AM

Adding the contents of RazorGenerator.targets file (presumably that's what you meant) would certainly help, but it does make the update story a lot messier. I'll check if I can get some advice from the MsBuild experts at work and see if we can make the Imports statement trigger in a deferred manner.

pranavkm wrote Jan 23, 2013 at 4:15 PM

This gets trickier. The RazorGenerator target depends on the msbuild task in the assembly both of which need to be present at the time the target gets loaded. Even if the target ends up in the project, you'll need to have the assembly checked in alongside for it to work which would defeat the purpose of package restore.

I haven't followed up with any of the MsBuild experts I guess now's a good time to do that.

lscharen wrote Feb 1, 2013 at 9:24 PM

I worked around the issue by following the pattern that the MSBuild Community Tasks NuGet package uses.
  1. Create a top-level .razorgenerator solution folder
  2. Copy the RazorGenerator.targets and RazorGenerator.MsBuild.dll files
  3. Fix up .csproj import to point to the new location of the .targets file
This allows the NuGet package restore to run before trying to build that project.