-
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
When replacing CodeStyle analyzers do not provide Features analyzers fallback options #75534
Merged
JoeRobich
merged 7 commits into
release/dev17.12
from
dev/jorobich/redirect-feature-analyzers
Oct 24, 2024
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0a838b9
Add a HasSdkCodeStyleAnalyzers property to ProjectAttributes
JoeRobich 180455c
Track unmapped AnalyzerReferences added by the project system.
JoeRobich 03eccdd
Selectively treat some Host analyzers as Project analyzers.
JoeRobich e9f7c04
Allow the DiagnosticComputer to treat some Host analyzers as Project …
JoeRobich 1198405
Added integration tests
JoeRobich 7344695
PR feedback
JoeRobich dff04fe
Backport test skip from #75612
JoeRobich 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
167 changes: 167 additions & 0 deletions
167
...sualStudio/IntegrationTest/New.IntegrationTests/CSharp/CSharpRedirectFeaturesAnalyzers.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,167 @@ | ||
// 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.Collections.Immutable; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Formatting; | ||
using Microsoft.CodeAnalysis.Options; | ||
using Microsoft.CodeAnalysis.Shared.TestHooks; | ||
using Roslyn.Test.Utilities; | ||
using Roslyn.VisualStudio.IntegrationTests; | ||
using Roslyn.VisualStudio.NewIntegrationTests.InProcess; | ||
using Xunit; | ||
|
||
namespace Roslyn.VisualStudio.NewIntegrationTests.CSharp; | ||
|
||
public class CSharpRedirectFeaturesAnalyzers : AbstractEditorTest | ||
{ | ||
protected override string LanguageName => LanguageNames.CSharp; | ||
|
||
private const int GlobalIndentationSize = 6; | ||
private const int DefaultIndentationSize = 4; | ||
|
||
[IdeFact] | ||
public async Task DoesNotUseHostOptions_WhenEnforceCodeStyleInBuildIsTrue() | ||
{ | ||
await SetupSolutionAsync( | ||
enforceCodeStyleInBuild: true, | ||
GlobalIndentationSize, | ||
HangMitigatingCancellationToken); | ||
|
||
var code = GenerateTestCode(GlobalIndentationSize); | ||
|
||
await TestServices.SolutionExplorer.AddFileAsync(ProjectName, "C.cs", code, open: true, cancellationToken: HangMitigatingCancellationToken); | ||
|
||
var errors = await GetErrorsFromErrorListAsync(HangMitigatingCancellationToken); | ||
AssertEx.Equal( | ||
[ | ||
"C.cs(3, 5): error IDE0055: Fix formatting", | ||
"C.cs(4, 5): error IDE0055: Fix formatting", | ||
"C.cs(6, 5): error IDE0055: Fix formatting", | ||
], | ||
errors); | ||
} | ||
|
||
[IdeFact] | ||
public async Task UsesHostOptions_WhenEnforceCodeStyleInBuildIsFalse() | ||
{ | ||
await SetupSolutionAsync( | ||
enforceCodeStyleInBuild: false, | ||
GlobalIndentationSize, | ||
HangMitigatingCancellationToken); | ||
|
||
var code = GenerateTestCode(DefaultIndentationSize); | ||
|
||
await TestServices.SolutionExplorer.AddFileAsync(ProjectName, "C.cs", code, open: true, cancellationToken: HangMitigatingCancellationToken); | ||
|
||
var errors = await GetErrorsFromErrorListAsync(HangMitigatingCancellationToken); | ||
AssertEx.Equal( | ||
[ | ||
"C.cs(3, 5): error IDE0055: Fix formatting", | ||
"C.cs(4, 5): error IDE0055: Fix formatting", | ||
"C.cs(6, 5): error IDE0055: Fix formatting", | ||
], | ||
errors); | ||
} | ||
|
||
private async Task SetupSolutionAsync(bool enforceCodeStyleInBuild, int globalIndentationSize, CancellationToken cancellationToken) | ||
{ | ||
await TestServices.SolutionExplorer.CreateSolutionAsync(SolutionName, cancellationToken); | ||
|
||
await TestServices.SolutionExplorer.AddCustomProjectAsync( | ||
ProjectName, | ||
".csproj", | ||
$""" | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<EnforceCodeStyleInBuild>{enforceCodeStyleInBuild}</EnforceCodeStyleInBuild> | ||
</PropertyGroup> | ||
|
||
</Project> | ||
""", | ||
cancellationToken); | ||
await TestServices.SolutionExplorer.RestoreNuGetPackagesAsync(ProjectName, cancellationToken); | ||
|
||
// Configure the global indentation size which would be part of the Host fallback options. | ||
var globalOptions = await TestServices.Shell.GetComponentModelServiceAsync<IGlobalOptionService>(cancellationToken); | ||
globalOptions.SetGlobalOption(FormattingOptions2.UseTabs, LanguageNames.CSharp, false); | ||
globalOptions.SetGlobalOption(FormattingOptions2.IndentationSize, LanguageNames.CSharp, globalIndentationSize); | ||
|
||
// Add .editorconfig to configure so that formatting diagnostics are errors | ||
await TestServices.SolutionExplorer.AddFileAsync(ProjectName, | ||
".editorconfig", | ||
""" | ||
root = true | ||
|
||
[*.cs] | ||
dotnet_diagnostic.IDE0055.severity = error | ||
""", | ||
open: false, | ||
cancellationToken); | ||
|
||
await TestServices.Workspace.WaitForAllAsyncOperationsAsync( | ||
[ | ||
FeatureAttribute.Workspace, | ||
FeatureAttribute.SolutionCrawlerLegacy, | ||
FeatureAttribute.DiagnosticService, | ||
FeatureAttribute.ErrorSquiggles | ||
], | ||
cancellationToken); | ||
} | ||
|
||
private static string GenerateTestCode(int indentationSize) | ||
{ | ||
var indentation = new string(' ', indentationSize); | ||
return $$""" | ||
class C | ||
{ | ||
{{indentation}}void M() | ||
{{indentation}}{ | ||
|
||
{{indentation}}} | ||
} | ||
"""; | ||
} | ||
|
||
private async Task<ImmutableArray<string>> GetErrorsFromErrorListAsync(CancellationToken cancellationToken) | ||
{ | ||
await WaitForCodeActionListToPopulateAsync(cancellationToken); | ||
|
||
await TestServices.ErrorList.ShowErrorListAsync(cancellationToken); | ||
await TestServices.Workspace.WaitForAllAsyncOperationsAsync( | ||
[ | ||
FeatureAttribute.Workspace, | ||
FeatureAttribute.SolutionCrawlerLegacy, | ||
FeatureAttribute.DiagnosticService, | ||
FeatureAttribute.ErrorSquiggles, | ||
FeatureAttribute.ErrorList | ||
], | ||
cancellationToken); | ||
return await TestServices.ErrorList.GetErrorsAsync(cancellationToken); | ||
} | ||
|
||
private async Task WaitForCodeActionListToPopulateAsync(CancellationToken cancellationToken) | ||
{ | ||
await TestServices.Editor.ActivateAsync(cancellationToken); | ||
await TestServices.Editor.PlaceCaretAsync("void M()", charsOffset: -1, cancellationToken); | ||
|
||
await TestServices.Editor.InvokeCodeActionListAsync(cancellationToken); | ||
|
||
await TestServices.Workspace.WaitForAllAsyncOperationsAsync( | ||
[ | ||
FeatureAttribute.Workspace, | ||
FeatureAttribute.SolutionCrawlerLegacy, | ||
FeatureAttribute.DiagnosticService, | ||
FeatureAttribute.ErrorSquiggles | ||
], | ||
cancellationToken); | ||
|
||
await TestServices.Editor.DismissLightBulbSessionAsync(cancellationToken); | ||
} | ||
} |
Oops, something went wrong.
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.
def don't like returning these as lazy enumerbles. can you make both an
ImmutableArray<ImmutableArray<...
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.
I could but then we have to do something about the common case where we aren't redirecting and are returning
analyzersPerReference.Values
which is anIEnumerable<ImmutableArray<DiagnosticAnalyzer>>
. The use of these IEnumerable's is also piped intoStateManager.CreateStateSetMap
. This might be cleanup for a follow up PR.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.
Ick :) Can that one change?
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.
at the very least, don't return mutable lists. return an immutable array here. that way we don't have to worry about it changing.
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.
Updated to use ArrayBuilder instead of List.