## ReSharper Code Analysis Goes Beyond Visual Studio

March 26th, 2013 by Dmitry Matveev

One of ReSharper’s most notable features, Code Analysis, is now unleashed and ready to hunt for bad and dead code—without even opening Visual Studio. Dubbed as InspectCode, it is as simple as a command-line tool can be and requires a minimum of one parameter—your solution file. But as it runs it will apply all of ReSharper’s code inspections—that’s over 1,400 of them—to code in all languages supported by ReSharper!
Can’t wait to try it out? Find the latest ReSharper 8 EAP build, download and unzip the Command Line Tools package, and then run InspectCode [SolutionFile].

While the tool works its way through your code, let’s take a closer look at its features and use cases.

### Output

Execution of InspectCode results in an XML file with code issues found within the specified scope (the whole solution or particular projects). The XML comprises two parts:

• The list of found issue types where each type has a description, severity level, and a link to the corresponding Code Inspection Wiki entry where available. Also, each issue has a category that can be used to group similar issues.
• The list of found issues where each issue has its type and place in a file. The issues are grouped by projects.

How the output should be processed is up to you. But here are a couple of recommended next steps: transform the output to an HTML report, or generate some messages on your continuous integration (CI) server based on number and types of detected issues.

### Usage Scenarios

Now let’s see how we can use the tool and what exactly we can do with its output. It may be helpful to run it on your local machine, but only if you don’t have ReSharper, because with ReSharper you can get inspection results for a selected scope with a couple of clicks and, if necessary, export detected issues to a report file. A more promising case is to use InspectCode on a CI server where you can integrate it in the build script and add code inspection results to your build reports and messages.

The JetBrains Teamcity team, who is pioneering the use of this tool right now, has created the following visual presentation of the code issues detected by InspectCode:

By the way, if you come up with more usage scenarios as you try out InspectCode, please share them with us.

### Settings

If you have previously worked on the target solution with ReSharper, you may have already configured code inspections settings. If so, InspectCode will find your custom settings in .DotSettings files and apply them. If there are no settings files, then the default severity levels will be used for all inspections.

Besides custom severity levels for code inspections, InspectCode looks for the following settings in .DotSettings files:

If you want to configure InspectCode on a CI server, you can make all configurations locally with ReSharper, save the settings to the Solution Team-Shared layer, and then commit the resulting SolutonName.sln.DotSettings file in your VCS. InspectCode on the server will find and apply these settings.

As an alternative, you can configure InspectCode by specifying a shared .DotSettings file through the /profile (/p) parameter. In this case the specified settings layer will override settings in all other layers. (See this post to learn how to create and share a common settings file.)

### Optional parameters

We have already mentioned two optional parameters: /profile (/p) and /no-swea. Among other optional parameters (which you can see by typing InspectCode /help) the following may come in handy:

• /output (/o) - lets you set the output file. By default, the output file is saved in the %TEMP% directory.
• /project - allows analyzing particular project(s) instead of the whole solution. After this parameter, you can type a project name or a wildcard that matches several projects within your solution.
• /no-buildin-settings - can be used to ignore settings from the Solution Team-Shared layer (SolutonName.sln.DotSettings file).
• /caches-home - lets you specify a custom location for the data that InspectCode caches. By default, the %TEMP% directory is used, unless there are settings files, in which case the one specified there is used. This parameter can be helpful if you want to use a fast SSD disk for the cache or if you want to store all your build processing data in a single place.

So our command line could look like this:
InspectCode C:\Projects\ReSharper\resharper.sln /project=Documents /o="C:\temp data\Results.xml" /no-swea

### Q & A

That’s about all for the introduction of InspectCode. Now let us try and answer some questions you might ask:

• How are command line tools licensed?
The EAP version is a free 30-day trial. We are still working on the licensing policy and it will be announced soon.
• Can I configure InspectCode without ReSharper?
Currently, configuration without the use of ReSharper is restricted to some basic settings like enabling solution-wide analysis, selecting projects to be analyzed, choosing the output file, and specifying cache location.
• Does InspectCode consider the Visual Studio project setting ‘Treat warnings as errors’?
Yes. It will change the severity of issues corresponding to compiler warnings.
• Will InspectCode analyze a solution that fails to build?
Yes. In this case you will see all compiler errors in the output in addition to ReSharper-specific issues. The only requirement for correct analysis is that all dependent libraries be referenced correctly.

If you have more questions or feedback about the Command Line Tools, we’ll be happy to hear from you.

### 36 Responses to “ReSharper Code Analysis Goes Beyond Visual Studio”

1. Andy Says:

so here how i have used the tools. They do nothing for me what is wrong?

JetBrains Duplicates Finder for .NET
Running in 64-bit mode, .NET runtime 4.0.30319.18034 under Microsoft Windows NT 6.1.7601 Service Pack 1

JetBrains InspectCode for .NET
Running in 64-bit mode, .NET runtime 4.0.30319.18034 under Microsoft Windows NT 6.1.7601 Service Pack 1

2. Dmitry Matveev Says:

The problem is in the ‘#’ symbol in the path.

3. Robert Ellison Says:

