Skip to content

Commit 77c928e

Browse files
authored
Merge pull request #1784 from OmniSharp/feature/warnings-not-as-errors
added support for WarningsNotAsErrors msbuild property
2 parents 098b634 + 59e11b9 commit 77c928e

File tree

7 files changed

+36
-6
lines changed

7 files changed

+36
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ All changes to the project will be documented in this file.
33

44
## [1.35.2] - not yet released
55
* Added support for `WarningsAsErrors` in csproj files (PR: [#1779](https://github.com/OmniSharp/omnisharp-roslyn/pull/1779))
6+
* 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))
67

78
## [1.35.1] - 2020-05-04
89
* 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))

src/OmniSharp.MSBuild/ProjectFile/ProjectFileInfo.ProjectData.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ private class ProjectData
5151
public ImmutableArray<string> AdditionalFiles { get; }
5252
public ImmutableArray<string> AnalyzerConfigFiles { get; }
5353
public ImmutableArray<string> WarningsAsErrors { get; }
54+
public ImmutableArray<string> WarningsNotAsErrors { get; }
5455
public RuleSet RuleSet { get; }
5556
public ImmutableDictionary<string, string> ReferenceAliases { get; }
5657
public ImmutableDictionary<string, string> ProjectReferenceAliases { get; }
@@ -76,6 +77,7 @@ private ProjectData()
7677
ReferenceAliases = ImmutableDictionary<string, string>.Empty;
7778
ProjectReferenceAliases = ImmutableDictionary<string, string>.Empty;
7879
WarningsAsErrors = ImmutableArray<string>.Empty;
80+
WarningsNotAsErrors = ImmutableArray<string>.Empty;
7981
}
8082

