Skip to content

Commit

Permalink
Handle /warnaserror with Warning analyzer bulk configuration in edito…
Browse files Browse the repository at this point in the history
…rconfig

Fixes batch compiler issue in #55541. I'll create a separate PR for the fix on IDE side
  • Loading branch information
mavasani committed Dec 22, 2021
1 parent 785ce8c commit 3ae6b3c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/Compilers/CSharp/Test/CommandLine/CommandLineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12479,6 +12479,11 @@ public void TestCategoryBasedBulkAnalyzerDiagnosticConfiguration(DiagnosticSever
dotnet_analyzer_diagnostic.category-{category}.severity = error";
TestBulkAnalyzerConfigurationCore(analyzer, analyzerConfigText, errorlog, expectedDiagnosticSeverity: ReportDiagnostic.Warn);

// Verify category based configuration to warning + /warnaserror reports errors.
analyzerConfigText = $@"
[*.cs]
dotnet_analyzer_diagnostic.category-{category}.severity = warning";
TestBulkAnalyzerConfigurationCore(analyzer, analyzerConfigText, errorlog, warnAsError: true, expectedDiagnosticSeverity: ReportDiagnostic.Error);

// Verify disabled by default analyzer is not enabled by category based configuration.
analyzer = new NamedTypeAnalyzerWithConfigurableEnabledByDefault(isEnabledByDefault: false, defaultSeverity);
Expand Down Expand Up @@ -12559,6 +12564,12 @@ public void TestBulkAnalyzerDiagnosticConfiguration(DiagnosticSeverity defaultSe
dotnet_analyzer_diagnostic.severity = error";
TestBulkAnalyzerConfigurationCore(analyzer, analyzerConfigText, errorlog, expectedDiagnosticSeverity: ReportDiagnostic.Warn);

// Verify bulk configuration to warning + /warnaserror reports errors.
analyzerConfigText = $@"
[*.cs]
dotnet_analyzer_diagnostic.severity = warning";
TestBulkAnalyzerConfigurationCore(analyzer, analyzerConfigText, errorlog, warnAsError: true, expectedDiagnosticSeverity: ReportDiagnostic.Error);

// Verify disabled by default analyzer is not enabled by bulk configuration.
analyzer = new NamedTypeAnalyzerWithConfigurableEnabledByDefault(isEnabledByDefault: false, defaultSeverity);
analyzerConfigText = $@"
Expand Down Expand Up @@ -12636,7 +12647,8 @@ private void TestBulkAnalyzerConfigurationCore(
bool errorlog,
ReportDiagnostic expectedDiagnosticSeverity,
string rulesetText = null,
bool noWarn = false)
bool noWarn = false,
bool warnAsError = false)
{
var diagnosticId = analyzer.Descriptor.Id;
var dir = Temp.CreateDirectory();
Expand All @@ -12654,6 +12666,11 @@ private void TestBulkAnalyzerConfigurationCore(
arguments = arguments.Append($"/nowarn:{diagnosticId}");
}

if (warnAsError)
{
arguments = arguments.Append($"/warnaserror");
}

if (errorlog)
{
arguments = arguments.Append($"/errorlog:errorlog");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public static bool TryGetSeverityFromBulkConfiguration(
if (analyzerConfigOptions.TryGetValue(categoryBasedKey, out var value) &&
AnalyzerConfigSet.TryParseSeverity(value, out severity))
{
// '/warnaserror' should bump Warning bulk configuration to Error.
if (severity == ReportDiagnostic.Warn && compilation.Options.GeneralDiagnosticOption == ReportDiagnostic.Error)
severity = ReportDiagnostic.Error;

return true;
}

Expand All @@ -74,6 +78,10 @@ public static bool TryGetSeverityFromBulkConfiguration(
if (analyzerConfigOptions.TryGetValue(DotnetAnalyzerDiagnosticSeverityKey, out value) &&
AnalyzerConfigSet.TryParseSeverity(value, out severity))
{
// '/warnaserror' should bump Warning bulk configuration to Error.
if (severity == ReportDiagnostic.Warn && compilation.Options.GeneralDiagnosticOption == ReportDiagnostic.Error)
severity = ReportDiagnostic.Error;

return true;
}

Expand Down

0 comments on commit 3ae6b3c

Please sign in to comment.