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

Port few IDE analyzers/fixers to shared layer to be enabled in CodeStyle NuGet package #41363

Closed

Conversation

mavasani
Copy link
Contributor

@mavasani mavasani commented Feb 3, 2020

NOTE: More than half the changed files in this PR are xlf files and majority of source file changes are just using resources from a shared resx file. Strongly recommend reviewing with GitHub filter feature to collapse all xlf file changes: Review Link

Following analyzers/fixers have been ported to the shared layer and will get enabled in CodeStyle NuGet package:

  1. Remove unused private members (IDE0051): An analyzer without options.
  2. C# make struct fields writable (IDE0064): Another simple analyzer without options.
  3. C# convert switch statement to expression (IDE0066): An analyzer with a C# code style option. Also includes refactoring of options infrastructure and test framework code to support option based analyzers and corresponding unit tests.
  4. C# use implicit or explicit type style analyzers (IDE0007 and IDE0008): Typical code style analyzers with bunch of utility types.
  5. Remove unnecessary usings/imports (IDE0005): Analyzer which used some language services, and is very commonly requested analyzer for CI enforcement.
    NOTE: This analyzer works on command line only if XML document comments are enabled for the project. This seems to be a compiler bug (Compiler does not report unused imports when XML doc comments are disabled #41640):
    internal override void ReportUnusedImports(SyntaxTree? filterTree, DiagnosticBag diagnostics, CancellationToken cancellationToken)
    {
    if (_lazyImportInfos != null && filterTree?.Options.DocumentationMode != DocumentationMode.None)

CodeStyleAnalyzers

#41462 tracks follow-up items once this PR goes in.

@sharwell
Copy link
Member

sharwell commented Feb 3, 2020

My main expectations for code layout in the new package is:

  1. No new shared projects. Use linked files if/when necessary since accomplish the same goal but are easier to maintain in the IDE.
  2. Tests for ported analyzers use the new analyzer testing library. This already works with .editorconfig with minimal configuration, as seen in FormattingAnalyzerTests.
  3. No OptionSet. The ported analyzers operate directly against AnalyzerConfigOptions. This already works with formatting analyzer as an example.
  4. MEF is not really an option for NuGet-installed analyzers. Even the code fixes are "exported", they are not constructed through MEF and don't have access to the export provider. The same limitations apply for refactorings.

@mavasani
Copy link
Contributor Author

mavasani commented Feb 3, 2020

No new shared projects. Use linked files if/when necessary since accomplish the same goal but are easier to maintain in the IDE.

Lets talk at the design meeting today. Using linked files makes this an extremely terrible experience for shared analyzers/fixers/test files, and forces each analyzer author to know to add their new analyzer/fixer/tests to both set of projects (Features + CodeStyle) and also now needs additional effort of understanding project structure in code style layer. All of this goes away with shared projects where you just add a file and it light ups wherever required.

Tests for ported analyzers use the new analyzer testing library. This already works with .editorconfig with minimal configuration, as seen in FormattingAnalyzerTests.

I think we really need to be practical here instead of idealistic. Forcing this requirement to port every analyzer/fi is putting tremendous extra work on initial porting effort and unnecessary delaying the overall port effort and customer value add. With my proposed change, porting each set of analyzer + fixer + tests can be done by simple file moves and some edits. Rewriting all tests to a different test framework should be done as a follow-up item so we don't end up again blocking this feature work, which has happened numerous times in the past because of our desire to be idealistic here.

No OptionSet. The ported analyzers operate directly against AnalyzerConfigOptions. This already works with formatting analyzer as an example.

Current ports use AnalyzerConfigOptions for analyzer/fixer in CodeStyle layer but still use the OptionSet for the implementation in Features layer to avoid any potential regressions. We can discuss this at the design meeting how we want to proceed here.

MEF is not really an option for NuGet-installed analyzers. Even the code fixes are "exported", they are not constructed through MEF and don't have access to the export provider. The same limitations apply for refactorings.

I don't understand this - the PR already refactors the MEF parts of code so the existing mechanism of document.GetLanguageService works in the code fixes in CodeStyle layer. I don't understand your concern here.

@CyrusNajmabadi
Copy link
Member

Somewhat concerning to see a port add 6000 new lines :-/ Is there a reason for that much addition?

@mavasani
Copy link
Contributor Author

mavasani commented Feb 3, 2020

Somewhat concerning to see a port add 6000 new lines

Almost all of those are coming from splitting our workspace layer extension method files into 2 separate files: workspace dependent and independent, with the latter being usable from analyzers. Unfortunately, GitHub sees this as new code addition, and also puts lot of merge pain on this branch :)

@CyrusNajmabadi
Copy link
Member

oh, this is a draft PR. bleagh. sorry.

in terms of the full PR, if we can make it simpler to review, that would be excellent. thanks! :)

