Build error caused by npm's node_modules


One of my team members encountered an unexpected error during compilation recently:
RazorGenerator.targets(31,9): error MSB4018: The "RazorCodeGen" task failed unexpectedly.
RazorGenerator.targets(31,9): error MSB4018: System.ArgumentException: Illegal characters in path.
It turned out to be a bogus error message triggered by the addition of a node.js-module. The package manager npm stores all project local packages in a subdirectory called node_modules, and this directory structure can get pretty deep as each module can have its own node_modules-directory. Once the path is longer than 260 characters (Windows API’s MAX_PATH), MSBuild breaks down and prints this bogus error.

The reason this occurs with Razor Generator is because of the following wildcard inclusion:
  <RazorSrcFiles Condition=" '@(RazorSrcFiles)' == '' " Include="**\*.cshtml" />
It doesn’t seem to help excluding node_modules either; at least I wasn’t successful. Perhaps MSBuild traverses the directory structure before it excludes an item.

One possible solution is to move the item group inside the target PrecompileRazorFiles, and then build it by filtering the Content item group (created by Visual Studio):
<Target Name="PrecompileRazorFiles"
    <RazorSrcFiles Include="@(Content)" Condition=" '@(RazorSrcFiles)' == '' And %(Extension) == '.cshtml' " />
    <RazorOutputFiles Include="@(RazorSrcFiles -> '$(RazorViewsCodeGenDirectory)%(RelativeDir)%(Filename)%(Extension).cs')" />
  <RazorCodeGen ProjectRoot="$(MsBuildProjectDirectory)"
What do you think? Would it be possible to include this change in an official release, or do you know of a better solution?

I know I can fix this myself by scanning a known subset of directories before including this target, or by using a variant of the above method: my own target with a "BeforeTargets" construct combined with a dummy RazorSrcFiles-item to prevent the default scan from occurring (for want of a better conditional property). But I figure others will run into the same problem eventually, so maybe you want to fix it as well.
Closed Mar 23, 2015 at 8:39 PM by davidebbo
This project has moved to https://github.com/RazorGenerator/RazorGenerator. Can you open there instead? Thanks!