提交 8301d5e8 编写于 作者: T Tom Meschter

Apply binding redirects

When running the `AnalyzerDependencyChecker` we need to apply assembly
binding redirects while checking for missing assemblies. Otherwise we'll
report an error that, for example, `System.Collections.Immutable
Version=1.1.34` cannot be found when it has instead been redirected to
`System.Collections.Immutable Version=1.1.36`.
上级 ba589080
......@@ -20,14 +20,16 @@ internal sealed class AnalyzerDependencyChecker
{
private readonly HashSet<string> _analyzerFilePaths;
private readonly HashSet<AssemblyIdentity> _assemblyWhiteList;
private readonly IBindingRedirectionService _bindingRedirectionService;
public AnalyzerDependencyChecker(IEnumerable<string> analyzerFilePaths, IEnumerable<AssemblyIdentity> assemblyWhiteList)
public AnalyzerDependencyChecker(IEnumerable<string> analyzerFilePaths, IEnumerable<AssemblyIdentity> assemblyWhiteList, IBindingRedirectionService bindingRedirectionService = null)
{
Debug.Assert(analyzerFilePaths != null);
Debug.Assert(assemblyWhiteList != null);
_analyzerFilePaths = new HashSet<string>(analyzerFilePaths, StringComparer.OrdinalIgnoreCase);
_assemblyWhiteList = new HashSet<AssemblyIdentity>(assemblyWhiteList);
_bindingRedirectionService = bindingRedirectionService;
}
public AnalyzerDependencyResults Run(CancellationToken cancellationToken = default(CancellationToken))
......@@ -70,7 +72,11 @@ private ImmutableArray<MissingAnalyzerDependency> FindMissingDependencies(List<A
{
cancellationToken.ThrowIfCancellationRequested();
if (!_assemblyWhiteList.Contains(reference))
var redirectedReference = _bindingRedirectionService != null
? _bindingRedirectionService.ApplyBindingRedirects(reference)
: reference;
if (!_assemblyWhiteList.Contains(redirectedReference))
{
builder.Add(new MissingAnalyzerDependency(
analyzerInfo.Path,
......
......@@ -23,6 +23,7 @@ internal sealed class AnalyzerDependencyCheckingService
private readonly VisualStudioWorkspaceImpl _workspace;
private readonly HostDiagnosticUpdateSource _updateSource;
private readonly BindingRedirectionService _bindingRedirectionService;
private CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
private Task<AnalyzerDependencyResults> _task = Task.FromResult((AnalyzerDependencyResults)null);
......@@ -35,6 +36,7 @@ internal sealed class AnalyzerDependencyCheckingService
{
_workspace = workspace;
_updateSource = updateSource;
_bindingRedirectionService = new BindingRedirectionService();
}
public async void CheckForConflictsAsync()
......@@ -185,12 +187,29 @@ private Task<AnalyzerDependencyResults> GetConflictsAsync()
_task = _task.SafeContinueWith(_ =>
{
IEnumerable<AssemblyIdentity> loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies().Select(assembly => AssemblyIdentity.FromAssemblyDefinition(assembly));
return new AnalyzerDependencyChecker(currentAnalyzerPaths, loadedAssemblies).Run(_cancellationTokenSource.Token);
return new AnalyzerDependencyChecker(currentAnalyzerPaths, loadedAssemblies, _bindingRedirectionService).Run(_cancellationTokenSource.Token);
},
TaskScheduler.Default);
return _task;
}
private class BindingRedirectionService : IBindingRedirectionService
{
public AssemblyIdentity ApplyBindingRedirects(AssemblyIdentity originalIdentity)
{
string redirectedAssemblyName = AppDomain.CurrentDomain.ApplyPolicy(originalIdentity.ToString());
AssemblyIdentity redirectedAssemblyIdentity;
if (AssemblyIdentity.TryParseDisplayName(redirectedAssemblyName, out redirectedAssemblyIdentity))
{
return redirectedAssemblyIdentity;
}
return originalIdentity;
}
}
}
}
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
namespace Microsoft.VisualStudio.LanguageServices.Implementation
{
internal interface IBindingRedirectionService
{
AssemblyIdentity ApplyBindingRedirects(AssemblyIdentity originalIdentity);
}
}
......@@ -35,6 +35,7 @@
<Compile Include="Implementation\CompilationErrorTelemetry\CompilationErrorTelemetryIncrementalAnalyzer.cs" />
<Compile Include="Implementation\Diagnostics\VisualStudioVenusSpanMappingService.cs" />
<Compile Include="Implementation\EditAndContinue\Interop\NativeMethods.cs" />
<Compile Include="Implementation\IBindingRedirectionService.cs" />
<Compile Include="Implementation\LanguageService\AbstractLanguageService`2.IVsLanguageBlock.cs" />
<Compile Include="Implementation\Library\FindResults\TreeItems\AbstractSourceTreeItem.cs" />
<Compile Include="Implementation\Library\FindResults\TreeItems\MetadataDefinitionTreeItem.cs" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册