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

WIP: remove local diagnostics flag. #76864

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
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
19 changes: 5 additions & 14 deletions src/EditorFeatures/Test/CodeFixes/CodeFixServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,7 @@ public async Task TestGetFixesAsyncForDocumentDiagnosticAnalyzerAsync()
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/67354")]
public async Task TestGetFixesAsyncForGeneratorDiagnosticAsync()
{
// We have a special GeneratorDiagnosticsPlaceholderAnalyzer that report 0 SupportedDiagnostics.
// We need to ensure that we don't skip this special analyzer
// when computing the diagnostics/code fixes for "Normal" priority bucket, which
// normally only execute those analyzers which report at least one fixable supported diagnostic.
// Note that this special placeholder analyzer instance is always included for the project,
// we do not need to include it in the passed in analyzers.
Assert.Empty(GeneratorDiagnosticsPlaceholderAnalyzer.Instance.SupportedDiagnostics);
// We do not get generator diagnostics, and so we should not have code fixes for them.

var analyzers = ImmutableArray<DiagnosticAnalyzer>.Empty;
var generator = new MockAnalyzerReference.MockGenerator();
Expand All @@ -208,11 +202,8 @@ public async Task TestGetFixesAsyncForGeneratorDiagnosticAsync()
var fixCollectionSet = await tuple.codeFixService.GetFixesAsync(document, TextSpan.FromBounds(0, 0),
priorityProvider: new DefaultCodeActionRequestPriorityProvider(CodeActionRequestPriority.Default),
cancellationToken: CancellationToken.None);
Assert.True(codeFix.Called);
var fixCollection = Assert.Single(fixCollectionSet);
Assert.Equal(MockFixer.Id, fixCollection.FirstDiagnostic.Id);
var fix = Assert.Single(fixCollection.Fixes);
Assert.Equal(fixTitle, fix.Action.Title);
Assert.False(codeFix.Called);
Assert.Empty(fixCollectionSet);
}

[Fact]
Expand Down Expand Up @@ -1078,7 +1069,7 @@ void M()

await diagnosticIncrementalAnalyzer.GetDiagnosticsForIdsAsync(
sourceDocument.Project.Solution, sourceDocument.Project.Id, sourceDocument.Id, diagnosticIds: null, shouldIncludeAnalyzer: null, getDocuments: null,
includeLocalDocumentDiagnostics: true, includeNonLocalDocumentDiagnostics: true, CancellationToken.None);
includeNonLocalDocumentDiagnostics: true, CancellationToken.None);
await diagnosticIncrementalAnalyzer.GetTestAccessor().TextDocumentOpenAsync(sourceDocument);

var lowPriorityAnalyzerData = new SuggestedActionPriorityProvider.LowPriorityAnalyzersAndDiagnosticIds();
Expand Down Expand Up @@ -1142,7 +1133,7 @@ static bool GetExpectDeprioritization(
static async Task VerifyCachedDiagnosticsAsync(Document sourceDocument, bool expectedCachedDiagnostic, TextSpan testSpan, DiagnosticIncrementalAnalyzer diagnosticIncrementalAnalyzer)
{
var cachedDiagnostics = await diagnosticIncrementalAnalyzer.GetCachedDiagnosticsAsync(sourceDocument.Project.Solution, sourceDocument.Project.Id, sourceDocument.Id,
includeLocalDocumentDiagnostics: true, includeNonLocalDocumentDiagnostics: true, CancellationToken.None);
includeNonLocalDocumentDiagnostics: true, CancellationToken.None);
cachedDiagnostics = cachedDiagnostics.WhereAsArray(d => !d.IsSuppressed);

if (!expectedCachedDiagnostic)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public async Task TestHasSuccessfullyLoadedBeingFalse()

var diagnostics = await analyzer.GetDiagnosticsForIdsAsync(
workspace.CurrentSolution, projectId: null, documentId: null, diagnosticIds: null, shouldIncludeAnalyzer: null, getDocuments: null,
includeLocalDocumentDiagnostics: true, includeNonLocalDocumentDiagnostics: false, CancellationToken.None);
includeNonLocalDocumentDiagnostics: false, CancellationToken.None);
Assert.NotEmpty(diagnostics);
}

