Skip to content

Commit

Permalink
Merge pull request #1784 from OmniSharp/feature/warnings-not-as-errors
Browse files Browse the repository at this point in the history
added support for WarningsNotAsErrors msbuild property
  • Loading branch information
bjorkstromm authored May 8, 2020
2 parents 098b634 + 59e11b9 commit 77c928e
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ All changes to the project will be documented in this file.

## [1.35.2] - not yet released
* Added support for `WarningsAsErrors` in csproj files (PR: [#1779](https://github.com/OmniSharp/omnisharp-roslyn/pull/1779))
* Added support for `WarningsNotAsErrors` in csproj files ([#1681](https://github.com/OmniSharp/omnisharp-roslyn/issues/1681), PR: [#1784](https://github.com/OmniSharp/omnisharp-roslyn/pull/1784))

## [1.35.1] - 2020-05-04
* Fixed not supported exception when trying to decompile a BCL assembly on Mono. For now we do not try to resolve implementation assembly from a ref assembly (PR: [#1767](https://github.com/OmniSharp/omnisharp-roslyn/pull/1767))
Expand Down
13 changes: 10 additions & 3 deletions src/OmniSharp.MSBuild/ProjectFile/ProjectFileInfo.ProjectData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private class ProjectData
public ImmutableArray<string> AdditionalFiles { get; }
public ImmutableArray<string> AnalyzerConfigFiles { get; }
public ImmutableArray<string> WarningsAsErrors { get; }
public ImmutableArray<string> WarningsNotAsErrors { get; }
public RuleSet RuleSet { get; }
public ImmutableDictionary<string, string> ReferenceAliases { get; }
public ImmutableDictionary<string, string> ProjectReferenceAliases { get; }
Expand All @@ -76,6 +77,7 @@ private ProjectData()
ReferenceAliases = ImmutableDictionary<string, string>.Empty;
ProjectReferenceAliases = ImmutableDictionary<string, string>.Empty;
WarningsAsErrors = ImmutableArray<string>.Empty;
WarningsNotAsErrors = ImmutableArray<string>.Empty;
}

private ProjectData(
Expand All @@ -94,6 +96,7 @@ private ProjectData(
ImmutableArray<string> preprocessorSymbolNames,
ImmutableArray<string> suppressedDiagnosticIds,
ImmutableArray<string> warningsAsErrors,
ImmutableArray<string> warningsNotAsErrors,
bool signAssembly,
string assemblyOriginatorKeyFile,
bool treatWarningsAsErrors,
Expand Down Expand Up @@ -126,6 +129,7 @@ private ProjectData(
PreprocessorSymbolNames = preprocessorSymbolNames.EmptyIfDefault();
SuppressedDiagnosticIds = suppressedDiagnosticIds.EmptyIfDefault();
WarningsAsErrors = warningsAsErrors.EmptyIfDefault();
WarningsNotAsErrors = warningsNotAsErrors.EmptyIfDefault();

SignAssembly = signAssembly;
AssemblyOriginatorKeyFile = assemblyOriginatorKeyFile;
Expand Down Expand Up @@ -153,6 +157,7 @@ private ProjectData(
ImmutableArray<string> preprocessorSymbolNames,
ImmutableArray<string> suppressedDiagnosticIds,
ImmutableArray<string> warningsAsErrors,
ImmutableArray<string> warningsNotAsErrors,
bool signAssembly,
string assemblyOriginatorKeyFile,
ImmutableArray<string> sourceFiles,
Expand All @@ -171,7 +176,7 @@ private ProjectData(
ImmutableDictionary<string, string> projectReferenceAliases)
: this(guid, name, assemblyName, targetPath, outputPath, intermediateOutputPath, projectAssetsFile,
configuration, platform, targetFramework, targetFrameworks, outputKind, languageVersion, nullableContextOptions, allowUnsafeCode, checkForOverflowUnderflow,
documentationFile, preprocessorSymbolNames, suppressedDiagnosticIds, warningsAsErrors, signAssembly, assemblyOriginatorKeyFile, treatWarningsAsErrors, defaultNamespace, runAnalyzers, runAnalyzersDuringLiveAnalysis, ruleset)
documentationFile, preprocessorSymbolNames, suppressedDiagnosticIds, warningsAsErrors, warningsNotAsErrors, signAssembly, assemblyOriginatorKeyFile, treatWarningsAsErrors, defaultNamespace, runAnalyzers, runAnalyzersDuringLiveAnalysis, ruleset)
{
SourceFiles = sourceFiles.EmptyIfDefault();
ProjectReferences = projectReferences.EmptyIfDefault();
Expand Down Expand Up @@ -216,6 +221,7 @@ public static ProjectData Create(MSB.Evaluation.Project project)
var preprocessorSymbolNames = PropertyConverter.ToPreprocessorSymbolNames(project.GetPropertyValue(PropertyNames.DefineConstants));
var suppressedDiagnosticIds = PropertyConverter.ToSuppressedDiagnosticIds(project.GetPropertyValue(PropertyNames.NoWarn));
var warningsAsErrors = PropertyConverter.SplitList(project.GetPropertyValue(PropertyNames.WarningsAsErrors), ',');
var warningsNotAsErrors = PropertyConverter.SplitList(project.GetPropertyValue(PropertyNames.WarningsNotAsErrors), ',');
var signAssembly = PropertyConverter.ToBoolean(project.GetPropertyValue(PropertyNames.SignAssembly), defaultValue: false);
var assemblyOriginatorKeyFile = project.GetPropertyValue(PropertyNames.AssemblyOriginatorKeyFile);
var treatWarningsAsErrors = PropertyConverter.ToBoolean(project.GetPropertyValue(PropertyNames.TreatWarningsAsErrors), defaultValue: false);
Expand All @@ -225,7 +231,7 @@ public static ProjectData Create(MSB.Evaluation.Project project)
return new ProjectData(
guid, name, assemblyName, targetPath, outputPath, intermediateOutputPath, projectAssetsFile,
configuration, platform, targetFramework, targetFrameworks, outputKind, languageVersion, nullableContextOptions, allowUnsafeCode, checkForOverflowUnderflow,
documentationFile, preprocessorSymbolNames, suppressedDiagnosticIds, warningsAsErrors, signAssembly, assemblyOriginatorKeyFile, treatWarningsAsErrors, defaultNamespace, runAnalyzers, runAnalyzersDuringLiveAnalysis, ruleset: null);
documentationFile, preprocessorSymbolNames, suppressedDiagnosticIds, warningsAsErrors, warningsNotAsErrors, signAssembly, assemblyOriginatorKeyFile, treatWarningsAsErrors, defaultNamespace, runAnalyzers, runAnalyzersDuringLiveAnalysis, ruleset: null);
}

public static ProjectData Create(MSB.Execution.ProjectInstance projectInstance)
Expand Down Expand Up @@ -260,6 +266,7 @@ public static ProjectData Create(MSB.Execution.ProjectInstance projectInstance)
var preprocessorSymbolNames = PropertyConverter.ToPreprocessorSymbolNames(projectInstance.GetPropertyValue(PropertyNames.DefineConstants));
var suppressedDiagnosticIds = PropertyConverter.ToSuppressedDiagnosticIds(projectInstance.GetPropertyValue(PropertyNames.NoWarn));
var warningsAsErrors = PropertyConverter.SplitList(projectInstance.GetPropertyValue(PropertyNames.WarningsAsErrors), ',');
var warningsNotAsErrors = PropertyConverter.SplitList(projectInstance.GetPropertyValue(PropertyNames.WarningsNotAsErrors), ',');
var signAssembly = PropertyConverter.ToBoolean(projectInstance.GetPropertyValue(PropertyNames.SignAssembly), defaultValue: false);
var treatWarningsAsErrors = PropertyConverter.ToBoolean(projectInstance.GetPropertyValue(PropertyNames.TreatWarningsAsErrors), defaultValue: false);
var runAnalyzers = PropertyConverter.ToBoolean(projectInstance.GetPropertyValue(PropertyNames.RunAnalyzers), defaultValue: true);
Expand Down Expand Up @@ -333,7 +340,7 @@ public static ProjectData Create(MSB.Execution.ProjectInstance projectInstance)
return new ProjectData(guid, name,
assemblyName, targetPath, outputPath, intermediateOutputPath, projectAssetsFile,
configuration, platform, targetFramework, targetFrameworks,
outputKind, languageVersion, nullableContextOptions, allowUnsafeCode, checkForOverflowUnderflow, documentationFile, preprocessorSymbolNames, suppressedDiagnosticIds, warningsAsErrors,
outputKind, languageVersion, nullableContextOptions, allowUnsafeCode, checkForOverflowUnderflow, documentationFile, preprocessorSymbolNames, suppressedDiagnosticIds, warningsAsErrors, warningsNotAsErrors,
signAssembly, assemblyOriginatorKeyFile,
sourceFiles, projectReferences.ToImmutable(), references.ToImmutable(), packageReferences, analyzers, additionalFiles, editorConfigFiles, treatWarningsAsErrors, defaultNamespace, runAnalyzers, runAnalyzersDuringLiveAnalysis, ruleset,
referenceAliases.ToImmutableDictionary(), projectReferenceAliases.ToImmutable());
Expand Down
1 change: 1 addition & 0 deletions src/OmniSharp.MSBuild/ProjectFile/ProjectFileInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ internal partial class ProjectFileInfo
public ImmutableArray<string> PreprocessorSymbolNames => _data.PreprocessorSymbolNames;
public ImmutableArray<string> SuppressedDiagnosticIds => _data.SuppressedDiagnosticIds;
public ImmutableArray<string> WarningsAsErrors => _data.WarningsAsErrors;
public ImmutableArray<string> WarningsNotAsErrors => _data.WarningsNotAsErrors;

public bool SignAssembly => _data.SignAssembly;
public string AssemblyOriginatorKeyFile => _data.AssemblyOriginatorKeyFile;
Expand Down
11 changes: 11 additions & 0 deletions src/OmniSharp.MSBuild/ProjectFile/ProjectFileInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ public static ImmutableDictionary<string, ReportDiagnostic> GetDiagnosticOptions
var suppressions = CompilationOptionsHelper.GetDefaultSuppressedDiagnosticOptions(projectFileInfo.SuppressedDiagnosticIds);
var specificRules = projectFileInfo.RuleSet?.SpecificDiagnosticOptions ?? ImmutableDictionary<string, ReportDiagnostic>.Empty;

// suppressions capture NoWarn and they have the highest priority
var combinedRules = specificRules.Concat(suppressions.Where(x => !specificRules.Keys.Contains(x.Key))).ToDictionary(x => x.Key, x => x.Value);

// then handle WarningsAsErrors
foreach (var warningAsError in projectFileInfo.WarningsAsErrors)
{
if (!suppressions.ContainsKey(warningAsError))
Expand All @@ -65,6 +67,15 @@ public static ImmutableDictionary<string, ReportDiagnostic> GetDiagnosticOptions
}
}

// WarningsNotAsErrors can overwrite WarningsAsErrors
foreach (var warningNotAsError in projectFileInfo.WarningsNotAsErrors)
{
if (!suppressions.ContainsKey(warningNotAsError))
{
combinedRules[warningNotAsError] = ReportDiagnostic.Warn;
}
}

return combinedRules.ToImmutableDictionary();
}

Expand Down
1 change: 1 addition & 0 deletions src/OmniSharp.MSBuild/ProjectFile/PropertyNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ internal static class PropertyNames
public const string VisualStudioVersion = nameof(VisualStudioVersion);
public const string VsInstallRoot = nameof(VsInstallRoot);
public const string WarningsAsErrors = nameof(WarningsAsErrors);
public const string WarningsNotAsErrors = nameof(WarningsNotAsErrors);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<WarningsAsErrors>CS1998,CS7080,CS7081</WarningsAsErrors>
<NoWarn>CS7081</NoWarn>
<WarningsAsErrors>CS1998,CS7080,CS7081</WarningsAsErrors>
<WarningsNotAsErrors>CS7080,CS7082</WarningsNotAsErrors>
</PropertyGroup>

</Project>
12 changes: 10 additions & 2 deletions tests/OmniSharp.MSBuild.Tests/ProjectFileInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,21 @@ public async Task WarningsAsErrors()
Assert.Contains("CS7080", projectFileInfo.WarningsAsErrors);
Assert.Contains("CS7081", projectFileInfo.WarningsAsErrors);

Assert.NotEmpty(projectFileInfo.WarningsNotAsErrors);
Assert.Contains("CS7080", projectFileInfo.WarningsNotAsErrors);
Assert.Contains("CS7082", projectFileInfo.WarningsNotAsErrors);

var compilationOptions = projectFileInfo.CreateCompilationOptions();
Assert.True(compilationOptions.SpecificDiagnosticOptions.ContainsKey("CS1998"), "Specific diagnostic option for CS1998 not found");
Assert.True(compilationOptions.SpecificDiagnosticOptions.ContainsKey("CS7080"), "Specific diagnostic option for CS7080 not found");
Assert.True(compilationOptions.SpecificDiagnosticOptions.ContainsKey("CS7081"), "Specific diagnostic option for CS7081 not found");
Assert.True(compilationOptions.SpecificDiagnosticOptions.ContainsKey("CS7082"), "Specific diagnostic option for CS7082 not found");
Assert.Equal(ReportDiagnostic.Error, compilationOptions.SpecificDiagnosticOptions["CS1998"]);
Assert.Equal(ReportDiagnostic.Error, compilationOptions.SpecificDiagnosticOptions["CS7080"]);
Assert.Equal(ReportDiagnostic.Suppress, compilationOptions.SpecificDiagnosticOptions["CS7081"]); // NoWarn should be given priority over WarningsAsErrors
// CS7080 is both in WarningsAsErrors and WarningsNotAsErrors, but WarningsNotAsErrors are higher priority
Assert.Equal(ReportDiagnostic.Warn, compilationOptions.SpecificDiagnosticOptions["CS7080"]);
Assert.Equal(ReportDiagnostic.Warn, compilationOptions.SpecificDiagnosticOptions["CS7082"]);
// CS7081 is both WarningsAsErrors and NoWarn, but NoWarn are higher priority
Assert.Equal(ReportDiagnostic.Suppress, compilationOptions.SpecificDiagnosticOptions["CS7081"]);
}
}
}
Expand Down

0 comments on commit 77c928e

Please sign in to comment.