Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCC-3: Fix linux-specific problems in Lombiq.Analyzers. #53

Merged
merged 15 commits into from
Apr 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ trim_trailing_whitespace = true
# New line preferences
end_of_line = crlf
insert_final_newline = true
dotnet_style_operator_placement_when_wrapping = beginning_of_line
tab_width = 4
dotnet_style_coalesce_expression = true:warning
dotnet_style_null_propagation = true:warning
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:warning
dotnet_style_prefer_auto_properties = true:warning

# C# files
[*.cs]
Expand Down Expand Up @@ -159,7 +165,7 @@ csharp_style_unused_value_assignment_preference = discard_variable:suggestion
csharp_style_unused_value_expression_statement_preference = discard_variable:none

# 'using' directive preferences
csharp_using_directive_placement = outside_namespace
csharp_using_directive_placement = outside_namespace:silent

#### C# Formatting Rules ####

Expand Down Expand Up @@ -259,6 +265,7 @@ dotnet_diagnostic.CA1810.severity = none

# Don't apply "CA1822: Mark members as static" to the public API
dotnet_code_quality.ca1822.api_surface = private, internal
csharp_prefer_braces = true:silent

# Various config files
[*.{config,csproj,json,props,targets}]
Expand Down
19 changes: 18 additions & 1 deletion Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)OrchardCore.ruleset</CodeAnalysisRuleSet>
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)orchardcore.ruleset</CodeAnalysisRuleSet>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<!-- Don't run analyzers during Visual Studio build, not to slow down development. They still run during dotnet build. -->
<RunAnalyzersDuringBuild>false</RunAnalyzersDuringBuild>
Expand All @@ -26,6 +26,23 @@

<Import Project="$(MSBuildThisFileDirectory)CommonPackages.props" />

<ItemGroup>
<!-- Fixing the <"error CS8032: An instance of analyzer Microsoft.CodeAnalysis.[...]Analyzer cannot be created from
[...]\.nuget\packages\microsoft.codeanalysis.csharp.codestyle\4.1.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.CSharp.CodeStyle.dll:
Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.1.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.."> by
adding "Microsoft.CodeAnalysis" AND "Microsoft.Net.Compilers" manually; see:
https://docs.microsoft.com/en-us/answers/questions/244179/microsoftcodeanalysis-problem.html -->
<PackageReference Include="Microsoft.CodeAnalysis.Compilers" Version="4.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers;</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Net.Compilers.Toolset" Version="4.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers;</IncludeAssets>
</PackageReference>
</ItemGroup>

<!-- Packages only for .NET Core and later. -->
<ItemGroup>
<!-- For future use. -->
Expand Down
8 changes: 4 additions & 4 deletions Docs/AddingAnalyzers.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ This will use the analyzer configuration suitable for Orchard Core projects. If

```xml
<PropertyGroup>
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)tools/Lombiq.Analyzers/General.ruleset</CodeAnalysisRuleSet>
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)tools/Lombiq.Analyzers/general.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
```

Expand All @@ -49,13 +49,13 @@ For at least Visual Studio and JetBrains Rider you don't need any further setup

The recommended approach for SDK-style projects is adding .NET Analyzers as a submodule as explained above due to the increased control you have over configuration. However, if you aren't using Git or prefer NuGet, you can also use the [NuGet package](https://www.nuget.org/packages/Lombiq.Analyzers/) to install it for just one project. Once you add the package to your project, all analyzers will be applied.

You can also add the package to all projects in a folder at once from a *Directory.Build.props* file (much like we do in [*CommonPackages.props*](../CommonPackages.props).
You can also add the package to all projects in a folder at once from a *Directory.Build.props* file (much like we do in [*CommonPackages.props*](../CommonPackages.props)).


## How to add the analyzers to individual non-SDK-style .NET Framework projects (not solutions)

1. Same as above - add the .NET-Analyzers repository as a submodule to your repository.
2. Create a *Directory.Build.props* file in every project folder you want to target, next to the *.csproj* file with the following content (import the *`NetFx.Build.props`* file instead of *Build.props*, adjust the relative path as suitable):
2. Create a *Directory.Build.props* file in every project folder you want to target, next to the *.csproj* file with the following content (import the *NetFx.Build.props* file instead of *Build.props*, adjust the relative path as suitable):
```xml
<Project>
<Import Project="../../../tools/Lombiq.Analyzers/NetFx.Build.props" />
Expand All @@ -66,7 +66,7 @@ You can also add the package to all projects in a folder at once from a *Directo
.editorconfig
```

This will use the analyzer configuration suitable for Orchard 1 projects. If you want to use this in a non-Orchard .NET Framework app then use the *General.ruleset* file as described above.
This will use the analyzer configuration suitable for Orchard 1 projects. If you want to use this in a non-Orchard .NET Framework app then use the *general.ruleset* file as described above.


## Introducing analyzers to an existing project
Expand Down
10 changes: 5 additions & 5 deletions Docs/ConfiguringAnalyzers.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ If not all the configuration in this project is suitable for your solution then

### Overriding analyzer configuration from a ruleset file

1. Create your own ruleset file, similar to this project's *General.ruleset* file, and add any rule configurations there that you want to override. Also, include this project's *General.ruleset* file as a child, allowing its rules to be available by default. So you'll have something like this (rules included only as examples):
1. Create your own ruleset file, similar to this project's *general.ruleset* file. Make sure the file name is all lower-case, because if you edit it in Visual Studio it will be converted anyway and that could cause problems on Unix-like systems. Add any rule configurations there that you want to override. Also, include this project's *general.ruleset* file as a child, allowing its rules to be available by default. In the end you should have something like this (the included rules only serve as an example):
```xml
<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="General C# rules" ToolsVersion="16.0">
<Include Path="tools\Lombiq.Analyzers\General.ruleset" Action="Default" />
<Include Path="tools\Lombiq.Analyzers\general.ruleset" Action="Default" />
<Rules AnalyzerId="SecurityCodeScan" RuleNamespace="SecurityCodeScan">
<Rule Id="SCS0005" Action="None" />
</Rules>
Expand All @@ -54,13 +54,13 @@ If not all the configuration in this project is suitable for your solution then
</Rules>
</RuleSet>
```
2. In the `Directory.Build.props` file of your solution add a reference to your own ruleset file, overriding the default:
2. In the *Directory.Build.props* file of your solution add a reference to your own ruleset file, overriding the default:
```xml
<PropertyGroup>
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)My.ruleset</CodeAnalysisRuleSet>
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)my.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
```
3. Now every rule you defined in *My.ruleset* will take precedence over the default ones. For everything else the default ones will be applied.
3. Now every rule you've defined in *my.ruleset* will take precedence over the default ones. For everything else, the default ones will be applied.

Note that if you add your ruleset file to the solution you'll get GUI support for it in Visual Studio and you'll be able to configure rules without manually editing the XML.

Expand Down
4 changes: 2 additions & 2 deletions NetFx.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)Orchard1.ruleset</CodeAnalysisRuleSet>
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)orchard1.ruleset</CodeAnalysisRuleSet>
<!-- Simply <GenerateDocumentationFile>true</GenerateDocumentationFile> can't be used because that's for SDK
projects only, see: https://stackoverflow.com/a/47118584/220230. -->
<DocumentationDir>$(OutputPath)</DocumentationDir>
Expand Down Expand Up @@ -32,7 +32,7 @@
Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.1.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.."> by
adding "Microsoft.CodeAnalysis" AND "Microsoft.Net.Compilers" manually; see:
https://docs.microsoft.com/en-us/answers/questions/244179/microsoftcodeanalysis-problem.html-->
https://docs.microsoft.com/en-us/answers/questions/244179/microsoftcodeanalysis-problem.html -->
<PackageReference Include="Microsoft.CodeAnalysis.Compilers" Version="4.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers;</IncludeAssets>
Expand Down
2 changes: 1 addition & 1 deletion VisualStudioExtension.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<Import Project="$(MSBuildThisFileDirectory)NetFx.Build.props" />
<PropertyGroup>
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)VisualStudioExtension.ruleset</CodeAnalysisRuleSet>
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)visualstudioextension.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
</Project>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.