-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Add SDK analyzer assembly redirector #80969
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
Open
jjonescz
wants to merge
1
commit into
dotnet:main
Choose a base branch
from
jjonescz:analyzer-redirecting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+279
−0
Open
Changes from all commits
Commits
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
86 changes: 86 additions & 0 deletions
86
src/VisualStudio/CSharp/Test/ProjectSystemShim/SdkAnalyzerAssemblyRedirectorTests.cs
This file contains hidden or 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,86 @@ | ||
| // 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.Generic; | ||
| using System.IO; | ||
| using System.Text.Json; | ||
| using Microsoft.VisualStudio.LanguageServices.ProjectSystem; | ||
| using Roslyn.Test.Utilities; | ||
| using Xunit; | ||
|
|
||
| namespace Roslyn.VisualStudio.CSharp.UnitTests.ProjectSystemShim; | ||
|
|
||
| public sealed class SdkAnalyzerAssemblyRedirectorTests : TestBase | ||
| { | ||
| [Theory] | ||
| [InlineData("9.0.0-preview.5.24306.11", "9.0.0-preview.7.24406.2")] | ||
| [InlineData("9.0.0-preview.5.24306.11", "9.0.1-preview.7.24406.2")] | ||
| [InlineData("9.0.100", "9.0.0-preview.7.24406.2")] | ||
| [InlineData("9.0.100", "9.0.200")] | ||
| [InlineData("9.0.100", "9.0.101")] | ||
| public void SameMajorMinorVersion(string a, string b) | ||
| { | ||
| var testDir = Temp.CreateDirectory(); | ||
|
|
||
| var vsDir = Path.Combine(testDir.Path, "vs"); | ||
| Metadata(vsDir, new() { { "AspNetCoreAnalyzers", a } }); | ||
| var vsAnalyzerPath = FakeDll(vsDir, @$"AspNetCoreAnalyzers\analyzers\dotnet\cs", "Microsoft.AspNetCore.App.Analyzers"); | ||
| var sdkAnalyzerPath = FakeDll(testDir.Path, @$"sdk\packs\Microsoft.AspNetCore.App.Ref\{b}\analyzers\dotnet\cs", "Microsoft.AspNetCore.App.Analyzers"); | ||
|
|
||
| var resolver = new SdkAnalyzerAssemblyRedirector(vsDir); | ||
| var redirected = resolver.RedirectPath(sdkAnalyzerPath); | ||
| AssertEx.Equal(vsAnalyzerPath, redirected); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void DifferentPathSuffix() | ||
| { | ||
| var testDir = Temp.CreateDirectory(); | ||
|
|
||
| var vsDir = Path.Combine(testDir.Path, "vs"); | ||
| Metadata(vsDir, new() { { "AspNetCoreAnalyzers", "9.0.0-preview.5.24306.11" } }); | ||
| FakeDll(vsDir, @"AspNetCoreAnalyzers\analyzers\dotnet\cs", "Microsoft.AspNetCore.App.Analyzers"); | ||
| var sdkAnalyzerPath = FakeDll(testDir.Path, @"sdk\packs\Microsoft.AspNetCore.App.Ref\9.0.0-preview.7.24406.2\analyzers\dotnet\vb", "Microsoft.AspNetCore.App.Analyzers"); | ||
|
|
||
| var resolver = new SdkAnalyzerAssemblyRedirector(vsDir); | ||
| var redirected = resolver.RedirectPath(sdkAnalyzerPath); | ||
| Assert.Null(redirected); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("8.0.100", "9.0.0-preview.7.24406.2")] | ||
| [InlineData("9.1.100", "9.0.0-preview.7.24406.2")] | ||
| [InlineData("9.1.0-preview.5.24306.11", "9.0.0-preview.7.24406.2")] | ||
| [InlineData("9.0.100", "9.1.100")] | ||
| [InlineData("9.0.100", "10.0.100")] | ||
| [InlineData("9.9.100", "9.10.100")] | ||
| public void DifferentMajorMinorVersion(string a, string b) | ||
| { | ||
| var testDir = Temp.CreateDirectory(); | ||
|
|
||
| var vsDir = Path.Combine(testDir.Path, "vs"); | ||
| Metadata(vsDir, new() { { "AspNetCoreAnalyzers", a } }); | ||
| FakeDll(vsDir, @$"AspNetCoreAnalyzers\analyzers\dotnet\cs", "Microsoft.AspNetCore.App.Analyzers"); | ||
| var sdkAnalyzerPath = FakeDll(testDir.Path, @$"sdk\packs\Microsoft.AspNetCore.App.Ref\{b}\analyzers\dotnet\cs", "Microsoft.AspNetCore.App.Analyzers"); | ||
|
|
||
| var resolver = new SdkAnalyzerAssemblyRedirector(vsDir); | ||
| var redirected = resolver.RedirectPath(sdkAnalyzerPath); | ||
| Assert.Null(redirected); | ||
| } | ||
|
|
||
| private static string FakeDll(string root, string subdir, string name) | ||
| { | ||
| var dllPath = Path.Combine(root, subdir, $"{name}.dll"); | ||
| Directory.CreateDirectory(Path.GetDirectoryName(dllPath)); | ||
| File.WriteAllText(dllPath, ""); | ||
| return dllPath; | ||
| } | ||
|
|
||
| private static void Metadata(string root, Dictionary<string, string> versions) | ||
| { | ||
| var metadataFilePath = Path.Combine(root, "metadata.json"); | ||
| Directory.CreateDirectory(Path.GetDirectoryName(metadataFilePath)); | ||
| File.WriteAllText(metadataFilePath, JsonSerializer.Serialize(versions)); | ||
| } | ||
| } |
193 changes: 193 additions & 0 deletions
193
src/VisualStudio/Core/Def/ProjectSystem/SdkAnalyzerAssemblyRedirector.cs
This file contains hidden or 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,193 @@ | ||
| // 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.ComponentModel.Composition; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.IO; | ||
| using System.Text.Json; | ||
| using Microsoft.CodeAnalysis.Host.Mef; | ||
| using Microsoft.CodeAnalysis.Workspaces.AnalyzerRedirecting; | ||
| using Microsoft.VisualStudio.Shell; | ||
| using Microsoft.VisualStudio.Shell.Interop; | ||
|
|
||
| // Example: | ||
| // FullPath: "C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\8.0.8\analyzers\dotnet\System.Windows.Forms.Analyzers.dll" | ||
| // ProductVersion: "8.0.8" | ||
| // PathSuffix: "analyzers\dotnet" | ||
| using AnalyzerInfo = (string FullPath, string ProductVersion, string PathSuffix); | ||
|
|
||
| namespace Microsoft.VisualStudio.LanguageServices.ProjectSystem; | ||
|
|
||
| /// <summary> | ||
| /// See <see href="https://github.com/dotnet/sdk/blob/main/documentation/general/analyzer-redirecting.md"/>. | ||
| /// </summary> | ||
| [Export(typeof(IAnalyzerAssemblyRedirector))] | ||
| internal sealed class SdkAnalyzerAssemblyRedirector : IAnalyzerAssemblyRedirector | ||
| { | ||
| private readonly IVsActivityLog? _log; | ||
|
|
||
| private readonly bool _enabled; | ||
|
|
||
| private readonly string? _insertedAnalyzersDirectory; | ||
|
|
||
| /// <summary> | ||
| /// Map from analyzer assembly name (file name without extension) to a list of matching analyzers. | ||
| /// </summary> | ||
| private readonly ImmutableDictionary<string, List<AnalyzerInfo>> _analyzerMap; | ||
|
|
||
| [ImportingConstructor] | ||
| [Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] | ||
| public SdkAnalyzerAssemblyRedirector(SVsServiceProvider serviceProvider) : this( | ||
| Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"CommonExtensions\Microsoft\DotNet")), | ||
| serviceProvider.GetServiceOnMainThread<SVsActivityLog, IVsActivityLog>()) | ||
| { | ||
| } | ||
|
|
||
| // Internal for testing. | ||
| [SuppressMessage("RoslynDiagnosticsReliability", "RS0034: Exported parts should have a public constructor marked with 'ImportingConstructorAttribute'", | ||
| Justification = "This is an internal constructor exposed for testing and delegated to by the public importing constructor")] | ||
|
Member
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. If we're not actually using the MEF component for testing, the better pattern might be to break the stuff needed for testing out to a separate class and then test that directly. |
||
| internal SdkAnalyzerAssemblyRedirector(string? insertedAnalyzersDirectory, IVsActivityLog? log = null) | ||
| { | ||
| _log = log; | ||
| var enable = Environment.GetEnvironmentVariable("DOTNET_ANALYZER_REDIRECTING"); | ||
| _enabled = !"0".Equals(enable, StringComparison.OrdinalIgnoreCase) && !"false".Equals(enable, StringComparison.OrdinalIgnoreCase); | ||
| _insertedAnalyzersDirectory = insertedAnalyzersDirectory; | ||
| _analyzerMap = CreateAnalyzerMap(); | ||
| } | ||
|
|
||
| private ImmutableDictionary<string, List<AnalyzerInfo>> CreateAnalyzerMap() | ||
| { | ||
| if (!_enabled) | ||
| { | ||
| Log("Analyzer redirecting is disabled."); | ||
| return ImmutableDictionary<string, List<AnalyzerInfo>>.Empty; | ||
| } | ||
|
|
||
| var metadataFilePath = Path.Combine(_insertedAnalyzersDirectory, "metadata.json"); | ||
| if (!File.Exists(metadataFilePath)) | ||
| { | ||
| Log($"File does not exist: {metadataFilePath}"); | ||
| return ImmutableDictionary<string, List<AnalyzerInfo>>.Empty; | ||
| } | ||
|
|
||
| var versions = JsonSerializer.Deserialize<Dictionary<string, string>>(File.ReadAllText(metadataFilePath)); | ||
| if (versions is null || versions.Count == 0) | ||
| { | ||
| Log($"Versions are empty: {metadataFilePath}"); | ||
| return ImmutableDictionary<string, List<AnalyzerInfo>>.Empty; | ||
| } | ||
|
|
||
| var builder = ImmutableDictionary.CreateBuilder<string, List<AnalyzerInfo>>(StringComparer.OrdinalIgnoreCase); | ||
|
|
||
| // Expects layout like: | ||
| // VsInstallDir\DotNetRuntimeAnalyzers\WindowsDesktopAnalyzers\analyzers\dotnet\System.Windows.Forms.Analyzers.dll | ||
| // ~~~~~~~~~~~~~~~~~~~~~~~ = topLevelDirectory | ||
| // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ = analyzerPath | ||
|
|
||
| foreach (var topLevelDirectory in Directory.EnumerateDirectories(_insertedAnalyzersDirectory)) | ||
| { | ||
| foreach (var analyzerPath in Directory.EnumerateFiles(topLevelDirectory, "*.dll", SearchOption.AllDirectories)) | ||
| { | ||
| if (!analyzerPath.StartsWith(topLevelDirectory, StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| var subsetName = Path.GetFileName(topLevelDirectory); | ||
| if (!versions.TryGetValue(subsetName, out var version)) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| var analyzerName = Path.GetFileNameWithoutExtension(analyzerPath); | ||
| var pathSuffix = analyzerPath.Substring(topLevelDirectory.Length + 1 /* slash */); | ||
| pathSuffix = Path.GetDirectoryName(pathSuffix); | ||
|
|
||
| AnalyzerInfo analyzer = new() { FullPath = analyzerPath, ProductVersion = version, PathSuffix = pathSuffix }; | ||
|
|
||
| if (builder.TryGetValue(analyzerName, out var existing)) | ||
| { | ||
| existing.Add(analyzer); | ||
| } | ||
| else | ||
| { | ||
| builder.Add(analyzerName, [analyzer]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Log($"Loaded analyzer map ({builder.Count}): {_insertedAnalyzersDirectory}"); | ||
|
|
||
| return builder.ToImmutable(); | ||
| } | ||
|
|
||
| public string? RedirectPath(string fullPath) | ||
| { | ||
| if (_enabled && _analyzerMap.TryGetValue(Path.GetFileNameWithoutExtension(fullPath), out var analyzers)) | ||
| { | ||
| foreach (var analyzer in analyzers) | ||
| { | ||
| var directoryPath = Path.GetDirectoryName(fullPath); | ||
|
|
||
| // Note that both paths we compare here are normalized via netfx's Path.GetDirectoryName. | ||
| if (directoryPath.EndsWith(analyzer.PathSuffix, StringComparison.OrdinalIgnoreCase) && | ||
| MajorAndMinorVersionsMatch(directoryPath, analyzer.PathSuffix, analyzer.ProductVersion)) | ||
| { | ||
| return analyzer.FullPath; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
|
|
||
| static bool MajorAndMinorVersionsMatch(string directoryPath, string pathSuffix, string version) | ||
| { | ||
| // Find the version number in the directory path - it is in the directory name before the path suffix. | ||
| // Example: | ||
| // "C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\8.0.8\analyzers\dotnet\" = directoryPath | ||
| // ~~~~~~~~~~~~~~~~ = pathSuffix | ||
| // ~~~~~ = directoryPathVersion | ||
| // This can match also a NuGet package because the version number is at the same position: | ||
| // "C:\.nuget\packages\Microsoft.WindowsDesktop.App.Ref\8.0.8\analyzers\dotnet\" | ||
|
|
||
| var index = directoryPath.LastIndexOf(pathSuffix, StringComparison.OrdinalIgnoreCase); | ||
| if (index < 0) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| var directoryPathVersion = Path.GetFileName(Path.GetDirectoryName(directoryPath.Substring(0, index))); | ||
|
|
||
| return AreVersionMajorMinorPartEqual(directoryPathVersion, version); | ||
| } | ||
|
|
||
| static bool AreVersionMajorMinorPartEqual(string version1, string version2) | ||
| { | ||
| var firstDotIndex = version1.IndexOf('.'); | ||
| if (firstDotIndex < 0) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| var secondDotIndex = version1.IndexOf('.', firstDotIndex + 1); | ||
| if (secondDotIndex < 0) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return 0 == string.Compare(version1, 0, version2, 0, secondDotIndex, StringComparison.OrdinalIgnoreCase); | ||
| } | ||
| } | ||
|
|
||
| private void Log(string message) | ||
| { | ||
| _log?.LogEntry( | ||
| (uint)__ACTIVITYLOG_ENTRYTYPE.ALE_INFORMATION, | ||
| "Roslyn" + nameof(SdkAnalyzerAssemblyRedirector), | ||
| message); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Eek this is a deadlock waiting to happen. Since this isn't cross-repo anymore, can we adjust the interface to follow VS threading rules? Especially since this just appears for logging, we could easily fetch it asynchronously and write out the log once we get the service.
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.
What are those? Would using this overload from
RoslynServiceExtensionsbe correct?So I would need to create a producer/consumer pattern basically?