Duplicate Finder, Part of ReSharper Command Line Tools

Along with ReSharper 8 EAP earlier this year, we have made ReSharper Command Line Tools available for you to download and try. We have already written about one of the tools included in this package — InspectCode, which analyzes your code outside of Visual Studio using hundreds of ReSharper code inspections. But the package also includes another tool, dupFinder and we’ll take a closer look at it in this post.

As its name suggests, dupFinder finds duplicates in C# and Visual Basic .NET code. Being a JetBrains tool, dupFinder does it in a smart way. By default, it considers code fragments as duplicates not only if they are identical, but also if they are structurally similar, even if they contain different variables, fields, methods, types or literals. Of course, you can configure allowed similarity level as well as the minimum relative size of duplicated fragments.

Running Duplicate Analysis

dupFinder is not exactly a new kid on the block. For quite a while, JetBrains TeamCity has included it out of the box, and this is probably the easiest and the most efficient way to make use of dupFinder. However, from now on you can get it running with your custom CI, version control, quality control or any other server and here is how:

One way to define the target sources is to specify a solution file: dupFinder understands solution files of Visual Studio 2003, 2005, 2008, 2010, and 2012. Alternatively, you can provide a specific list of source files as a set of newline-delimited wildcards.

Configuring Options

Using optional parameters, you can configure how dupFinder should analyze your source code. To explore the full list of options, run dupFinder /help. Below are some of the options that you might be interested in:

/exclude allows excluding files from duplicate code search. The value is a set of newline-delimited wildcards (for example, **Generated*.cs). Note that the paths should be either absolute or relative to the working directory.

/exclude-by-comment and /exclude-code-regions allow excluding files by substrings of opening comments and regions. The value is a set of newline-delimited keywords (e.g. ‘generated code’ will exclude regions containing ‘Windows Form Designer generated code’).

/discard-fields, /discard-literals, /discard-local-vars, /discard-types specify whether to filter out similar fragments as non-duplicates if they have different variables, fields, methods, types or literals. The default value for all of them is ‘false’. To illustrate the way it works, consider the following example. There are two code fragments otherwise identical, one contains myStatusBar.SetText("Logging In...");, the other contains myStatusBar.SetText("Not Logged In");. If ‘discard-literals’ is set to ‘false’, these fragments are considered duplicates.

/discard-cost allows setting a threshold for code complexity of duplicated fragments. The fragments with lower complexity are discarded as non-duplicates. The value for this option is provided in relative units.
Using this option, you can filter out equal code fragments that present no semantic duplication. E.g. you can often have the following statements in tests: Assert.AreEqual(gold, result);. If the ‘discard-cost’ value is less than 10, statements like that will appear as duplicates, which is obviously unhelpful. You’ll need to play a bit with this value to find a balance between avoiding false positives and missing real duplicates. The proper values will differ for different codebases.

/show-text: if this parameter is used, detected duplicate fragments will be embedded into the report.

Understanding Output

The resulting output is a single XML file that presents the following information:

The Statistics node is an overview of analyzed code, where CodeBaseCost is the relative size of target source code, TotalFragmentsCost is the relative size of the code for analysis after applying filters (‘discard-cost’, ‘discard-literals’, etc.), and TotalDuplicatesCost is the relative size of detected duplicates.

The Duplicates node contains Duplicate nodes, which in turn contain two or more Fragment elements.

Each Duplicate node has a Cost attribute: duplicates with greater cost are the most important ones as they potentially present greater problems.

Each Fragment element contains file name as well as duplicated piece presented in two alternative ways: as a file offset range and as a line range. If the /show-text option was enabled for analysis, then a Text node with the duplicated code is added to each fragment.

Practical Use

We are now ready to have some practice with dupFinder. In the steps described below we’ll take a solution, e.g. SolutionWithDuplicates.sln and see how to start duplicate analysis using an MSBuild target with a simple HTML report based on the dupFinder output.

Step 1

Step 2

Now let’s think ahead to processing the dupFinder output. If we leverage the /show-text option, we’ll be able to build an HTML report by applying an XSL transformation to the dupFinder XML output; something like this will do:

We put this XSL stylesheet with the rest of the tools into C:programsCLT.

Step 3

The easiest way to run duplicate analysis and the ensuing transformation is specify a new MSBuild target. Since we are now in the solution directory, we go into one of its project subdirectories and open the project file (*.csproj) with a text editor, then add the following element into the root <Project> node:

In this build target, which executes after the project build is finished, we move the working directory one folder up from the project directory to the solution directory, run dupFinder, and then apply an XSL transformation to the dupFinder outpiut using our XSL stylesheet.

Step 4

Finally, all we have to do is to build our solution. If everything goes right, we’ll get two new files in the solution directory: dupReport.xml and dupReport.html. If we open dupReport.html, we can look through the list of all detected duplicates right in the web browser:

This simple example can be extended and customized in many ways, but we hope it shows you that there is nothing difficult in integrating ReSharper Command Line Tools into your workflow.

I’ve also tried **/**.Test*.cs, ****.Test*.cs and a few other globs; none of them have worked.

Sort of related to this, the command line help doesn’t go any further in specifying the delimiter. I’d expect commas or semicolons; the page here says it expects newline-delimited tokens which only makes sense if /exclude is looking for a file of exclude patterns. If it is, the neither the command line help nor this page makes that clear.

Hi Dave,
Sorry if didn’t make it clear but all optional parameters that receive values have the following format: /option=value
So your example should look like:
dupfinder /exclude=**Test*.cs YourSolution.sln

I tried every syntax combination, including the one specified by Dmitry, and including using an external file with the patterns one-per-line, and in every case, I get the EXACT same output. Which includes all the *Test.cs files. Nothing I do seems to filter them out.

I’m thinking this functionality is just plain broken… or at least ONE of the many permutations I did, based on very vague and unclear documentation, would have worked.

Hi Paul,
I mentioned above that the exclude paths should be either absolute or relative to the working directory.
The best way is to run dupfinder from the solution directory – open the command line in the solution direcotry and then run dupfinder using the full path to its executable file. E.g.:
C:programsCLToolsdupfinder.exe /exclude=**Tests.cs YourSolution.sln

This is what I would really prefer… I want to run it on a bunch of solutions, not having to have to cd to every single solution in between each… that’s very awkward. Especially given that the tool is on a different drive than the solution, is on a different drive than the output in this situation.

Given that it works just fine like this *except* for the fact that the /exclude is ignored, can I put in a request to fix this (in my opinion) bug (or think of it as an enhancement if you must), so that I don’t have to run it from the solution directory to get it to work?

I have verified that what you’ve suggested seems to work, but I cannot stress enough how awkward and unintuitive that is for me. I really want to just sit in one place and run it on all the solutions… that’s so much easier and more intuitive.

It would also be nice if you could give the name of the xls file on the command line, so the output could have that line generated right in it, so I wouldn’t have to open the file in Notepad++, paste in the xml-stylesheet line, save, and then go look at it in the browser. Again, just going for ease of use here… trying to automate things is difficult as it is right now.

I want to exclude multiple files. According to the documentation,
/exclude allows excluding files from duplicate code search. The value is a set of newline-delimited wildcards (for example, **Generated*.cs).
I was able to exclude files that ends with the word “Map” with the following command
/exclude=**Map.cs
I also need to specify another wildcard. I tried few combinations without any luck.
/exclude=**Map.csnr**NinjectWebCommon.cs
Any help will be appreciated.

Yes, it supports VS2013 solution files.
But be informed that dupFinder uses solution files for the only purpose – to get the list of included sorce files, so you can always specify just a list of source files as an input.

Hi All, Sorry for being away for some time.
@Nuwan, in your case, it looks like we have an issue with delimiters. You can watch the issue and comment on it.
@Nick, I think this link will help you.
@Grady, good idea, thanks.

Is there any support for importing the Inspections output files into TeamCity similar to the duplicates finder? I keep details of the build stages in psake and instead of codifying every step in detail in TeamCity itself. I see there is one for the duplicates finder “ResharperDupFinder” already.

I have tried numerous settings and am unable to “exclude” any files. I’ve spent over an hour trying different mutations and read numerous sites/articles, all with no luck..

Any help would be appreciated
My Solution file is here …
“C:1 DevelopmentDashboard SystemSource Code5 Solution FilesE-Tabs.Dashboard.System”
The source code projects are located in numerous folders from this root …
“C:1 DevelopmentDashboard SystemSource Code”

I created a batch file (just want to use the command line) … to exclude ALL
*generated.cs and *Designer.cs in any subfolder in any project.

If you are running your script in the solution directory, then use
/exclude=”**generated.cs; **Designer.cs”
otherwise use
/exclude=”C:**generated.cs; C:**Designer.cs;”
C is the drive letter.
Please answer whether it helps.

I’m very new to this so the instructions are really unclear to me.
My first issue: You lost me at step 3. How do I run dupFinder? Can it be done through an executing program? ‘specifying a new MSBuild target’ What does that mean? What options do I have to run it without dealing with solution directories? I am dealing with .cs files in a directory.

The other thing is, how do I pass a list of source files to it? I want to compare a bunch of source files to a known source file and then include options to single out the most similar source file. The output would then be a diff of the known source file and the most similar file, and then I would transform it to html. This is all I’m trying to do. If dupFinder is not capable of finding the most similar source file, I would be content with a diff of all source files.