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.
- Open Visual NDepend
- Open the File menu and select New Project
- From the New NDepend Project dialog, enter a dummy name and file name and click OK
- When the Project Properties window appears, click Add VS Solution or Project
- Click Browse and then choose any existing Visual Studio solution
- Press F5 to run the analysis
- When the analysis is complete, click the Rule File drop down in the Queries and Rules Explorer window and then click Create Rule File
- Enter the filename of the file and then click OK to create an empty file (make sure the filename is unique or you'll get an error)
- Right click Project Rules from the Queries and Rules Explorer tree and click Copy
- Find the node for the new rules file you just created, right click it and click Paste Group
- Exit Visual NDepend and delete the dummy project
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.
- Follow steps 1 to 3 above to create a dummy project
- Exit Visual NDepend
- Open the dummy project you created in a text editor of your choice
- Do a search for
- Select from the
<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
- Copy this into a new text file
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.
- Open a NDepend project in a text editor
- Delete the all text between
</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
- Save the file
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.
- 2019-04-16 - First published
- 2020-11-22 - Updated formatting
Like what you're reading? Perhaps you like to buy us a coffee?