Skip to content

Commit

Permalink
feat: add package for enabling IDE0005
Browse files Browse the repository at this point in the history
  • Loading branch information
shuebner committed Aug 6, 2023
1 parent 0894e06 commit 19d0061
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 2 deletions.
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,25 @@
# EnableUnnecessaryUsings
Package that applies a workaround to get IDE0005 (unnecessary usings) in the CLI build
# SvSoft.MSBuild.CheckUnnecessaryUsings

Enable the compiler check for [unnecessary usings (IDE0005)](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0005) in CLI builds just by including this package.

```xml
<PackageReference Include="SvSoft.MSBuild.CheckUnnecessaryUsings" Version="1.0.0" PrivateAssets="all" />
```

The package will enforce style checking in the build by setting `EnforceCodeStyleInBuild`.
(You may thus get additional non-IDE0005 style-related warnings, depending on your style settings.)
It will also set the diagnostic severity of IDE0005 to `warning` for the consuming project via a global analyzer config file.
The severity will be overridden by default by any `.globalconfig` file or any other global analyzer config file with a `global_level` greater than 10.

# Background

There is a [long standing issue in Roslyn](https://github.com/dotnet/roslyn/issues/41640) that prevents the CLI build from checking for unnecessary usings unless XML doc generation is turned on.
Yes, I know, crazy.
There is a [comparatively simple workaround](https://github.com/dotnet/roslyn/issues/41640#issuecomment-985780130).
But it has some disadvantages:

* causes an unwanted and unnecessary potentially empty XML doc file to be included in the output folder
* has to be manually disabled as soon as the project _does_ want real XML doc generation

In contrast, this solution does not leave any unnecessary files in the output folder.
It will also only trigger when `GenerateDocumentationFile` is `false` (default) in the consuming project. As soon as XML doc file generation is enabled by setting it to true, the workaround will no longer trigger and thus not have any unwanted side-effects.
4 changes: 4 additions & 0 deletions configs/CheckUnnecessaryUsings.globalconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
is_global = true
global_level = 10

dotnet_diagnostic.IDE0005.severity = warning
8 changes: 8 additions & 0 deletions configs/CheckUnnecessaryUsings.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project>
<PropertyGroup>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
<ItemGroup>
<GlobalAnalyzerConfigFiles Include="$(MSbuildThisFileDirectory)*.globalconfig" />
</ItemGroup>
</Project>
15 changes: 15 additions & 0 deletions configs/CheckUnnecessaryUsings.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project>
<Target Name="__TriggerUnnecessaryUsingsCheck" BeforeTargets="CoreCompile">
<!-- Condition to let consuming projects enable documentation file generation themselves -->
<ItemGroup Condition="'$(GenerateDocumentationFile)' != 'true'">
<!-- Trigger compiler check for unnecessary usings -->
<DocFileItem Include="$(IntermediateOutputPath)DummyDocForUnnecessaryUsings.xml" />
</ItemGroup>
<PropertyGroup Condition="'$(GenerateDocumentationFile)' != 'true'">
<!-- we do not want the dummy file in the output folder -->
<CopyDocumentationFileToOutputDirectory>false</CopyDocumentationFileToOutputDirectory>
<!-- Disable warnings about missing XML doc comments -->
<NoWarn>$(NoWarn);CS1591;CS1573</NoWarn>
</PropertyGroup>
</Target>
</Project>
44 changes: 44 additions & 0 deletions package/CheckUnnecessaryUsings.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<Project Sdk="Microsoft.NET.Sdk" DefaultTargets="Pack">
<PropertyGroup>
<PackageId>SvSoft.MSBuild.CheckUnnecessaryUsings</PackageId>
<Title>Enable IDE0005 Unnecessary Usings Check</Title>
<Description>Applies a workaround so that consuming projects can have IDE0005 (Unnecessary Usings) checked in their compile-time build. Projects can still choose to enable XML doc file generation without interference from this workaround.</Description>
<Authors>Sven Hübner</Authors>
<PackageTags>msbuild;ide0005;usings</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<!-- Make this a no-code package. Use some bogus TFM to make the SDK happy. -->
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<NoBuild>true</NoBuild>
<IncludeBuildOutput>false</IncludeBuildOutput>
<SuppressDependenciesWhenPacking>true</SuppressDependenciesWhenPacking>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="all" />
</ItemGroup>
<PropertyGroup>
<PublishRepositoryurl>true</PublishRepositoryurl>
</PropertyGroup>

<ItemGroup>
<None Include="../configs/*.globalconfig"
Pack="true"
PackagePath="build" />

<None Include="../configs/*.props"
Pack="true"
PackagePath="build/$(PackageId).props" />

<None Include="../configs/*.targets"
Pack="true"
PackagePath="build/$(PackageId).targets" />

<None Include="../README.md"
Pack="true"
PackagePath="/"/>
</ItemGroup>
</Project>
30 changes: 30 additions & 0 deletions samples/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project>
<ItemGroup>
<!-- use latest local temp package version -->
<PackageReference Include="SvSoft.MSBuild.CheckUnnecessaryUsings" Version="*-test.*" PrivateAssets="all" />
</ItemGroup>

<PropertyGroup>
<__UtcTicks>$([System.DateTimeOffset]::Now.UtcTicks)</__UtcTicks>
<!-- create some strictly increasing version number -->
<__TempPackageVersion>1.0.0-test.$(__UtcTicks)</__TempPackageVersion>
<__TempPackageOutputPath>$(MSBuildThisFileDirectory)../_out</__TempPackageOutputPath>
</PropertyGroup>

<PropertyGroup>
<!-- restore from the temp package build output folder -->
<RestoreAdditionalProjectSources>$(RestoreAdditionalProjectSources);$(__TempPackageOutputPath)</RestoreAdditionalProjectSources>
</PropertyGroup>

<PropertyGroup>
<__PackageProjectPath>$(MSBuildThisFileDirectory)../package/CheckUnnecessaryUsings.csproj</__PackageProjectPath>
</PropertyGroup>

<!-- Build a temp package on every restore. -->
<Target Name="__PackTempPackage" BeforeTargets="Restore">
<MSBuild Targets="restore" Projects="$(__PackageProjectPath)" />
<MSBuild Targets="pack"
Projects="$(__PackageProjectPath)"
Properties="OutputPath=$(__TempPackageOutputPath);Version=$(__TempPackageVersion)" />
</Target>
</Project>
5 changes: 5 additions & 0 deletions samples/nuget.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<configuration>
<packageSources>
<clear />
</packageSources>
</configuration>
4 changes: 4 additions & 0 deletions samples/xml_doc_disabled/Class.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// unnecessary using to trigger IDE0005
using System.IO;

class Class {}
5 changes: 5 additions & 0 deletions samples/xml_doc_disabled/Sample_with_doc_disabled.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
</Project>
4 changes: 4 additions & 0 deletions samples/xml_doc_enabled/Class.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// unnecessary using to trigger IDE0005
using System.IO;

class Class {}
9 changes: 9 additions & 0 deletions samples/xml_doc_enabled/Sample_with_doc_enabled.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
</Project>

0 comments on commit 19d0061

Please sign in to comment.