⬆️ ⬇️

SolutionCop





Hi habr! The main discussion will be about developing on .Net, that is, using Microsoft Visual Studio, ReSharper, Nuget, etc.

I think many of you have developed large solutions ( in msdn - solution ), with many subprojects. And in this case, the problem of synchronization of Nuget packages, build settings, etc. often became common. Moreover, ReSharper will help here weakly, except that he, too, will become confused in the many libraries used.

To verify the source code, an Open Source solution was made - SolutionCop , which is free to use.

To begin with, I will give you a couple of examples when we would not interfere with checking our solutions.



Example 1: Different versions of Nuget libraries.



For example, there are three projects: exe, dll1 and dll2. exe refers to both libraries, each of them refers, for example, to RX. But dll1 uses RX 2.2.0 , and dll2 uses RX 2.2.5 . In fact, it’s not at all possible to get an error, since the function signatures are more or less the same, moreover, MsBuild most often collects projects in the same order. However, such a configuration can lead to problems that will appear after the deployment, when all the unit tests pass (because they refer only to their library), and when the resulting set of files will be prepared.

')

Example 2: the project refers to the library directly, and not through Nuget.



Again, take our three projects: exe, dll1 and dll2. Suppose we also use Jetbrains.Annotations to mark the NotNull / CanBeNull code with attributes and get pretty static analysis. But bad luck: for dll1 we honestly downloaded the package version 9.2.0 , and in dll2 we just asked ReSharper to add a link, which he did. As a result, there is no package with attributes in the packages.config file dll2, which means that if the project is built in the order dll2 -> dll1 -> exe, we will get an error, because the Nuget package will be downloaded only when collecting dll1!



There are some more examples where different settings in projects can lead to hilarious problems. For example, projects for .Net 4.0 and .Net 4.5 can refer to identical packages, but to different libraries in them ( see Nuget help ), which means we will again receive special effects when building projects. However, we better go to SolutionCop.



Installation



SolutionCop is available on Nuget , it is installed at the level of the entire solution. After installing the package, you need to create an xml file with the rules, configure them and embed the checker in the build procedure on the CI .

Step by Step:

  1. Create an xml file with the rules (we will check the SolutionCop itself):



    SolutionCop.exe -s "C:\git\SolutionCop\src\SolutionCop.sln"



    After this command, the SolutionCop.xml file will appear next to the sln file. It lists all the rules, but they are all turned off. Consider them later.
  2. We configure the CI server so that SolutionCop is launched on every build (a solution with 100 projects is checked for 3-4 seconds). For TeamCity, you can even publish status with a more convenient format. For everyone else, you’ll have to read Error Output. The application will return 0 if all the rules have been executed successfully. So, the command line for TeamCity:



    SolutionCop.exe -s "C:\git\SolutionCop\src\SolutionCop.sln" -b TeamCity --suppress-success-status-message.



    The last argument is necessary if TeamCity does not write the status of the unit tests (despite the fact that they were executed later). It will disable SolutionCop output in case all the rules have been executed.


rules



So SolutionCop is configured, it now looks into the solution, but does not check anything. And therefore I will list the rules that may be useful. A detailed description of their use is on GitHub , I just list the interesting things. For each rule, of course, you can set exceptions, etc.





In fact, there are a number of other rules in SolutionCop that will help clean up the code. I tried to list those that may be needed by almost everyone who develops on .Net.

Source: https://habr.com/ru/post/270873/



All Articles