@CyrusNajmabadi
Copy link
Member

Unfortunately, GitHub sees this as new code addition, and also puts lot of merge pain on this branch :)

Yeah. i would be ok with this going in as a two or three PR merge. specifically: one PR is just the real code changes. It would not be expected to pass CI. The other PR is just the loc changes. The third is the merge of the other two. It would pass, but wouldn't really need reviewing.

Note: i did the same when doing my regex PR as my log changes absolutely crippled github and made it so hard to review.

@mavasani mavasani removed the Blocked label Feb 12, 2020
@mavasani mavasani changed the title Refactor workspace utilities and port few IDE analyzers/fixers to CodeStyle layer Port few IDE analyzers/fixers to shared layer to be enabled in CodeStyle NuGet package Feb 12, 2020
@@ -92,11 +100,134 @@ public TestParameters WithIncludeDiagnosticsOutsideSelection(bool includeDiagnos
? TestWorkspace.Create(initialMarkup, openDocuments: false)
: CreateWorkspaceFromFile(initialMarkup, parameters);

// For CodeStyle layer testing, we create an .editorconfig at project root
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the primary test IDE analyzer/code fix test framework file that has been changed to enable supporting Code Style layer unit tests for CodeStyle analyzers/fixes.

@@ -199,138 +330,6 @@ protected Task TestSmartTagGlyphTagsAsync(string initialMarkup, ImmutableArray<s
}
}

protected async Task TestAddDocumentInRegularAndScriptAsync(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Split into a separate partial file which is not included in CodeStyle test utilities project.


namespace Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions
{
public abstract partial class AbstractCodeActionOrUserDiagnosticTest
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Existing code just moved into a separate partial declaration.

@@ -224,56 +227,5 @@ private void AssertNoAnalyzerExceptionDiagnostics(IEnumerable<Diagnostic> diagno
var analyzerExceptionDiagnostics = diagnostics.Where(diag => diag.Descriptor.CustomTags.Contains(WellKnownDiagnosticTags.AnalyzerException));
AssertEx.Empty(analyzerExceptionDiagnostics, "Found analyzer exception diagnostics");
}

#region Parentheses options
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved into a separate partial file which is not included in CodeStyle test utilities project (yet).

@@ -293,158 +292,5 @@ protected Document GetDocumentAndAnnotatedSpan(TestWorkspace workspace, out stri

Assert.True(expectedTextSpans.SetEquals(actualTextSpans));
}

internal async Task TestWithMockedGenerateTypeDialog(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved into a separate partial declaration.

mavasani added a commit to mavasani/roslyn that referenced this pull request Feb 12, 2020
@mavasani mavasani marked this pull request as ready for review February 12, 2020 23:53
@mavasani mavasani requested review from a team as code owners February 12, 2020 23:53
Copy link
Member

@333fred 333fred left a comment

Choose a reason for hiding this comment

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

Compiler.sln change lgtm. Did not look at other changes.

mavasani added a commit to mavasani/roslyn that referenced this pull request Feb 19, 2020
Extracted from dotnet#41363
Third follow-up item from dotnet#41462

Apart from adding resx files, changes also include:
1. Moving resource strings duplicated across Workspaces and CodeStyle layer into the shared resx files
2. Source file changes to use the resource strings from the shared resx.

We should no longer require use of `#if CODE_STYLE` in the shared layer for the purpose of resource strings.
@mavasani
Copy link
Contributor Author

Closing this big PR in favor of a smaller PR: #41815

@mavasani mavasani closed this Feb 20, 2020
mavasani added a commit to mavasani/roslyn that referenced this pull request Feb 20, 2020
Extracted out of dotnet#41363 so it only contains the following changes:

1. Port analyzer/fixer/test files for ConvertSwitchStatementToExpression to shared layer
2. Options related namespace changes (see comment dotnet#41363 (comment) for details)
3. MEF based language service discovery in CodeStyle layer. dotnet#41462 tracks replacing this with a non-MEF based approach.
4. Minimal resx/xlf file changes - few were needed as the resources used by analyzer/fixer were moved to shared resx file.
5. Enable .editorconfig based unit test support for CodeStyle layer. NOTE: First commit dotnet@b006324 just ports changes from dotnet#40513 which are required to enable this support. This commit should become redundant once that PR is merged in.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants