Code Cleanup with the ReSharper Command Line Tools

Along with the latest releases of ReSharper, we have made Code Cleanup part of the ReSharper Command Line Tools. These tools allow us to run several ReSharper features from the command line or as part of continuous integration. Two tools have been there for a while — InspectCode (which can run hundreds of ReSharper code inspections outside of Visual Studio) and dupFinder (which helps finding duplicate code in C# and VB.NET code).

The new Code Cleanup command line will apply formatting code style preferences and other improvements such as file layout, removing redundancies, optimizing using, … in bulk mode. This instantly eliminates code style violations in a project or solution, and ensures a uniform code base.

Getting started with the Code Cleanup command line tool

First, we will have to download the ReSharper Command Line Tools that contain Code Cleanup. Note that these are free and do not require us to have the full ReSharper installed (except for creating custom code cleanup profiles).

After downloading and unzipping the command line tools, either on a local machine or a CI server, we can run CleanupCode.exe to automatically apply code formatting and code style preferences.

Since the tool can target a project or solution, let’s dive in at once and run it against the latest YouTrackSharp solution:

CleanupCode.exe YouTrackSharp.sln

The tool will scan all files in our solution and apply code formatting rules specified in the Full Cleanup profile:

Code cleanup command line - No parameters

Tip: after running CleanupCode.exe, a git diff will reveal any changes that were made by the tool.

Using a custom code cleanup profile

Out of the box, ReSharper ships with two default code cleanup profiles: Reformat Code (only applies code formatting rules) and Full Cleanup (which applies all available cleanup tasks (except adding a file header).

When no additional settings are specified or no custom cleanup profile is found in the solution shared settingsCleanupCode.exe will make use of ReSharper’s Full Cleanup profile.

To run a custom set of tasks, we’ll have to create a custom code cleanup profile from the ReSharper | Options menu, under Code Editing | Code Cleanup and specify the set of desired tasks there. We can start with a Duplicate of an existing profile, and then start customizing. To share this custom code cleanup profile with members of our team (and the CleanupCode.exe tool), we will have to save the settings to the team-shared settings. This will generate a <SolutionName>.sln.DotSettings file for us that can be added to source control.

Custom code cleanup profile in ReSharper

Once saved, we can run CleanupCode.exe --profile=YouTrackSharp YouTrackSharp.sln, telling it to use our custom code cleanup profile instead.

Use a custom code cleanup profile

Once completed, the Code Cleanup tool will have updated code in our solution based on our custom preferences!

There are a number of other command line options we can specify. Here are a few that might be of use:

  • --settings – Use a .DotSettings other than the solution-shared one.
  • --profile – Run a specific code cleanup profile.
  • --include/--exclude – Relative path(s) that define the files to include/exclude during clean up.
  • --disable-settings-layers – Don’t use other settings layers from ReSharper.
  • Several options related to MSBuild, cache folders, … Details are available from CleanupCode.exe --help.

It’s possible to generate a configuration file in which these options are specified. Using the CleanupCode.exe --config-create=config.xml, an empty configuration file is generated. After customizing it, we can use it with CleanupCode.exe --config=config.xml instead of having to pass these options on the command line.

Where to use Code Cleanup?

We’ve seen we can use the new Code Cleanup command line tool to apply formatting and other code style preferences to our code base. They are especially handy when working in mixed teams.

Imagine some developers are using ReSharper or Rider to enforce these settings, and others are not. Or an open source project where different contributors have different environments. The command line tools can be run as part of a continuous integration pipeline, or perhaps as a git precommit hook, to ensure every team member commits code that adheres to code style preferences.

We could use CleanupCode.exe on the build server to reformat code from a pull request before merging it into the master branch. Or perhaps run the tool and check if any changes were made – failing the build if there is a diff (and thus code style settings have not been followed).

Give the ReSharper Command Line Tools a try!

Comments below can no longer be edited.

22 Responses to Code Cleanup with the ReSharper Command Line Tools

  1. Avatar

    Robin says:

    March 1, 2018

    I absolutely love that you are enhancing the command line tools for Reshaper! But please please make them Mac OS or Linux compatible! We are literally running a Windows slave only for running Reshaper inspections.. Rider uses the Reshaper backend on Mac. Or add a command line interface to Rider while working on a standalone solution.

    Thank you for your great products!

    • Avatar

      Maarten Balliauw says:

      March 1, 2018

      Thanks for the great feedback!

    • Avatar

      Thomas says:

      April 26, 2018

      I can only second the request for Linux and Mac OS support. Linux being my personal focus 😀
      I hear from my colleagues thar has talked to jetbrains developers at a conference that you have an internal version that can actually do this. Care to elaborate 😀

      • Avatar

        Mr. User says:

        December 12, 2019

        I was able to get the cli working on linux and windows. I assume mac works as well. I was trying it out in automated git pipelines you can check out my work here.

  2. Avatar

    Urs Meili says:

    March 2, 2018

    Great – I’ve waited for that feature for years 🙂

  3. Avatar

    David De Sloovere says:

    March 23, 2018

    What other download options are there for these tools? Manual download is not the best option when integrating in an automated process.
    I’ve found packages on the Chocolatey repository, I hope these are official and get updated with every new release.

  4. Avatar

    Michael Chadwick says:

    April 25, 2018

    Does this tool work on C++ projects?

    • Avatar

      Maarten Balliauw says:

      April 25, 2018

      This only works on C# and Visual Basic .NET code for now.

      • Avatar

        Tim Finer says:

        May 30, 2018

        Why does the page for Resharper C++ say that it does then?

        Command-line code cleanup
        ReSharper Command Line Tools, a free standalone collection of tools that can be used from the command line or as a part of the continuous integration process, now lets you perform code cleanup in C++ projects. Run cleanupcode.x86.exe on your solution, and it will automatically reformat your code, remove common redundancies, apply Clang-Tidy fix-its and common code style fixes. This instantly eliminates code style violations in a project or a solution, and ensures a uniform code base.

        In related news, bogus errors which were output by the InspectCode tool on x64 projects were fixed in this release. In addition, TeamCity 2018.1 is able to run InspectCode on C++ projects (see TW-48978 for details).

        To learn more, see Code Cleanup with the ReSharper Command Line Tools.

        • Avatar

          Maarten Balliauw says:

          May 30, 2018

          Haven’t tried them yet. Do give it a go then!

  5. Avatar

    Igor says:

    May 10, 2018

    For some reason CodeCleanup does not add explicit type modifiers, does not apply var style and don’t use auto-property. I tried default Full Cleanup profile and custom profile. Am I doing something wrong or CodeCleanup tool not intended for this?

  6. Avatar

    Maxim Kropotov says:

    September 21, 2018

    Possibly it’s just me, but in case it might be useful to somebody else:
    to run the default ‘Reformat Code’ profile, the parameter value needs to be –profile=”Built-in: Reformat Code”, not just ‘Reformat Code’.

  7. Avatar

    TSS says:

    December 20, 2018

    Any one have done just formatting that whole clean up? When I format with custom profile it is still invoking msbuild. Is there a way to turn running of msbuild to off?

  8. Avatar

    Shusi says:

    April 30, 2019

    Awesome Tool!
    I’m usinng it with VS 2017. But I can’t figure out how to only format the currently active File in VS? Is there a way to do this? Since Formatting the complete Solution is not an option for me because of several Legacy Code Classes, which I’m not interested in touching ever again unless I have to 🙂

  9. Avatar

    John Zabroski says:

    July 29, 2019

    How do I configure Modifiers order refactoring? I am trying this right now while trying to compare a decompiled assembly to what’s checked into source control, and it’s a lot of work due to the fact the modifiers are not consistently sorted.

    I ran the default/full profile with cleanupcode.exe, but it doesn’t seem to be sorting consistently despite the fact I ran the same command on both the decompiled project and the source controlled project.

  10. Avatar

    Thomas Beimel says:

    October 11, 2019

    You say “Or perhaps run the tool and check if any changes were made – failing the build if there is a diff (and thus code style settings have not been followed).”

    How can I run the CleanupCode.exe only to check if there are not all files have the right code style?

  11. Avatar

    Gustav Wengel says:

    November 22, 2019

    How does including multiple files via –include work? I can make it work with one file, or one directory, but no matter how I try to specify multiple files, it just doesn’t do anything.

Discover more