Creating an ndrules file for use with NDepend
A quick article on configuring NDepend projects to use external rule files.
A long time ago, I wrote of some initial experiences using NDepend. Since then, I've done little with the product, although I did integrate it with Jenkins for both pipeline and freestyle jobs and so every time the CI builds it executes NDepend. However, the NDepend configuration used by these jobs is (probably) significantly out of date.
As I've just upgraded NDepend to the current version, I thought I'd better update the configurations. Fortunately when I created NDepend projects, I always edited them to use an external rules file and to change all the paths to relative. Absolute paths don't work well in CI scenarios and it always felt very wrong to me to have every single project duplicating all the rules - updating them would be painful experience.
The rules file I created dates from January 2017; I'm sure that something has changed in that period so I wanted to update it. The only problem is I didn't make any notes on how I created the rules file and it wasn't an obvious task to perform. This blog post is mainly notes for Forgetful Future Richard but I feel the information is useful enough to warrant it being a public post.
The main reason I think an external file should be used is simply to make updating rules easier, and to avoid having everything duplicated.
Although I never got to that point, NDepend allows you to combine both project and external rules, so I reasoned I would have all the stock rules in one externally referenced file and then add application specific rules at the project level.
NDepend doesn't seem to ship with an external rule file and so I need to manually create one. There also doesn't seem to be a straight forward way of creating one either, so the approach is a little convoluted. I will update this post if I find a better way.
These instructions are using the stand-alone Visual NDepend software, I haven't tested to see if the same applies when using the Visual Studio extension.
There's a lot of steps to jump through in order to get default rules, you can make it slightly easier by doing some of the work using a text editor.
<Queries>element to to the closing
</Queries>value - this will be almost at the end of the file so you can just select the remainder of the document and backtrack a few characters
This approach means you don't have to run a dummy analysis, and also means you don't end up with a superfluous Project Rules container group.
The GUI doesn't allow you to delete the default Project
Rules group so I do this directly by editing the
file in a text editor, rather than having to delete each child
group one by one.
</Queries>(I usually delete the whole thing, NDepend doesn't seem to mind)
<RuleFile>children to the
<RuleFiles>element for each file you wish to reference
This is an example element taken from one of my existing projects
When I compared the "new" stock rules I created against my previous one from 2017, there were over 600 differences. Some of these are documentation changes or priority, but there were also a bunch of brand new rules. Therefore if you are using external files it behoves you to check once in a while to see if NDepend has introduced stock rule changes that could be of benefit.
Like what you're reading? Perhaps you like to buy us a coffee?