0

I am looking for a way to detect from the command line unused ProjectReference references in the csproj file (not using references).

Something like that but I want to add the process in an automatic CI pipeline, without opening VS, resharper or any other IDE tool.

I see there exist this command to remove references, but first I need to detect which references are not used.

There exist already a question about that but it's 13 years old and the answers are obsolete.

I wonder if the VS tool posted above has any dotnet equivalent tool.

xavier
  • 1,860
  • 4
  • 18
  • 46
  • 3
    Making changes to code files during a CI pipeline run might not be a good idea as it means you have to commit the changes, push another PR or similar and somehow automate this without causing issues with branch policies. If you are using VS22 17.2.2 or later you can enable code cleanup on save. – Nope Mar 14 '23 at 10:59
  • Sure, I actually want to detect if there is an unused reference and, in that case, the build would fail and a PR with the change should be created. I could skip that second part, the important part is the first one, the detection one – xavier Mar 14 '23 at 11:01
  • I edited the question to remove the removal part. That's not the important part of the question – xavier Mar 14 '23 at 11:03
  • We use a mix of things. As we use VS22 we can enable code cleanup on save `Tools > Options > Text Editor > Code Cleanup > Run Code Cleanup profile on Save` and the profiles are configurable. We also use PR reviews to manually check and we use SonarCloud in the Build Validation on the Branch Policies. SonarCloud can be configured for different rules as well. Other than that, if some slip through the odd time, the next time someone works on the class we clean it up as we notice it. – Nope Mar 14 '23 at 11:09
  • I think the `Run Code Cleanup` works only for `using`s, not for project references in the `csproj` file. Still, in a project with a lot of projects and several people modifying them, we can't rely on everyone paying attention on these details. Some people modify code from different IDEs and not all teams are eager to check SonarCloud if they are not forced to (for example with a failing build). Moreover, it should be run the tool to all the existing projects… – xavier Mar 14 '23 at 11:29

2 Answers2

1

Starting in .NET5 you can have code style analysis on build.

The rule you are looking for is Remove unnecessary using directives (IDE0005). Use a .editorconfig to make sure the rule is set to error level instead of warning/info.

SynerCoder
  • 12,493
  • 4
  • 47
  • 78
  • No, that's not what I need. As you can see in all 3 links I posted, what I want is to delete ProjectReferences from the csproj file. I'll update my question to make it clearer – xavier Mar 14 '23 at 11:21
  • Ah, sorry, my bad. Misunderstood it at first. – SynerCoder Mar 14 '23 at 11:24
0

You can add NuGet package called ReferenceTrimmer. By adding that package an analyzer will be added to your project that will produce warnings during build for all unused dependencies (be it NuGet or project).

You could use WarningsAsErrors compiler option to make sure the generated warning will be elevated to an error.

SynerCoder
  • 12,493
  • 4
  • 47
  • 78