-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add package for enabling IDE0005
- Loading branch information
Showing
11 changed files
with
153 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<configuration> | ||
<packageSources> | ||
<clear /> | ||
</packageSources> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// unnecessary using to trigger IDE0005 | ||
using System.IO; | ||
|
||
class Class {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// unnecessary using to trigger IDE0005 | ||
using System.IO; | ||
|
||
class Class {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |