Skip to content

Remove feature options entirely #77806

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

Merged
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
108 changes: 55 additions & 53 deletions src/EditorFeatures/Test/Diagnostics/DiagnosticAnalyzerServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,12 @@ public async Task TestDisabledByDefaultAnalyzerEnabledWithEditorConfig(bool enab

if (enabledWithEditorconfig)
{
var editorconfigText = @$"
[*.cs]
dotnet_diagnostic.{DisabledByDefaultAnalyzer.s_syntaxRule.Id}.severity = warning
dotnet_diagnostic.{DisabledByDefaultAnalyzer.s_semanticRule.Id}.severity = warning
dotnet_diagnostic.{DisabledByDefaultAnalyzer.s_compilationRule.Id}.severity = warning";
var editorconfigText = $"""
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just test cleanup.

[*.cs]
dotnet_diagnostic.{DisabledByDefaultAnalyzer.s_syntaxRule.Id}.severity = warning
dotnet_diagnostic.{DisabledByDefaultAnalyzer.s_semanticRule.Id}.severity = warning
dotnet_diagnostic.{DisabledByDefaultAnalyzer.s_compilationRule.Id}.severity = warning
""";

project = project.AddAnalyzerConfigDocument(".editorconfig", filePath: "z:\\.editorconfig", text: SourceText.From(editorconfigText)).Project;
}
Expand Down Expand Up @@ -345,10 +346,10 @@ public async Task TestFullSolutionAnalysisForHiddenAnalyzers_SeverityInAnalyzerC

// Escalating the analyzer to non-hidden effective severity through analyzer config options
// ensures that analyzer executes in full solution analysis.
var analyzerConfigText = $@"
[*.cs]
dotnet_diagnostic.{NamedTypeAnalyzer.DiagnosticId}.severity = warning
";
var analyzerConfigText = $"""
[*.cs]
dotnet_diagnostic.{NamedTypeAnalyzer.DiagnosticId}.severity = warning
""";

project = project.AddAnalyzerConfigDocument(
".editorconfig",
Expand Down Expand Up @@ -554,36 +555,36 @@ internal async Task TestRemoveUnnecessaryInlineSuppressionsAnalyzer(BackgroundAn
string code;
if (testPragma)
{
code = $@"
#pragma warning disable {NamedTypeAnalyzer.DiagnosticId} // Unnecessary
#pragma warning disable CS0168 // Variable is declared but never used - Unnecessary

#pragma warning disable {NamedTypeAnalyzer.DiagnosticId} // Necessary
class A
{{
void M()
{{
#pragma warning disable CS0168 // Variable is declared but never used - Necessary
int x;
}}
}}
";
code = $$"""
#pragma warning disable {{NamedTypeAnalyzer.DiagnosticId}} // Unnecessary
#pragma warning disable CS0168 // Variable is declared but never used - Unnecessary

#pragma warning disable {{NamedTypeAnalyzer.DiagnosticId}} // Necessary
class A
{
void M()
{
#pragma warning disable CS0168 // Variable is declared but never used - Necessary
int x;
}
}
""";
}
else
{
code = $@"
[System.Diagnostics.CodeAnalysis.SuppressMessage(""Category1"", ""{NamedTypeAnalyzer.DiagnosticId}"")] // Necessary
class A
{{
[System.Diagnostics.CodeAnalysis.SuppressMessage(""Category2"", ""{NamedTypeAnalyzer.DiagnosticId}"")] // Unnecessary
[System.Diagnostics.CodeAnalysis.SuppressMessage(""Category3"", ""CS0168"")] // Unnecessary
void M()
{{
#pragma warning disable CS0168 // Variable is declared but never used - Necessary
int x;
}}
}}
";
code = $$"""
[System.Diagnostics.CodeAnalysis.SuppressMessage("Category1", "{{NamedTypeAnalyzer.DiagnosticId}}")] // Necessary
class A
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Category2", "{{NamedTypeAnalyzer.DiagnosticId}}")] // Unnecessary
[System.Diagnostics.CodeAnalysis.SuppressMessage("Category3", "CS0168")] // Unnecessary
void M()
{
#pragma warning disable CS0168 // Variable is declared but never used - Necessary
int x;
}
}
""";
}

string[] files;
Expand All @@ -605,7 +606,6 @@ void M()
using var workspace = new EditorTestWorkspace(composition);

workspace.GlobalOptions.SetGlobalOption(SolutionCrawlerOptionsStorage.BackgroundAnalysisScopeOption, LanguageNames.CSharp, analysisScope);
workspace.GlobalOptions.SetGlobalOption(SolutionCrawlerOptionsStorage.EnableDiagnosticsInSourceGeneratedFiles, isSourceGenerated);

var compilerDiagnosticsScope = analysisScope.ToEquivalentCompilerDiagnosticsScope();
workspace.GlobalOptions.SetGlobalOption(SolutionCrawlerOptionsStorage.CompilerDiagnosticsScopeOption, LanguageNames.CSharp, compilerDiagnosticsScope);
Expand Down Expand Up @@ -714,14 +714,15 @@ internal async Task TestOnlyRequiredAnalyzerExecutedDuringDiagnosticComputation(
[CombinatorialData]
public async Task TestFilterSpanOnContextAsync(FilterSpanTestAnalyzer.AnalysisKind kind)
{
var source = @"
class B
{
void M()
{
int x = 1;
}
}";
var source = """
class B
{
void M()
{
int x = 1;
}
}
""";
var additionalText = @"This is an additional file!";

using var workspace = TestWorkspace.CreateCSharp(source);
Expand Down Expand Up @@ -788,14 +789,15 @@ internal async Task TestCancellationDuringDiagnosticComputation_OutOfProc(Analyz
// NOTE: Unfortunately, we cannot perform an end-to-end OutOfProc test, similar to the InProc test above because AnalyzerImageReference is not serializable.
// So, we perform a very targeted test which directly uses the 'DiagnosticComputer' type that is used for all OutOfProc diagnostic computation.

var source = @"
class A
{
void M()
{
int x = 0;
}
}";
var source = """
class A
{
void M()
{
int x = 0;
}
}
""";

using var workspace = TestWorkspace.CreateCSharp(source);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public void SerializationAndDeserializationForNullableBool([CombinatorialValues(
CompletionViewOptionsStorage.EnableArgumentCompletionSnippets,
FeatureOnOffOptions.OfferRemoveUnusedReferences,
InheritanceMarginOptionsStorage.ShowInheritanceMargin,
SolutionCrawlerOptionsStorage.EnableDiagnosticsInSourceGeneratedFiles,
CompletionOptionsStorage.ShowItemsFromUnimportedNamespaces,
CompletionOptionsStorage.ShowNewSnippetExperienceUserOption,
CompletionOptionsStorage.TriggerOnDeletion,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ internal sealed partial class UnitTestingWorkCoordinator : IUnitTestingWorkCoord
{
private readonly CountLogAggregator<WorkspaceChangeKind> _logAggregator = new();
private readonly IAsynchronousOperationListener _listener;
private readonly Microsoft.CodeAnalysis.SolutionCrawler.ISolutionCrawlerOptionsService? _solutionCrawlerOptionsService;

private readonly CancellationTokenSource _shutdownNotificationSource = new();
private readonly CancellationToken _shutdownToken;
Expand All @@ -43,7 +42,6 @@ public UnitTestingWorkCoordinator(
Registration = registration;

_listener = listener;
_solutionCrawlerOptionsService = Registration.Services.GetService<Microsoft.CodeAnalysis.SolutionCrawler.ISolutionCrawlerOptionsService>();

// event and worker queues
_shutdownToken = _shutdownNotificationSource.Token;
Expand Down Expand Up @@ -294,36 +292,34 @@ private void EnqueueDocumentChangedEvent(Solution oldSolution, Solution newSolut

// If all features are enabled for source generated documents, the solution crawler needs to
// include them in incremental analysis.
if (_solutionCrawlerOptionsService?.EnableDiagnosticsInSourceGeneratedFiles == true)

// TODO: if this becomes a hot spot, we should be able to expose/access the dictionary
// underneath GetSourceGeneratedDocumentsAsync rather than create a new one here.
var oldProjectSourceGeneratedDocuments = await oldProject.GetSourceGeneratedDocumentsAsync(_shutdownToken).ConfigureAwait(false);
var oldProjectSourceGeneratedDocumentsById = oldProjectSourceGeneratedDocuments.ToDictionary(static document => document.Id);
var newProjectSourceGeneratedDocuments = await newProject.GetSourceGeneratedDocumentsAsync(_shutdownToken).ConfigureAwait(false);
var newProjectSourceGeneratedDocumentsById = newProjectSourceGeneratedDocuments.ToDictionary(static document => document.Id);

foreach (var (oldDocumentId, _) in oldProjectSourceGeneratedDocumentsById)
{
// TODO: if this becomes a hot spot, we should be able to expose/access the dictionary
// underneath GetSourceGeneratedDocumentsAsync rather than create a new one here.
var oldProjectSourceGeneratedDocuments = await oldProject.GetSourceGeneratedDocumentsAsync(_shutdownToken).ConfigureAwait(false);
var oldProjectSourceGeneratedDocumentsById = oldProjectSourceGeneratedDocuments.ToDictionary(static document => document.Id);
var newProjectSourceGeneratedDocuments = await newProject.GetSourceGeneratedDocumentsAsync(_shutdownToken).ConfigureAwait(false);
var newProjectSourceGeneratedDocumentsById = newProjectSourceGeneratedDocuments.ToDictionary(static document => document.Id);

foreach (var (oldDocumentId, _) in oldProjectSourceGeneratedDocumentsById)
if (!newProjectSourceGeneratedDocumentsById.ContainsKey(oldDocumentId))
{
if (!newProjectSourceGeneratedDocumentsById.ContainsKey(oldDocumentId))
{
// This source generated document was removed
EnqueueFullDocumentEvent(oldSolution, oldDocumentId, UnitTestingInvocationReasons.DocumentRemoved);
}
// This source generated document was removed
EnqueueFullDocumentEvent(oldSolution, oldDocumentId, UnitTestingInvocationReasons.DocumentRemoved);
}
}

foreach (var (newDocumentId, newDocument) in newProjectSourceGeneratedDocumentsById)
foreach (var (newDocumentId, newDocument) in newProjectSourceGeneratedDocumentsById)
{
if (!oldProjectSourceGeneratedDocumentsById.TryGetValue(newDocumentId, out var oldDocument))
{
if (!oldProjectSourceGeneratedDocumentsById.TryGetValue(newDocumentId, out var oldDocument))
{
// This source generated document was added
EnqueueFullDocumentEvent(newSolution, newDocumentId, UnitTestingInvocationReasons.DocumentAdded);
}
else
{
// This source generated document may have changed
await EnqueueChangedDocumentWorkItemAsync(oldDocument, newDocument).ConfigureAwait(continueOnCapturedContext: false);
}
// This source generated document was added
EnqueueFullDocumentEvent(newSolution, newDocumentId, UnitTestingInvocationReasons.DocumentAdded);
}
else
{
// This source generated document may have changed
await EnqueueChangedDocumentWorkItemAsync(oldDocument, newDocument).ConfigureAwait(continueOnCapturedContext: false);
}
}
});
Expand Down Expand Up @@ -382,11 +378,8 @@ private async Task EnqueueFullProjectWorkItemAsync(Project project, UnitTestingI

// If all features are enabled for source generated documents, the solution crawler needs to
// include them in incremental analysis.
if (_solutionCrawlerOptionsService?.EnableDiagnosticsInSourceGeneratedFiles == true)
{
foreach (var document in await project.GetSourceGeneratedDocumentsAsync(_shutdownToken).ConfigureAwait(false))
await EnqueueDocumentWorkItemAsync(project, document.Id, document, invocationReasons).ConfigureAwait(false);
}
foreach (var document in await project.GetSourceGeneratedDocumentsAsync(_shutdownToken).ConfigureAwait(false))
await EnqueueDocumentWorkItemAsync(project, document.Id, document, invocationReasons).ConfigureAwait(false);
}

private async Task EnqueueWorkItemAsync(IUnitTestingIncrementalAnalyzer analyzer, UnitTestingReanalyzeScope scope)
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ internal static class SolutionCrawlerOptionsStorage
public static readonly PerLanguageOption2<CompilerDiagnosticsScope> CompilerDiagnosticsScopeOption = new(
"dotnet_compiler_diagnostics_scope", defaultValue: CompilerDiagnosticsScope.OpenFiles, group: s_backgroundAnalysisOptionGroup, serializer: EditorConfigValueSerializer.CreateSerializerForEnum<CompilerDiagnosticsScope>());

public static readonly Option2<bool> EnableDiagnosticsInSourceGeneratedFiles = new(
"dotnet_enable_diagnostics_in_source_generated_files", defaultValue: true, group: s_backgroundAnalysisOptionGroup);

/// <summary>
/// Enables forced <see cref="BackgroundAnalysisScope.Minimal"/> scope when low VM is detected to improve performance.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public async ValueTask<ImmutableArray<IDiagnosticSource>> CreateDiagnosticSource
using var _ = ArrayBuilder<IDiagnosticSource>.GetInstance(out var result);

var solution = context.Solution;
var enableDiagnosticsInSourceGeneratedFiles = solution.Services.GetService<ISolutionCrawlerOptionsService>()?.EnableDiagnosticsInSourceGeneratedFiles == true;
var codeAnalysisService = solution.Services.GetRequiredService<ICodeAnalysisDiagnosticAnalyzerService>();

foreach (var project in WorkspaceDiagnosticSourceHelpers.GetProjectsInPriorityOrder(solution, context.SupportedLanguages))
Expand All @@ -75,12 +74,8 @@ async Task AddDocumentsAndProjectAsync(Project project, IDiagnosticAnalyzerServi
AddDocumentSources(project.Documents);
AddDocumentSources(project.AdditionalDocuments);

// If all features are enabled for source generated documents, then compute todo-comments/diagnostics for them.
if (enableDiagnosticsInSourceGeneratedFiles)
{
var sourceGeneratedDocuments = await project.GetSourceGeneratedDocumentsAsync(cancellationToken).ConfigureAwait(false);
AddDocumentSources(sourceGeneratedDocuments);
}
var sourceGeneratedDocuments = await project.GetSourceGeneratedDocumentsAsync(cancellationToken).ConfigureAwait(false);
AddDocumentSources(sourceGeneratedDocuments);

// Finally, add the appropriate FSA or CodeAnalysis project source to get project specific diagnostics, not associated with any document.
AddProjectSource();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,7 @@ private protected static InitializationOptions GetInitializationOptions(
bool useVSDiagnostics,
WellKnownLspServerKinds serverKind = WellKnownLspServerKinds.AlwaysActiveVSLspServer,
string[]? sourceGeneratedMarkups = null,
IEnumerable<DiagnosticAnalyzer>? additionalAnalyzers = null,
bool enableDiagnosticsInSourceGeneratedFiles = true)
IEnumerable<DiagnosticAnalyzer>? additionalAnalyzers = null)
{
// If no explicit compiler diagnostics scope has been provided, match it with the provided analyzer diagnostics scope
compilerDiagnosticsScope ??= analyzerDiagnosticsScope switch
Expand All @@ -354,7 +353,6 @@ private protected static InitializationOptions GetInitializationOptions(
globalOptions.SetGlobalOption(SolutionCrawlerOptionsStorage.CompilerDiagnosticsScopeOption, LanguageNames.CSharp, compilerDiagnosticsScope.Value);
globalOptions.SetGlobalOption(SolutionCrawlerOptionsStorage.CompilerDiagnosticsScopeOption, LanguageNames.VisualBasic, compilerDiagnosticsScope.Value);
globalOptions.SetGlobalOption(SolutionCrawlerOptionsStorage.CompilerDiagnosticsScopeOption, InternalLanguageNames.TypeScript, compilerDiagnosticsScope.Value);
globalOptions.SetGlobalOption(SolutionCrawlerOptionsStorage.EnableDiagnosticsInSourceGeneratedFiles, enableDiagnosticsInSourceGeneratedFiles);
},
ServerKind = serverKind,
SourceGeneratedMarkups = sourceGeneratedMarkups ?? [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1048,12 +1048,12 @@ public async Task TestWorkspaceDiagnosticsForClosedFilesWithRunCodeAnalysisFSAOn
}

[Theory, CombinatorialData]
public async Task SourceGeneratorFailures_FSA(bool useVSDiagnostics, bool mutatingLspWorkspace, bool enableDiagnosticsInSourceGeneratedFiles)
public async Task SourceGeneratorFailures_FSA(bool useVSDiagnostics, bool mutatingLspWorkspace)
{
await using var testLspServer = await CreateTestLspServerAsync(["class C {}"], mutatingLspWorkspace,
GetInitializationOptions(BackgroundAnalysisScope.FullSolution, CompilerDiagnosticsScope.FullSolution, useVSDiagnostics, enableDiagnosticsInSourceGeneratedFiles: enableDiagnosticsInSourceGeneratedFiles));
GetInitializationOptions(BackgroundAnalysisScope.FullSolution, CompilerDiagnosticsScope.FullSolution, useVSDiagnostics));

var generator = new Roslyn.Test.Utilities.TestGenerators.TestSourceGenerator()
var generator = new TestSourceGenerator()
{
ExecuteImpl = context => throw new InvalidOperationException("Source generator failed")
};
Expand Down
Loading
Loading