Monday, 29 June 2015

npm please stop hurting Visual Studio

I don't know about you but I personally feel that the following sentence may well be the saddest in the English language:

2>ASPNETCOMPILER : error ASPRUNTIME: The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.

The message above would suggest there is some kind of ASP.Net issue going on. There isn't - the problem actually lies with Windows. It's not the first time it's come up but for those of you not aware there is something you need to know about Windows: It handles long paths badly.

There's a number of caveats which people may attach the above sentence. But essentially what I have said is true. And it becomes brutally apparent to you the moment you start using a few node / npm powered tools in your workflow. You will likely see that horrible message and you won't be able to get much further forward. Sigh. I thought this was the future...

This post is about how to deal with the long path issue when using npm with Visual Studio. This should very much be a short term workaround as npm 3.0 is planned to make long paths with npm a thing of the past. But until that golden dawn....

The Latest Infraction

I'm a big fan of Gulp and Bower. They rock. Steve Cadwallader wrote an excellent blog post about integrating Gulp into your Visual Studio build. Essentially the Gist of his post is this: forget using Task Runner Explorer to trigger your Gulp / Grunt jobs. No, actually plug it into the build process by tweaking your .csproj file. The first time I used this approach it was a dream come true. It just worked and I was a very happy man.

Since this approach was so marvellous I took a look at the demo / docs part of jQuery Validation Unobtrusive Native with a view to applying it there. I originally wrote this back in 2013 and at the time used NuGet for both server and client side package management. I decided to migrate it to use Bower for the client side packages (which I planned to combine with a Gulp script which was going to pull out the required JS / CSS etc as needed). However it wasn't the plain sailing I'd imagined. The actual switchover from NuGet to Bower was simple. Just a case of removing NuGet packages and adding their associated Bower counterpart. The problem came when the migration was done and I hit "compile". That's when I got to see 2>ASPNETCOMPILER : error ASPRUNTIME: The specified path, file name, or both are too long... etc

For reasons that I don't fully understand, Visual Studio is really upset by the presence in the project structure of one almighty long path. Oddly enough, not a path that's actually part of the Visual Studio project in question at all. Rather one that has come along as a result of our Gulp / Bower / npm shenanigans. Quick as a flash, I whipped out Daniel Schroeder's Path Length Checker to see where the problem lay:

And lo, the fault lay with Bower. Poor show, Bower, poor show.

rimraf to the Rescue

rimraf is "the UNIX command rm -rf for node". (By the way, what is it with node and the pathological hatred of capital letters?)

What this means is: rimraf can delete. Properly. So let's get it: npm install -g rimraf. Then at any time at the command line we can dispose of a long path in 2 shakes of lamb's tail.

In my current situation the contents of the node_modules folder is causing me heartache. But with rimraf in play I can get rid of it with the magic words: rimraf ./node_modules. Alakazam! So let's poke this command into the extra commands that I've already shoplifted from Steve's blog post. I'll end up with the following section of XML at the end of my .csproj:

  <Target Name="GulpBuild">
    <Exec Command="npm install" />
    <Exec Command="bower install" />
    <Exec Command="gulp" />
    <Exec Command="rimraf ./node_modules" />
  <Target Name="GulpClean">
    <Exec Command="npm install" />
    <Exec Command="gulp clean" />
    <Exec Command="rimraf ./node_modules" />
  <Target Name="CollectGulpOutput">
      <_CustomFiles Include="build\**\*" />
      <FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
    <Message Text="CollectGulpOutput list: %(_CustomFiles.Identity)" />

So let's focus on the important bits in the GulpBuild target:

  • <Exec Command="npm install" /> - install the node packages our project uses as specified in package.json. This will include Gulp and Bower. The latter package is going to contain super-long, Windows wrecking paths.
  • <Exec Command="bower install" /> - install the bower packages specified in bower.json using Bower (which was installed by npm just now).
  • <Exec Command="gulp" /> - do a little dance, make a little love, copy a few files, get down tonight.
  • <Exec Command="rimraf ./node_modules" /> - remove the node_modules folder populated by the npm install command.

With that addition of rimraf ./node_modules to the build phase the problem goes away. During each build a big, big Windows path is being constructed but then it's wiped again before it has chance to upset anyone. I've also added the same to the GulpClean target.

You are very welcome.

Friday, 19 June 2015

Back to the Future with Code First Migrations

Code First Migrations. They look a little like this in Visual Studio:

The thing I want you to notice about the image above is not the pithily named migrations. It isn't the natty opacity on everything but the migration files (which I can assure you took me to the very limits of my GIMP expertise). No, whilst exciting in themselves what I want you to think about is the order in which migrations are applied. Essentially how the __MigrationHistory table in SQL Server ends up being populated in this manner:

Because, myself, I didn't really think about this until it came time for me to try and change the ordering of some migrations manually. Do you know how migrations end up the order they do? I bet you don't. But either way, let's watch and see what happens to the pre-enlightenment me as I attempt to take a migration which appears before a migration I have created locally and move it to after that same migration.

Great Scott! It's clearly filename driven

That's right - it's blindingly obvious to me. All I need do is take the migration I want to move forwards in time and rename it in Visual Studio. So take our old migration ("2014 is so passé darling"):

And rename it to make it new and shiny ("2015! Gorgeous - I love it sweetie!"):

Perfection right? Wrong! What you've done makes not the slightest jot of difference.

Whoa, this is heavy! Gimme the project file

How could I be so dim? I mean it makes perfect sense - before the days of TypeScript's tsconfig.json the default ordering of *.ts files being passed to the TypeScript compiler was determined by the ordering of the *.ts files in the .csproj file. It must be the same for Code First Migrations.

So, simply spin up Notepad++ and let's play hack the XML until each file is referenced in the required order.

Well, I'm glad we sorted that out. A quick test to reassure myself of my astuteness. Drum roll.... Fail!! Things are just as they were. Shame on you John Reilly, shame on you.

Designer.cs... Your kids are gonna love it

I want you to look very carefully at this and tell me what you see. We're looking at the mysterious 201508121401253_AddSagacityToSage.Designer.cs file that sits underneath the main 201508121401253_AddSagacityToSage.cs file. What could it be.... Give in?

The IMigrationMetadata.Id property is returning 201408121401253_AddSagacityToSage. That is the old date! Remember? The passé one. If you change that property to line up with the file name change you're done. It works.

Let's say it together: "Automatic Migrations? Where we're going, we don't need Automatic Migrations."