Jeremy Davis
Jeremy Davis
Sitecore, C# and web development
Article printed from: https://blog.jermdavis.dev/posts/2020/a-moment-of-confusion-with-generator-helix

A moment of confusion with generator-helix

Published 03 February 2020
Helix Sitecore ~1 min. read

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:

Solution Folder

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:

Solution Diff

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...

↑ Back to top