Expand Down Expand Up @@ -273,7 +273,6 @@ public async Task TestHostAnalyzerOrderingAsync()
AssertEx.Equal(
[
typeof(FileContentLoadAnalyzer),
typeof(GeneratorDiagnosticsPlaceholderAnalyzer),
typeof(CSharpCompilerDiagnosticAnalyzer),
typeof(Analyzer),
typeof(Priority0Analyzer),
Expand Down Expand Up @@ -847,9 +846,8 @@ void M()
Assert.Equal(CancellationTestAnalyzer.DiagnosticId, diagnostic.Id);
}

[Theory, WorkItem("https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1909806")]
[CombinatorialData]
internal async Task TestGeneratorProducedDiagnostics(bool fullSolutionAnalysis, bool analyzeProject, TestHost testHost)
[Theory, CombinatorialData, WorkItem("https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1909806")]
internal async Task TestGeneratorDoesNotProduceDiagnostics(bool fullSolutionAnalysis, bool analyzeProject, TestHost testHost)
{
using var workspace = EditorTestWorkspace.CreateCSharp("// This file will get a diagnostic", composition: s_featuresCompositionWithMockDiagnosticUpdateSourceRegistrationService.WithTestHostParts(testHost));

Expand All @@ -875,7 +873,7 @@ internal async Task TestGeneratorProducedDiagnostics(bool fullSolutionAnalysis,
var incrementalAnalyzer = service.CreateIncrementalAnalyzer(workspace);
var diagnostics = await incrementalAnalyzer.ForceAnalyzeProjectAsync(project, CancellationToken.None);

Assert.NotEmpty(diagnostics);
Assert.Empty(diagnostics);
}

private static Document GetDocumentFromIncompleteProject(AdhocWorkspace workspace)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ Namespace Microsoft.CodeAnalysis.Editor.Implementation.Diagnostics.UnitTests
Dim diagnosticProvider = GetDiagnosticProvider(workspace)
Dim actualDiagnostics = diagnosticProvider.GetDiagnosticsForIdsAsync(
workspace.CurrentSolution, projectId:=Nothing, documentId:=Nothing, diagnosticIds:=Nothing, shouldIncludeAnalyzer:=Nothing,
includeLocalDocumentDiagnostics:=True, includeNonLocalDocumentDiagnostics:=True, CancellationToken.None).Result
includeNonLocalDocumentDiagnostics:=True, CancellationToken.None).Result

If diagnostics Is Nothing Then
Assert.Empty(actualDiagnostics)
Expand Down
16 changes: 7 additions & 9 deletions src/EditorFeatures/Test2/Diagnostics/DiagnosticServiceTests.vb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ Imports Microsoft.CodeAnalysis.UnitTests.Diagnostics
Imports Roslyn.Utilities

Namespace Microsoft.CodeAnalysis.Editor.Implementation.Diagnostics.UnitTests

<[UseExportProvider]>
Public Class DiagnosticServiceTests

Expand Down Expand Up @@ -533,7 +532,7 @@ Namespace Microsoft.CodeAnalysis.Editor.Implementation.Diagnostics.UnitTests

diagnostics = Await diagnosticService.GetDiagnosticsForIdsAsync(
project.Solution, projectId:=Nothing, documentId:=Nothing, diagnosticIds:=Nothing, shouldIncludeAnalyzer:=Nothing,
includeLocalDocumentDiagnostics:=True, includeNonLocalDocumentDiagnostics:=True, CancellationToken.None)
includeNonLocalDocumentDiagnostics:=True, CancellationToken.None)
Dim diagnostic = diagnostics.First()
Assert.True(diagnostic.Id = "AD0001")
Assert.Contains("CodeBlockStartedAnalyzer", diagnostic.Message, StringComparison.Ordinal)
Expand Down Expand Up @@ -604,7 +603,7 @@ Namespace Microsoft.CodeAnalysis.Editor.Implementation.Diagnostics.UnitTests
Dim document = project.Documents.Single()
Dim diagnostics = Await diagnosticService.GetDiagnosticsForIdsAsync(
project.Solution, project.Id, documentId:=Nothing, diagnosticIds:=Nothing, shouldIncludeAnalyzer:=Nothing,
includeLocalDocumentDiagnostics:=True, includeNonLocalDocumentDiagnostics:=True, CancellationToken.None)
includeNonLocalDocumentDiagnostics:=True, CancellationToken.None)
Assert.Equal(1, diagnostics.Length)
Dim diagnostic = diagnostics.First()
Assert.Equal(OperationAnalyzer.Descriptor.Id, diagnostic.Id)
Expand Down Expand Up @@ -803,7 +802,7 @@ class AnonymousFunctions
Dim projectDiagnostics = Await diagnosticService.GetDiagnosticsForIdsAsync(
project.Solution, project.Id, documentId:=Nothing,
diagnosticIds:=Nothing, shouldIncludeAnalyzer:=Nothing,
includeLocalDocumentDiagnostics:=True, includeNonLocalDocumentDiagnostics:=True, CancellationToken.None)
includeNonLocalDocumentDiagnostics:=True, CancellationToken.None)
Assert.Equal(2, projectDiagnostics.Length)