Thanks for listening! This is what we need to really make effective use of ReSharper. Can’t wait to try it out.

4. Steven Evans Says:

This is pretty awesome. Really hope the licensing policy works nicely both for open source solutions where everything needs to live in source control since build servers likely are shared, and enterprise environments where having to install things on a build server is possible.

5. Andy Says:

@DmitryMatveev. Yes, thank you, it was a problem.
Another question to dupfinder. Why the following command line ignores exclude patterns:
dupfinder.exe /idle-priority /show-stats /show-text /discard-local-vars /discard-cost=70 /debug /normalize-types /exclude-code-regions=”Windows Form Designer generated code;Component Designer generated code” /o=”dupfinder.xml” “C:\Path\To\My Solution.sln” /e=”**/*.Designer.cs;**/*.generated.cs;**/Model.cs;”

I have also tried patterns just like *.Designer.cs, or */*.Designer.cs, but they appear anyway

6. ta.speot.is Says:

I know that to an extent TeamCity competes with Bamboo, but I’d love to see this integrated with Bamboo.

It appears dotCover has some experimental integration, care of the community: https://bitbucket.org/squirmy/bamboo-dotnet-plugin

If the support was there, out-of-the-box, it would likely go some ways to having ReSharper deployed more widely where I work.

7. Vinod Soni Says:

I think Resharper Command Line failed to write data at the shared location
I am using following command
inspectcode \\LOUCLRCS03DM02\WEB_PHARMACY\Builds\PCMS\TEST\Web\Src\PCMS.sln /o=”\\LOUCLRCS03DM02\WEB_PHARMACY\Builds\PCMS\TEST\Web\Code Analysis\Results.xml”

and getting following error
Failed to write data to the output file “\\LOUCLRCS03DM02\WEB_PHARMACY\Builds\PCMS\TEST\Web\Code Analysis\Results.xml”

Kindly help if somebody faced the same kind of problem, It’s working fine if I mention some location of my local machine like C:\Results.xml

8. Jura Gorohovsky Says:

@Vinod
Thanks for letting us know! Here’s a bug report based on your feedback.

9. Slava Trenogin Says:

10. Dmitry Matveev Says:

@Andy,
Sorry for answering you so late and thanks for your question anyway.
We didn’t announce dupFinder yet so its help is incomplete. Actually everything should work if you specify absolute paths or paths relative to the dupfinder.exe, not to the solution file. This is because dupfinder, unlike InspectCode, doesn’t base its analysis on the solution file. We’ll announce this tool and come up with all details in the nearest future.

11. Sarrasim Says:

Is there any news about licensing policy for this tool?

12. Jura Gorohovsky Says:

@Sarrasim Not yet, please hold on a little more.

13. Alexander Says:

Can you please update command line tools build for EAP? The last one is expired.

14. Jura Gorohovsky Says:

@Alexander, will do, thanks for noticing.

15. Dan Says:

Is there an InspectCode mode that would be suitable for use in a source control plugin. In other words, I would like to configure my source control system to evaluate all prospective source code submissions against my Resharper rules and reject submissions that do not conform. This would require something more granular than evaluation at the project or solution levels.

16. Nils Says:

17. Dmitry Matveev Says:

@Dan,
Your scenario looks very interesting, but currently inspectCode can only take Visual Studio solutions as input.
Anyway, I logged your request to our issue tracker so you can watch it there: http://youtrack.jetbrains.com/issue/RSRP-375162

18. Jura Gorohovsky Says:

@Nils
At this point, we’ll make the command-line tools for free. We’ll be looking at ways to provide additional tooling for them, and if we come up with ideas, there might be a paid edition. However, it’s too early to talk about that right now. In the scope if 8.0, they’re going to be free. We’re working on a license agreement reflecting this.

19. Jesse Houwing Says:

I love what this offers, I’d love to see a few things added:
- Allow me to run the inspectcode/dupfinder on a .csproj without having to specify the solution file or the project name. Just the file.
- Allow me to specify a list of .csproj files so that I can work without having a solution context.
- Give me an output shaping option that outputs MsBuild compatible warnings/errors, that makes it much easier to integrate into workflows that already know how to handle those.
- Can I somehow specify the Configuration/Platform to analyze?
- Are you guys planning to allow me to specify a logger/formatter type,assembly that I can pass to the commandline so that warnings and errors can be streamed to the host. Running the tool, then parsing the xml is so 1992 ;).

20. Dmitry Matveev Says:

@Jesse, thanks for the feedback. I logged everything in our issue tracker.
Note to others: if you have feature suggestions, please feel free to sign up and use the issue tracker - they won’t get lost there.

21. Rachel Says:

We have a fairly detailed project structure with a lot of shared .targets files. Each of these is imported by referring to \$(SolutionDir).

However, it seems that the tool(s) subsitute ProjectDir which means that almost none of solution is actually inspected.

Is this a “feature” ?

22. offler Says:

How can I use the dupfinder from within Resharper 8.0?

23. Jura Gorohovsky Says:

@offler
Duplicate Finder is not a part of ReSharper as an extension to Visual Studio, it’s only available as part of ReSharper Command Line Tools.

24. yasin Says:

Hi,

Above i see InspectCode C:\Projects\ReSharper\resharper.sln /project=Documents /o=”C:\temp data\Results.xml” /no-swea

I try this example with my solution and projects, but when there are more than one project i cant run this command. How can i do like this ?

C:\Projects\ReSharper\resharper.sln /project=Documents /project=XxxProject
/project=YyyProject /o=”C:\temp data\Results.xml” /no-swea

25. Dmitry Matveev Says:

Hi yasin,
Just use the semicolon (;) to separate multiple project wildcards. So:
C:\Projects\ReSharper\resharper.sln /project=Documents;XxxProject;YyyProject /o=”C:\temp data\Results.xml” /no-swea
To make sure that it works correctly, take the latest 8.1 EAP build

26. yasin Says:

Thanks Dmitry , it work.

but there is another problem. When i run thşs command with two project, there are two project under issues tag. One of them is correctly produce output but the other don’t produce output correctly. Output is below.

….
File=”..\Documents\Visual Studio 2012\Projects\Deneme2\PeakScreen1\App.cs” –>(..\) Here when i try to find this path i catch the FileNotFoundException normally.
What can i do to achieve this problem ?

27. Dmitry Matveev Says:

@yasin, If the tool misbihaves with the correct parameters, please log a bug in our issue tracker, choose ReSharper Automation Tools (Command Line) as a subsystem.

28. yasin Says:

@Dmitry ,
I created a bug and i have a question . I have two project and i want to produce xml document. How can it produce output.xml immediately? (like xml flush)

29. Dmitry Matveev Says:

@yasin, Sorry, I didn’t get you. You mean, two projects in different solutions or get it from R# user interface?

30. yasin Says:

@Dmitry , i have a solution and two projects in it. R# command line tool produce xml file but when process finish xml isnt published. I have code like;

using (Process process = new Process())
{
StringBuilder argumentLineBuilder = new StringBuilder();
foreach (string programArg in programArgs)
argumentLineBuilder.AppendFormat(”{0} “, programArg);

ProcessStartInfo processStartInfo = new ProcessStartInfo(@”D:\Yasin\Programlar\Resharper-commandline-8.0.0.39\inspectcode.exe”, argumentLineBuilder.ToString());
processStartInfo.CreateNoWindow = true;
processStartInfo.ErrorDialog = false;
processStartInfo.RedirectStandardError = true;
processStartInfo.UseShellExecute = false;

process.StartInfo = processStartInfo;
process.Start();
//process.WaitForExit();
}

How can the tool produce xml file immediately?

31. Bernhard Says:

Seems to work reasonable well. I have two questions,

1) How can I get the same behaviour as resharper if i have incorporated the StyleCop rules? Which setting file should I reference?

2) I use some legacy files, which very high number of code issues. How can I exclude certain files?

best regards
Bernhard

32. Dmitry Matveev Says:

Thanks for the feedback, Bernhard

1. If the StyleCop rules are set with ReSharper options, then you should get the same results as if you inspect code with ReSharper, provided that you do not move your solution from the local machine. If you want to run the analysis on a server, then the local settings will not apply. To apply all the necessary settings on the server, you need to save all of them in a single settings fiile and then use this file through the /profile (/p) parameter.
2. You can use the settings to exlude specific files, here is how to exclude files from code analyis. If you run InspectCode on a server, save the configuration of excluded files to the same settings file, together with your StyleCop rules.

33. yasin Says:

Hi,

When I run inspectcode.exe it produce like this xml content

But it generally produce well format. File attribute has a problem so it produce relative path. Is it a bug?

How can i achieve this problem?

Thanks.

34. yasin Says:

Xml content like :

Issue TypeId=”RedundantUsingDirective” File=”..\Documents\Visual Studio 2012\Projects\Deneme2\PeakScreen1\App.cs” Offset=”0-13″ Message=”Using directive is not required by the code and can be safely removed”

35. Dmitry Matveev Says:

Hi yasin,
Check out the Command Line Tools package in the latest R# 8.1 EAP. The file attribute now shows the path relative to the solution.

36. yasin Says:

Hi Dmitry,

In my opinion there are still some problem.When i run exe twice, one of output like :
Issue TypeId=”InconsistentNaming” File=”..\..\..\..\..\..\_Xxxx.Tfs.Ugalt\Code\Peak\Common\Peak.Common\ApplicationInformation.cs” Offset=”981-1001″ Line=”31″ Message=”Name ‘getSiteDirectoryName’ does not match rule ‘Methods, properties and events’. Suggested name is ‘GetSiteDirectoryName’.”
D:\_Xxxx.Tfs.Ug\Code\Peak\App\Service\Peak.App.Service.Mevduat\Peak.App.Service.Mevduat.sln

The other is :
Solution>..\..\..\..\..\..\Peak\Common\Peak.Common\Peak.Common.sln
ApplicationInformation.cs

File=”D:\Work\UGALT2\UGALT\Code\ALM\Code\Package\ALM.Code.Package.CodeAnalysis\bin\Debug\ApplicationInformation.cs”

Can i take absolute path from file attribute ?