未验证 提交 146f9208 编写于 作者: T Tomáš Matoušek 提交者: GitHub

Temporarily allow duplicate analyzer references (#43567)

上级 52a56c4d
......@@ -231,7 +231,7 @@ public sealed class ProjectInfo
PublicContract.ToBoxedImmutableArrayWithDistinctNonNullItems(documents, nameof(documents)),
PublicContract.ToBoxedImmutableArrayWithDistinctNonNullItems(projectReferences, nameof(projectReferences)),
PublicContract.ToBoxedImmutableArrayWithDistinctNonNullItems(metadataReferences, nameof(metadataReferences)),
PublicContract.ToBoxedImmutableArrayWithDistinctNonNullItems(analyzerReferences, nameof(analyzerReferences)),
PublicContract.ToBoxedImmutableArrayWithNonNullItems(analyzerReferences, nameof(analyzerReferences)),
PublicContract.ToBoxedImmutableArrayWithDistinctNonNullItems(additionalDocuments, nameof(additionalDocuments)),
analyzerConfigDocuments: SpecializedCollections.EmptyBoxedImmutableArray<DocumentInfo>(),
hostObjectType);
......@@ -336,7 +336,7 @@ public ProjectInfo WithMetadataReferences(IEnumerable<MetadataReference>? metada
=> With(metadataReferences: PublicContract.ToBoxedImmutableArrayWithDistinctNonNullItems(metadataReferences, nameof(metadataReferences)));
public ProjectInfo WithAnalyzerReferences(IEnumerable<AnalyzerReference>? analyzerReferences)
=> With(analyzerReferences: PublicContract.ToBoxedImmutableArrayWithDistinctNonNullItems(analyzerReferences, nameof(analyzerReferences)));
=> With(analyzerReferences: PublicContract.ToBoxedImmutableArrayWithNonNullItems(analyzerReferences, nameof(analyzerReferences)));
internal string GetDebuggerDisplay()
=> nameof(ProjectInfo) + " " + Name + (!string.IsNullOrWhiteSpace(FilePath) ? " " + FilePath : "");
......
......@@ -802,7 +802,7 @@ public Solution WithProjectAnalyzerReferences(ProjectId projectId, IEnumerable<A
var newState = _state.WithProjectAnalyzerReferences(
projectId,
PublicContract.ToBoxedImmutableArrayWithDistinctNonNullItems(analyzerReferences, nameof(analyzerReferences)));
PublicContract.ToBoxedImmutableArrayWithNonNullItems(analyzerReferences, nameof(analyzerReferences)));
if (newState == _state)
{
......
......@@ -21,6 +21,7 @@
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.UnitTests.Execution;
using Roslyn.Test.Utilities;
using Roslyn.Utilities;
using Xunit;
......@@ -501,6 +502,45 @@ public async Task SnapshotWithMissingReferencesTest()
var recovered = await GetSolutionAsync(snapshotService, snapshot).ConfigureAwait(false);
}
[Fact]
public async Task SnapshotWithIdenticalAnalyzerFiles()
{
var hostServices = MefHostServices.Create(
MefHostServices.DefaultAssemblies.Add(typeof(Host.TemporaryStorageServiceFactory.TemporaryStorageService).Assembly));
var project = new AdhocWorkspace(hostServices).CurrentSolution.AddProject("Project", "Project.dll", LanguageNames.CSharp);
using var temp = new TempRoot();
var dir = temp.CreateDirectory();
// create two analyzer assembly files whose content is identical but path is different:
var file1 = dir.CreateFile("analyzer1.dll").WriteAllBytes(TestResources.AnalyzerTests.FaultyAnalyzer);
var file2 = dir.CreateFile("analyzer2.dll").WriteAllBytes(TestResources.AnalyzerTests.FaultyAnalyzer);
var analyzer1 = new AnalyzerFileReference(file1.Path, DummyAssemblyLoader.Instance);
var analyzer2 = new AnalyzerFileReference(file2.Path, DummyAssemblyLoader.Instance);
project = project.AddAnalyzerReferences(new[] { analyzer1, analyzer2 });
var snapshotService = (IRemotableDataService)new RemotableDataServiceFactory().CreateService(project.Solution.Workspace.Services);
using var snapshot = await snapshotService.CreatePinnedRemotableDataScopeAsync(project.Solution, CancellationToken.None).ConfigureAwait(false);
var recovered = await GetSolutionAsync(snapshotService, snapshot).ConfigureAwait(false);
AssertEx.Equal(new[] { file1.Path, file1.Path }, recovered.GetProject(project.Id).AnalyzerReferences.Select(r => r.FullPath));
}
internal class DummyAssemblyLoader : IAnalyzerAssemblyLoader
{
public static DummyAssemblyLoader Instance = new DummyAssemblyLoader();
public void AddDependencyLocation(string fullPath)
{
}
public Assembly LoadFromPath(string fullPath)
=> Assembly.LoadFrom(fullPath);
}
[Fact]
public async Task UnknownLanguageTest()
{
......
......@@ -53,8 +53,7 @@ public void Create_Errors_DuplicateItems()
analyzerReferences: new AnalyzerReference[] { null }));
var analyzerReference = new TestAnalyzerReference();
Assert.Throws<ArgumentException>("analyzerReferences[1]",
() => ProjectInfo.Create(pid, VersionStamp.Default, "proj", "assembly", "C#", analyzerReferences: new[] { analyzerReference, analyzerReference }));
ProjectInfo.Create(pid, VersionStamp.Default, "proj", "assembly", "C#", analyzerReferences: new[] { analyzerReference, analyzerReference });
Assert.Throws<ArgumentNullException>(() => ProjectInfo.Create(pid, VersionStamp.Default, name: "Goo", assemblyName: "Bar", language: "C#",
metadataReferences: new MetadataReference[] { null }));
......@@ -193,7 +192,7 @@ public void TestProperties()
SolutionTestHelpers.TestListProperty(instance, (old, value) => old.WithDocuments(value), opt => opt.Documents, documentInfo, allowDuplicates: false);
SolutionTestHelpers.TestListProperty(instance, (old, value) => old.WithAdditionalDocuments(value), opt => opt.AdditionalDocuments, documentInfo, allowDuplicates: false);
SolutionTestHelpers.TestListProperty(instance, (old, value) => old.WithAnalyzerConfigDocuments(value), opt => opt.AnalyzerConfigDocuments, documentInfo, allowDuplicates: false);
SolutionTestHelpers.TestListProperty(instance, (old, value) => old.WithAnalyzerReferences(value), opt => opt.AnalyzerReferences, (AnalyzerReference)new TestAnalyzerReference(), allowDuplicates: false);
SolutionTestHelpers.TestListProperty(instance, (old, value) => old.WithAnalyzerReferences(value), opt => opt.AnalyzerReferences, (AnalyzerReference)new TestAnalyzerReference(), allowDuplicates: true);
SolutionTestHelpers.TestListProperty(instance, (old, value) => old.WithMetadataReferences(value), opt => opt.MetadataReferences, (MetadataReference)new TestMetadataReference(), allowDuplicates: false);
SolutionTestHelpers.TestListProperty(instance, (old, value) => old.WithProjectReferences(value), opt => opt.ProjectReferences, new ProjectReference(projectId), allowDuplicates: false);
}
......
......@@ -897,7 +897,7 @@ public void WithProjectAnalyzerReferences()
(old, value) => old.WithProjectAnalyzerReferences(projectId, value),
opt => opt.GetProject(projectId)!.AnalyzerReferences,
analyzerRef,
allowDuplicates: false);
allowDuplicates: true);
Assert.Throws<ArgumentNullException>("projectId", () => solution.WithProjectAnalyzerReferences(null!, new[] { analyzerRef }));
Assert.Throws<InvalidOperationException>(() => solution.WithProjectAnalyzerReferences(ProjectId.CreateNewId(), new[] { analyzerRef }));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册