Dim noLocationDiagnostic = projectDiagnostics.First(Function(d) d.DataLocation.DocumentId Is Nothing)
Expand Down Expand Up @@ -947,7 +946,7 @@ class AnonymousFunctions
Dim document = project.Documents.Single()
Dim diagnostics = (Await diagnosticService.GetDiagnosticsForIdsAsync(
project.Solution, project.Id, documentId:=Nothing, diagnosticIds:=Nothing, shouldIncludeAnalyzer:=Nothing,
includeLocalDocumentDiagnostics:=True, includeNonLocalDocumentDiagnostics:=True, CancellationToken.None)).
includeNonLocalDocumentDiagnostics:=True, CancellationToken.None)).
Select(Function(d) d.Id = NamedTypeAnalyzer.DiagDescriptor.Id)

Assert.Equal(1, diagnostics.Count)
Expand Down Expand Up @@ -1042,7 +1041,7 @@ class AnonymousFunctions
Dim incrementalAnalyzer = diagnosticService.CreateIncrementalAnalyzer(workspace)
Dim diagnostics = Await diagnosticService.GetDiagnosticsForIdsAsync(
project.Solution, project.Id, documentId:=Nothing, diagnosticIds:=Nothing, shouldIncludeAnalyzer:=Nothing,
includeLocalDocumentDiagnostics:=True, includeNonLocalDocumentDiagnostics:=True, CancellationToken.None)
includeNonLocalDocumentDiagnostics:=True, CancellationToken.None)
Assert.Equal(2, diagnostics.Length)
Dim file1HasDiag = False, file2HasDiag = False
For Each diagnostic In diagnostics
Expand Down Expand Up @@ -2133,7 +2132,6 @@ class MyClass
' Get cached project diagnostics.
Dim diagnostics = Await diagnosticService.GetCachedDiagnosticsAsync(
workspace, project.Id, documentId:=Nothing,
includeLocalDocumentDiagnostics:=True,
includeNonLocalDocumentDiagnostics:=True,
CancellationToken.None)

Expand All @@ -2145,7 +2143,7 @@ class MyClass
' Get diagnostics explicitly
Dim hiddenDiagnostics = Await diagnosticService.GetDiagnosticsForIdsAsync(
project.Solution, project.Id, documentId:=Nothing, diagnosticIds:=Nothing, shouldIncludeAnalyzer:=Nothing,
includeLocalDocumentDiagnostics:=True, includeNonLocalDocumentDiagnostics:=True, CancellationToken.None)
includeNonLocalDocumentDiagnostics:=True, CancellationToken.None)
Assert.Equal(1, hiddenDiagnostics.Length)
Assert.Equal(analyzer.Descriptor.Id, hiddenDiagnostics.Single().Id)
End Using
Expand Down Expand Up @@ -2232,7 +2230,7 @@ class C
Dim incrementalAnalyzer = diagnosticService.CreateIncrementalAnalyzer(workspace)
Dim diagnostics = Await diagnosticService.GetDiagnosticsForIdsAsync(
project.Solution, project.Id, documentId:=Nothing, diagnosticIds:=Nothing, shouldIncludeAnalyzer:=Nothing,
includeLocalDocumentDiagnostics:=True, includeNonLocalDocumentDiagnostics:=True, CancellationToken.None)
includeNonLocalDocumentDiagnostics:=True, CancellationToken.None)
Assert.Empty(diagnostics)
End Using
End Function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ public bool ContainsDiagnostics(Workspace workspace, ProjectId projectId)
public Task ForceAnalyzeProjectAsync(Project project, CancellationToken cancellationToken)
=> throw new NotImplementedException();

