Cake build issue reporting with PRCA

|
I have covered Cake build in one of my talks I did for a local user group I participate in Copenhagen. However, most of that just explained what Cake build is. If you are reading this and have no idea what Cake build is, I highly recommend looking at the Cake homepage and also watching this introduction by Gary Ewan Park, which explains very well what is great about Cake.

Once you have set up your Cake build, you may wonder how to process all the warnings and issues that MSBuild throws at you. You may also be running the ReSharper command line analysis tool which finds issues with your code. How do you combine all these results and report them or go through them to figure out what is going on?

Pascal Berger has written some great add-ins for Cake, which helps you with this, called Pull Request Code Analysis (PRCA). Let's try set it up for both MSBuild and ReSharper code Analysis.

Add-ins and tools

In your build script you need to add a couple of tools and addins.


In the snippet above, we start by adding the ReSharper CommandLineTools, for analyzing with either the default ReSharper rules or one or more rule sets that you have defined for your solution.
We also add MSBuild Extension Pack, which allows us to use an XML file format that PRCA knows how to parse. The default logging format does not work.

Then we add the relevant PRCA addins. In this case I am only interested in catching issues with my code. However, PRCA also supports reporting the issues it finds into TFS/VSTS pull requests.

MSBuild

Having added the required add-ins and tools for all this to work. Let's add some settings to our MSBuild, such that it can log the build in the format PRCA understands.

Here the important parts is the call to WithLogger, which sets up file logging to XML. The rest is up to you to figure out. I like to pass along the configuration as arguments in my scripts, which is where the configuration and platform variables come from. In this case we are logging to msbuildLogPath which is up to you to decide where is. It could for instance be in an artifacts folder.

ReShaper Inspect Code

Now let's add some static analysis into the mix and lets get some results from the ReSharper CLI tools. A ReShaper license is not required to use the CLI tool, so there is basically no reason not to use it if you want to improve your code.

The CLI tool just like the plugin for Visual Studio also supports extensions, which is great for adding additional analysis. This can help you catch spelling mistakes and other cool additions through the extensions.


We pass along similar MSBuild properties as we did in our build. The Profile property here is the ReSharper settings file. You can configure multiple layers of settings here, similarly to what ReShaper allows you in the VS extension.
I have chosen not to throw exceptions when finding violations, so I can report them at the end of a build if the rest of it succeeds. You can see I added a couple of extensions in the Extensions property. One for checking spelling and ensuring async suffix on async methods.
An important note here is the contents of ArgumentCustomization. On my environment, the CLI tool seems confused about which MSBuild version to use. Even though the documentation claims to always use the latest version, it still picked up an old version. Hence, I had to specify --toolset=15.0 myself to make it work.
The InspectCode tool takes a little while to run. In my case here it dumps the results in a log file at inspectReportFilePath. Just like with MSBuild, you could put that in artifacts or where ever you like.

Reading issues

Now to tie this blog post up, we now need to read the issues logged from both the build and the ReSharper code inspection. This is what PRCA excels at.


Here I simply check existence of the msbuild log and the code inspection report and run the ReadIssues tool. From this I get a IEnumerable<ICodeAnalysisIssue> which gives you the path to the file where the issue is in, the line, the message, which rule it violates and url to the rule documentation. From this you can get a very good idea about how to resolve the issue being reported.

There are plans on adding tools to create reports in various formats from this enumerable, currently you need to do something about the issues yourself, which highly depends on the environment you use to report issues, make pull requests etc.
If you are using TFS/VSTS there are built in methods to report these. More information can be found in the PRCA docs.

When you run the PRCA tool it will look something like this in your cake output.
As you see this project I've run it on has a lot of issues 😢