-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Add BuildOnlyDiagnosticIds LSP request handler #69475
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/Features/LanguageServer/Protocol/Handler/Diagnostics/BuildOnlyDiagnosticIdsHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Composition; | ||
using System.Linq; | ||
using System.Runtime.Serialization; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Host.Mef; | ||
using Microsoft.CodeAnalysis.PooledObjects; | ||
using Roslyn.Utilities; | ||
|
||
namespace Microsoft.CodeAnalysis.LanguageServer.Handler; | ||
|
||
[DataContract] | ||
internal record class BuildOnlyDiagnosticIdsResult([property: DataMember(Name = "ids")] string[] Ids); | ||
|
||
[ExportCSharpVisualBasicStatelessLspService(typeof(BuildOnlyDiagnosticIdsHandler)), Shared] | ||
[Method(BuildOnlyDiagnosticIdsMethodName)] | ||
[method: ImportingConstructor] | ||
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] | ||
internal sealed class BuildOnlyDiagnosticIdsHandler( | ||
DiagnosticAnalyzerInfoCache.SharedGlobalCache globalCache, | ||
[ImportMany] IEnumerable<Lazy<ILspBuildOnlyDiagnostics, ILspBuildOnlyDiagnosticsMetadata>> compilerBuildOnlyDiagnosticsProviders) | ||
: ILspServiceRequestHandler<BuildOnlyDiagnosticIdsResult> | ||
{ | ||
public const string BuildOnlyDiagnosticIdsMethodName = "workspace/buildOnlyDiagnosticIds"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this a well known lsp method? or a roslyn extension? |
||
|
||
private readonly DiagnosticAnalyzerInfoCache.SharedGlobalCache _globalCache = globalCache; | ||
private readonly ImmutableDictionary<string, string[]> _compilerBuildOnlyDiagnosticIds = compilerBuildOnlyDiagnosticsProviders | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: _languageNameToBuildOnlyDiagnosticIds |
||
.ToImmutableDictionary(lazy => lazy.Metadata.LanguageName, lazy => lazy.Metadata.BuildOnlyDiagnostics); | ||
|
||
public bool MutatesSolutionState => false; | ||
public bool RequiresLSPSolution => true; | ||
|
||
public Task<BuildOnlyDiagnosticIdsResult> HandleRequestAsync(RequestContext context, CancellationToken cancellationToken) | ||
{ | ||
Contract.ThrowIfNull(context.Solution); | ||
|
||
using var _1 = ArrayBuilder<string>.GetInstance(out var builder); | ||
foreach (var languageName in context.Solution.Projects.Select(p => p.Language).Distinct()) | ||
{ | ||
if (_compilerBuildOnlyDiagnosticIds.TryGetValue(languageName, out var compilerBuildOnlyDiagnosticIds)) | ||
{ | ||
builder.AddRange(compilerBuildOnlyDiagnosticIds); | ||
} | ||
} | ||
|
||
using var _2 = PooledHashSet<(object Reference, string Language)>.GetInstance(out var seenAnalyzerReferencesByLanguage); | ||
|
||
foreach (var project in context.Solution.Projects) | ||
{ | ||
var analyzersPerReferenceMap = context.Solution.State.Analyzers.CreateDiagnosticAnalyzersPerReference(project); | ||
foreach (var (analyzerReference, analyzers) in analyzersPerReferenceMap) | ||
{ | ||
if (!seenAnalyzerReferencesByLanguage.Add((analyzerReference, project.Language))) | ||
continue; | ||
|
||
foreach (var analyzer in analyzers) | ||
{ | ||
// We have already added the compiler build-only diagnostics upfront. | ||
if (analyzer.IsCompilerAnalyzer()) | ||
continue; | ||
|
||
foreach (var buildOnlyDescriptor in _globalCache.AnalyzerInfoCache.GetCompilationEndDiagnosticDescriptors(analyzer)) | ||
{ | ||
builder.Add(buildOnlyDescriptor.Id); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return Task.FromResult(new BuildOnlyDiagnosticIdsResult(builder.ToArray())); | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
...atures/LanguageServer/ProtocolUnitTests/Diagnostics/BuildOnlyDiagnosticIdsHandlerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
#nullable disable | ||
|
||
using System.Collections.Immutable; | ||
using System.Reflection; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.LanguageServer.Handler; | ||
using Microsoft.CodeAnalysis.PooledObjects; | ||
using Roslyn.Test.Utilities; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Microsoft.CodeAnalysis.LanguageServer.UnitTests.Completion | ||
{ | ||
public class BuildOnlyDiagnosticIdsHandlerTests(ITestOutputHelper testOutputHelper) : AbstractLanguageServerProtocolTests(testOutputHelper) | ||
{ | ||
[Theory, CombinatorialData] | ||
[WorkItem("https://github.com/dotnet/vscode-csharp/issues/5728")] | ||
public async Task TestCSharpBuildOnlyDiagnosticIdsAsync(bool mutatingLspWorkspace) | ||
{ | ||
await using var testLspServer = await CreateTestLspServerAsync("class C { }", mutatingLspWorkspace); | ||
|
||
var result = await testLspServer.ExecuteRequest0Async<BuildOnlyDiagnosticIdsResult>(BuildOnlyDiagnosticIdsHandler.BuildOnlyDiagnosticIdsMethodName, | ||
CancellationToken.None); | ||
|
||
var expectedBuildOnlyDiagnosticIds = GetExpectedBuildOnlyDiagnosticIds(LanguageNames.CSharp); | ||
AssertEx.SetEqual(expectedBuildOnlyDiagnosticIds, result.Ids); | ||
} | ||
|
||
[Theory, CombinatorialData] | ||
[WorkItem("https://github.com/dotnet/vscode-csharp/issues/5728")] | ||
public async Task TestVisualBasicBuildOnlyDiagnosticIdsAsync(bool mutatingLspWorkspace) | ||
{ | ||
await using var testLspServer = await CreateVisualBasicTestLspServerAsync(markup: @" | ||
Class C | ||
End Class", mutatingLspWorkspace); | ||
|
||
var result = await testLspServer.ExecuteRequest0Async<BuildOnlyDiagnosticIdsResult>(BuildOnlyDiagnosticIdsHandler.BuildOnlyDiagnosticIdsMethodName, | ||
CancellationToken.None); | ||
|
||
var expectedBuildOnlyDiagnosticIds = GetExpectedBuildOnlyDiagnosticIds(LanguageNames.VisualBasic); | ||
AssertEx.SetEqual(expectedBuildOnlyDiagnosticIds, result.Ids); | ||
} | ||
|
||
private protected override TestAnalyzerReferenceByLanguage CreateTestAnalyzersReference() | ||
{ | ||
var builder = ImmutableDictionary.CreateBuilder<string, ImmutableArray<DiagnosticAnalyzer>>(); | ||
builder.Add(LanguageNames.CSharp, ImmutableArray.Create( | ||
DiagnosticExtensions.GetCompilerDiagnosticAnalyzer(LanguageNames.CSharp), | ||
new BuildOnlyAnalyzer(), | ||
new LiveAnalyzer())); | ||
builder.Add(LanguageNames.VisualBasic, ImmutableArray.Create( | ||
DiagnosticExtensions.GetCompilerDiagnosticAnalyzer(LanguageNames.VisualBasic), | ||
new BuildOnlyAnalyzer(), | ||
new LiveAnalyzer())); | ||
return new(builder.ToImmutableDictionary()); | ||
} | ||
|
||
private static string[] GetExpectedBuildOnlyDiagnosticIds(string languageName) | ||
{ | ||
using var _ = ArrayBuilder<string>.GetInstance(out var builder); | ||
|
||
// NOTE: 'CSharpLspBuildOnlyDiagnosticsTests' and 'VisualBasicLspBuildOnlyDiagnosticsTests' already verify that | ||
// the corresponding build-only diagnostic providers return expected compiler build-only diagnostic IDs. | ||
// So, here we just directly append 'attribute.BuildOnlyDiagnostics' from these providers to our expected build-only diagnostic IDs. | ||
var compilerBuildOnlyDiagnosticsType = languageName switch | ||
{ | ||
LanguageNames.CSharp => typeof(CSharp.LanguageServer.CSharpLspBuildOnlyDiagnostics), | ||
LanguageNames.VisualBasic => typeof(VisualBasic.LanguageServer.VisualBasicLspBuildOnlyDiagnostics), | ||
_ => null | ||
}; | ||
|
||
if (compilerBuildOnlyDiagnosticsType != null) | ||
{ | ||
var attribute = compilerBuildOnlyDiagnosticsType.GetCustomAttribute<LspBuildOnlyDiagnosticsAttribute>(); | ||
builder.AddRange(attribute.BuildOnlyDiagnostics); | ||
} | ||
|
||
builder.Add(BuildOnlyAnalyzer.Id); | ||
return builder.ToArray(); | ||
} | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] | ||
private sealed class BuildOnlyAnalyzer : DiagnosticAnalyzer | ||
{ | ||
public const string Id = "BuildOnly0001"; | ||
private static readonly DiagnosticDescriptor s_descriptor = new(Id, "Title", "Message", "Category", DiagnosticSeverity.Warning, isEnabledByDefault: true, customTags: new[] { WellKnownDiagnosticTags.CompilationEnd }); | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(s_descriptor); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
} | ||
} | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] | ||
private sealed class LiveAnalyzer : DiagnosticAnalyzer | ||
{ | ||
public const string Id = "Live0001"; | ||
private static readonly DiagnosticDescriptor s_descriptor = new(Id, "Title", "Message", "Category", DiagnosticSeverity.Warning, isEnabledByDefault: true); | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(s_descriptor); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indentation.