public Task<ImmutableArray<DiagnosticData>> GetCachedDiagnosticsAsync(Workspace workspace, ProjectId? projectId, DocumentId? documentId, bool includeLocalDocumentDiagnostics, bool includeNonLocalDocumentDiagnostics, CancellationToken cancellationToken)
public Task<ImmutableArray<DiagnosticData>> GetCachedDiagnosticsAsync(Workspace workspace, ProjectId? projectId, DocumentId? documentId, bool includeNonLocalDocumentDiagnostics, CancellationToken cancellationToken)
=> throw new NotImplementedException();

public Task<ImmutableArray<DiagnosticData>> GetDiagnosticsAsync(Solution solution, ProjectId? projectId, DocumentId? documentId, bool includeNonLocalDocumentDiagnostics, CancellationToken cancellationToken)
=> throw new NotImplementedException();

public Task<ImmutableArray<DiagnosticData>> GetDiagnosticsForIdsAsync(Solution solution, ProjectId? projectId, DocumentId? documentId, ImmutableHashSet<string>? diagnosticIds, Func<DiagnosticAnalyzer, bool>? shouldIncludeAnalyzer, Func<Project, DocumentId?, IReadOnlyList<DocumentId>>? getDocuments, bool includeLocalDocumentDiagnostics, bool includeNonLocalDocumentDiagnostics, CancellationToken cancellationToken)
public Task<ImmutableArray<DiagnosticData>> GetDiagnosticsForIdsAsync(Solution solution, ProjectId? projectId, DocumentId? documentId, ImmutableHashSet<string>? diagnosticIds, Func<DiagnosticAnalyzer, bool>? shouldIncludeAnalyzer, Func<Project, DocumentId?, IReadOnlyList<DocumentId>>? getDocuments, bool includeNonLocalDocumentDiagnostics, CancellationToken cancellationToken)
=> throw new NotImplementedException();

public Task<ImmutableArray<DiagnosticData>> GetDiagnosticsForSpanAsync(TextDocument document, TextSpan? range, Func<string, bool>? shouldIncludeDiagnostic, bool includeCompilerDiagnostics, ICodeActionRequestPriorityProvider priorityProvider, DiagnosticKind diagnosticKind, bool isExplicit, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ public async Task<ImmutableArray<DiagnosticData>> GetLastComputedDocumentDiagnos
return [];

var diagnostics = await _diagnosticAnalyzerService.GetCachedDiagnosticsAsync(
_workspace, documentId.ProjectId, documentId, includeLocalDocumentDiagnostics: true,
includeNonLocalDocumentDiagnostics: true, cancellationToken).ConfigureAwait(false);
_workspace, documentId.ProjectId, documentId, includeNonLocalDocumentDiagnostics: true, cancellationToken).ConfigureAwait(false);
return diagnostics.WhereAsArray(d => !d.IsSuppressed);
}

Expand All @@ -166,8 +165,7 @@ public async Task<ImmutableArray<DiagnosticData>> GetLastComputedProjectDiagnost
return [];

var diagnostics = await _diagnosticAnalyzerService.GetCachedDiagnosticsAsync(
_workspace, projectId, documentId: null, includeLocalDocumentDiagnostics: false,
includeNonLocalDocumentDiagnostics: false, cancellationToken).ConfigureAwait(false);
_workspace, projectId, documentId: null, includeNonLocalDocumentDiagnostics: false, cancellationToken).ConfigureAwait(false);
return diagnostics.WhereAsArray(d => !d.IsSuppressed);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Linq;
using System.Reflection;
using Microsoft.CodeAnalysis.Diagnostics.Telemetry;
using Microsoft.CodeAnalysis.Simplification;
using Roslyn.Utilities;

namespace Microsoft.CodeAnalysis.Diagnostics;
Expand All @@ -18,8 +17,7 @@ internal static class DiagnosticAnalyzerExtensions
public static bool IsWorkspaceDiagnosticAnalyzer(this DiagnosticAnalyzer analyzer)
=> analyzer is DocumentDiagnosticAnalyzer
|| analyzer is ProjectDiagnosticAnalyzer
|| analyzer == FileContentLoadAnalyzer.Instance
|| analyzer == GeneratorDiagnosticsPlaceholderAnalyzer.Instance;
|| analyzer == FileContentLoadAnalyzer.Instance;

public static bool IsBuiltInAnalyzer(this DiagnosticAnalyzer analyzer)
=> analyzer is IBuiltInAnalyzer || analyzer.IsWorkspaceDiagnosticAnalyzer() || analyzer.IsCompilerAnalyzer();
Expand Down
Loading
Loading