8183
private ProjectData(
@@ -94,6 +96,7 @@ private ProjectData(
9496
ImmutableArray<string> preprocessorSymbolNames,
9597
ImmutableArray<string> suppressedDiagnosticIds,
9698
ImmutableArray<string> warningsAsErrors,
99+
ImmutableArray<string> warningsNotAsErrors,
97100
bool signAssembly,
98101
string assemblyOriginatorKeyFile,
99102
bool treatWarningsAsErrors,
@@ -126,6 +129,7 @@ private ProjectData(
126129
PreprocessorSymbolNames = preprocessorSymbolNames.EmptyIfDefault();
127130
SuppressedDiagnosticIds = suppressedDiagnosticIds.EmptyIfDefault();
128131
WarningsAsErrors = warningsAsErrors.EmptyIfDefault();
132+
WarningsNotAsErrors = warningsNotAsErrors.EmptyIfDefault();
129133

130134
SignAssembly = signAssembly;
131135
AssemblyOriginatorKeyFile = assemblyOriginatorKeyFile;
@@ -153,6 +157,7 @@ private ProjectData(
153157
ImmutableArray<string> preprocessorSymbolNames,
154158
ImmutableArray<string> suppressedDiagnosticIds,
155159
ImmutableArray<string> warningsAsErrors,
160+
ImmutableArray<string> warningsNotAsErrors,
156161
bool signAssembly,
157162
string assemblyOriginatorKeyFile,
158163
ImmutableArray<string> sourceFiles,
@@ -171,7 +176,7 @@ private ProjectData(
171176
ImmutableDictionary<string, string> projectReferenceAliases)
172177
: this(guid, name, assemblyName, targetPath, outputPath, intermediateOutputPath, projectAssetsFile,
173178
configuration, platform, targetFramework, targetFrameworks, outputKind, languageVersion, nullableContextOptions, allowUnsafeCode, checkForOverflowUnderflow,
174-
documentationFile, preprocessorSymbolNames, suppressedDiagnosticIds, warningsAsErrors, signAssembly, assemblyOriginatorKeyFile, treatWarningsAsErrors, defaultNamespace, runAnalyzers, runAnalyzersDuringLiveAnalysis, ruleset)
179+
documentationFile, preprocessorSymbolNames, suppressedDiagnosticIds, warningsAsErrors, warningsNotAsErrors, signAssembly, assemblyOriginatorKeyFile, treatWarningsAsErrors, defaultNamespace, runAnalyzers, runAnalyzersDuringLiveAnalysis, ruleset)
175180
{
176181
SourceFiles = sourceFiles.EmptyIfDefault();
177182
ProjectReferences = projectReferences.EmptyIfDefault();
@@ -216,6 +221,7 @@ public static ProjectData Create(MSB.Evaluation.Project project)
216221
var preprocessorSymbolNames = PropertyConverter.ToPreprocessorSymbolNames(project.GetPropertyValue(PropertyNames.DefineConstants));
217222
var suppressedDiagnosticIds = PropertyConverter.ToSuppressedDiagnosticIds(project.GetPropertyValue(PropertyNames.NoWarn));
218223
var warningsAsErrors = PropertyConverter.SplitList(project.GetPropertyValue(PropertyNames.WarningsAsErrors), ',');
224+
var warningsNotAsErrors = PropertyConverter.SplitList(project.GetPropertyValue(PropertyNames.WarningsNotAsErrors), ',');
219225
var signAssembly = PropertyConverter.ToBoolean(project.GetPropertyValue(PropertyNames.SignAssembly), defaultValue: false);
220226
var assemblyOriginatorKeyFile = project.GetPropertyValue(PropertyNames.AssemblyOriginatorKeyFile);
221227
var treatWarningsAsErrors = PropertyConverter.ToBoolean(project.GetPropertyValue(PropertyNames.TreatWarningsAsErrors), defaultValue: false);
@@ -225,7 +231,7 @@ public static ProjectData Create(MSB.Evaluation.Project project)
225231
return new ProjectData(
226232
guid, name, assemblyName, targetPath, outputPath, intermediateOutputPath, projectAssetsFile,
227233
configuration, platform, targetFramework, targetFrameworks, outputKind, languageVersion, nullableContextOptions, allowUnsafeCode, checkForOverflowUnderflow,
228-
documentationFile, preprocessorSymbolNames, suppressedDiagnosticIds, warningsAsErrors, signAssembly, assemblyOriginatorKeyFile, treatWarningsAsErrors, defaultNamespace, runAnalyzers, runAnalyzersDuringLiveAnalysis, ruleset: null);
234+
documentationFile, preprocessorSymbolNames, suppressedDiagnosticIds, warningsAsErrors, warningsNotAsErrors, signAssembly, assemblyOriginatorKeyFile, treatWarningsAsErrors, defaultNamespace, runAnalyzers, runAnalyzersDuringLiveAnalysis, ruleset: null);
229235
}
230236

231237
public static ProjectData Create(MSB.Execution.ProjectInstance projectInstance)
@@ -260,6 +266,7 @@ public static ProjectData Create(MSB.Execution.ProjectInstance projectInstance)
260266
var preprocessorSymbolNames = PropertyConverter.ToPreprocessorSymbolNames(projectInstance.GetPropertyValue(PropertyNames.DefineConstants));
261267
var suppressedDiagnosticIds = PropertyConverter.ToSuppressedDiagnosticIds(projectInstance.GetPropertyValue(PropertyNames.NoWarn));
262268
var warningsAsErrors = PropertyConverter.SplitList(projectInstance.GetPropertyValue(PropertyNames.WarningsAsErrors), ',');
269+
var warningsNotAsErrors = PropertyConverter.SplitList(projectInstance.GetPropertyValue(PropertyNames.WarningsNotAsErrors), ',');
263270
var signAssembly = PropertyConverter.ToBoolean(projectInstance.GetPropertyValue(PropertyNames.SignAssembly), defaultValue: false);
264271
var treatWarningsAsErrors = PropertyConverter.ToBoolean(projectInstance.GetPropertyValue(PropertyNames.TreatWarningsAsErrors), defaultValue: false);
265272
var runAnalyzers = PropertyConverter.ToBoolean(projectInstance.GetPropertyValue(PropertyNames.RunAnalyzers), defaultValue: true);
@@ -333,7 +340,7 @@ public static ProjectData Create(MSB.Execution.ProjectInstance projectInstance)
333340
return new ProjectData(guid, name,
334341
assemblyName, targetPath, outputPath, intermediateOutputPath, projectAssetsFile,
335342
configuration, platform, targetFramework, targetFrameworks,
336-
outputKind, languageVersion, nullableContextOptions, allowUnsafeCode, checkForOverflowUnderflow, documentationFile, preprocessorSymbolNames, suppressedDiagnosticIds, warningsAsErrors,
343+
outputKind, languageVersion, nullableContextOptions, allowUnsafeCode, checkForOverflowUnderflow, documentationFile, preprocessorSymbolNames, suppressedDiagnosticIds, warningsAsErrors, warningsNotAsErrors,
337344
signAssembly, assemblyOriginatorKeyFile,
338345
sourceFiles, projectReferences.ToImmutable(), references.ToImmutable(), packageReferences, analyzers, additionalFiles, editorConfigFiles, treatWarningsAsErrors, defaultNamespace, runAnalyzers, runAnalyzersDuringLiveAnalysis, ruleset,
339346
referenceAliases.ToImmutableDictionary(), projectReferenceAliases.ToImmutable());

src/OmniSharp.MSBuild/ProjectFile/ProjectFileInfo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ internal partial class ProjectFileInfo
4242
public ImmutableArray<string> PreprocessorSymbolNames => _data.PreprocessorSymbolNames;
4343
public ImmutableArray<string> SuppressedDiagnosticIds => _data.SuppressedDiagnosticIds;
4444
public ImmutableArray<string> WarningsAsErrors => _data.WarningsAsErrors;
45+
public ImmutableArray<string> WarningsNotAsErrors => _data.WarningsNotAsErrors;
4546

4647
public bool SignAssembly => _data.SignAssembly;
4748
public string AssemblyOriginatorKeyFile => _data.AssemblyOriginatorKeyFile;

src/OmniSharp.MSBuild/ProjectFile/ProjectFileInfoExtensions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ public static ImmutableDictionary<string, ReportDiagnostic> GetDiagnosticOptions
5555
var suppressions = CompilationOptionsHelper.GetDefaultSuppressedDiagnosticOptions(projectFileInfo.SuppressedDiagnosticIds);
5656
var specificRules = projectFileInfo.RuleSet?.SpecificDiagnosticOptions ?? ImmutableDictionary<string, ReportDiagnostic>.Empty;
5757

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

61+
// then handle WarningsAsErrors
6062
foreach (var warningAsError in projectFileInfo.WarningsAsErrors)
6163
{
6264
if (!suppressions.ContainsKey(warningAsError))
@@ -65,6 +67,15 @@ public static ImmutableDictionary<string, ReportDiagnostic> GetDiagnosticOptions
6567
}
6668
}
6769

70+
// WarningsNotAsErrors can overwrite WarningsAsErrors
71+
foreach (var warningNotAsError in projectFileInfo.WarningsNotAsErrors)
72+
{
73+
if (!suppressions.ContainsKey(warningNotAsError))
74+
{
75+
combinedRules[warningNotAsError] = ReportDiagnostic.Warn;
76+
}
77+
}
78+
6879
return combinedRules.ToImmutableDictionary();
6980
}
7081

src/OmniSharp.MSBuild/ProjectFile/PropertyNames.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@ internal static class PropertyNames
4646
public const string VisualStudioVersion = nameof(VisualStudioVersion);
4747
public const string VsInstallRoot = nameof(VsInstallRoot);
4848
public const string WarningsAsErrors = nameof(WarningsAsErrors);
49+
public const string WarningsNotAsErrors = nameof(WarningsNotAsErrors);
4950
}
5051
}

test-assets/test-projects/WarningsAsErrors/WarningsAsErrors.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>netcoreapp3.1</TargetFramework>
6-
<WarningsAsErrors>CS1998,CS7080,CS7081</WarningsAsErrors>
76
<NoWarn>CS7081</NoWarn>
7+
<WarningsAsErrors>CS1998,CS7080,CS7081</WarningsAsErrors>
8+
<WarningsNotAsErrors>CS7080,CS7082</WarningsNotAsErrors>
89
</PropertyGroup>
910

1011
</Project>

tests/OmniSharp.MSBuild.Tests/ProjectFileInfoTests.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,21 @@ public async Task WarningsAsErrors()
210210
Assert.Contains("CS7080", projectFileInfo.WarningsAsErrors);
211211
Assert.Contains("CS7081", projectFileInfo.WarningsAsErrors);
212212

213+
Assert.NotEmpty(projectFileInfo.WarningsNotAsErrors);
214+
Assert.Contains("CS7080", projectFileInfo.WarningsNotAsErrors);
215+
Assert.Contains("CS7082", projectFileInfo.WarningsNotAsErrors);
216+
213217
var compilationOptions = projectFileInfo.CreateCompilationOptions();
214218
Assert.True(compilationOptions.SpecificDiagnosticOptions.ContainsKey("CS1998"), "Specific diagnostic option for CS1998 not found");
215219
Assert.True(compilationOptions.SpecificDiagnosticOptions.ContainsKey("CS7080"), "Specific diagnostic option for CS7080 not found");
216220
Assert.True(compilationOptions.SpecificDiagnosticOptions.ContainsKey("CS7081"), "Specific diagnostic option for CS7081 not found");
221+
Assert.True(compilationOptions.SpecificDiagnosticOptions.ContainsKey("CS7082"), "Specific diagnostic option for CS7082 not found");
217222
Assert.Equal(ReportDiagnostic.Error, compilationOptions.SpecificDiagnosticOptions["CS1998"]);
218-
Assert.Equal(ReportDiagnostic.Error, compilationOptions.SpecificDiagnosticOptions["CS7080"]);
219-
Assert.Equal(ReportDiagnostic.Suppress, compilationOptions.SpecificDiagnosticOptions["CS7081"]); // NoWarn should be given priority over WarningsAsErrors
223+
// CS7080 is both in WarningsAsErrors and WarningsNotAsErrors, but WarningsNotAsErrors are higher priority
224+
Assert.Equal(ReportDiagnostic.Warn, compilationOptions.SpecificDiagnosticOptions["CS7080"]);
225+
Assert.Equal(ReportDiagnostic.Warn, compilationOptions.SpecificDiagnosticOptions["CS7082"]);
226+
// CS7081 is both WarningsAsErrors and NoWarn, but NoWarn are higher priority
227+
Assert.Equal(ReportDiagnostic.Suppress, compilationOptions.SpecificDiagnosticOptions["CS7081"]);
220228
}
221229
}
222230
}

0 commit comments

Comments
 (0)