A moment of confusion with “generator-helix”

Because of the old maxim “anything you do more than once should be automated”, we all find ourselves working with tools to auto-generate projects and solutions for Helix architecture these days. Mostly these tools work fine – but every so often you can bump your head against unexpected behaviour – as I did recently:

My confusion

I was doing some experimentation with a helix solution that used the Generator-Helix package for Yeoman. Everything was working fine for a while – but I needed to make a change I wasn’t sure about, so for simplicity’s sake I took a copy of the solution file before I ran the next command – and suddenly my Yeoman commands stopped updating the solution open in Visual Studio:

Cue some head scratching…

The issue

After some digging through the code deployed by NPM, it turns out this tool is using a simple “find the first file in the solution folder that matches *.sln” to decide which solution to update. It doesn’t use the Solution’s name when finding the file to modify. Looking into the code for the generator (in “generator-helix/generators/add” wherever NPM put the module), you find this in the writing() function in index.js:

const files = fs.readdirSync(this.destinationPath());
const SolutionFile = files.find(file => file.toUpperCase().endsWith(".SLN"));
const scriptParameters = '-SolutionFile \'' + this.destinationPath(SolutionFile) + '\' -Name ' + this.settings.LayerPrefixedProjectName + ' -Type ' + this.layer + ' -ProjectPath \'' + this.settings.ProjectPath + '\'' + ' -SolutionFolderName ' + this.templatedata.projectname;

This is the code that gets called when you run “yo helix:add” – So because my backup file was coming up first in the solution folder’s file listing, the change was getting made to my backup file – not to my active solution. A behaviour I could quickly check with a diff between these two files, to show that it was the copy which had recevied my updates:

Confusion resolved! And a simple change to rename the backup file so it would sort last fixed my problem.

And yes, I know – I could just use Source Control to undo changes rather than messing with backups. But sometimes “the lazy answer” teaches you something new. And it’s not uncommon for me to find source trees with multiple solution files…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.