提交 57c81319 编写于 作者: K Kevin Pilch 提交者: GitHub

Merge pull request #20950 from Pilchie/Fix466282-UnresolvedAnalyzerReferences

Handle serializing unresolved analyzer references
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Execution;
using Microsoft.CodeAnalysis.Serialization;
using Microsoft.CodeAnalysis.UnitTests;
using Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem;
using Microsoft.VisualStudio.Shell.Interop;
using Moq;
using Roslyn.Test.Utilities;
using Roslyn.Utilities;
using Xunit;
namespace Roslyn.VisualStudio.Next.UnitTests.Services
{
public class VisualStudioSnapshotSerializationTests : SnapshotSerializationTestBase
{
[Fact, WorkItem(466282, "https://devdiv.visualstudio.com/DevDiv/_workitems/edit/466282")]
public async Task TestUnresolvedAnalyzerReference()
{
var workspace = new AdhocWorkspace();
var project = workspace.CurrentSolution.AddProject("empty", "empty", LanguageNames.CSharp);
var mockFileChangeService = new Mock<IVsFileChangeEx>();
using (var analyzer = new VisualStudioAnalyzer(
@"PathToAnalyzer",
fileChangeService: mockFileChangeService.Object,
hostDiagnosticUpdateSource: null,
projectId: project.Id,
workspace: workspace,
loader: null,
language: project.Language))
{
var analyzerReference = analyzer.GetReference();
project = project.WithAnalyzerReferences(new AnalyzerReference[]
{
analyzerReference,
});
var checksum = await project.State.GetChecksumAsync(CancellationToken.None).ConfigureAwait(false);
Assert.NotNull(checksum);
var assetBuilder = new CustomAssetBuilder(workspace);
var serializer = new Serializer(workspace);
var asset = assetBuilder.Build(analyzerReference, CancellationToken.None);
using (var stream = SerializableBytes.CreateWritableStream())
using (var writer = new ObjectWriter(stream))
{
await asset.WriteObjectToAsync(writer, CancellationToken.None).ConfigureAwait(false);
stream.Position = 0;
using (var reader = ObjectReader.TryGetReader(stream))
{
var recovered = serializer.Deserialize<AnalyzerReference>(asset.Kind, reader, CancellationToken.None);
var assetFromStorage = assetBuilder.Build(recovered, CancellationToken.None);
Assert.Equal(asset.Checksum, assetFromStorage.Checksum);
// This won't round trip, but we should get an UnresolvedAnalyzerReference, with the same path
Assert.Equal(analyzerReference.FullPath, recovered.FullPath);
}
}
}
}
}
}
......@@ -259,6 +259,7 @@
<Compile Include="Mocks\TestOptionSet.cs" />
<Compile Include="Remote\JsonConverterTests.cs" />
<Compile Include="Remote\RemoteHostClientServiceFactoryTests.cs" />
<Compile Include="Services\VisualStudioSnapshotSerializationTests.cs" />
<Compile Include="Services\SolutionServiceTests.cs" />
<Compile Include="Services\AssetStorageTests.cs" />
<Compile Include="Services\AssetServiceTests.cs" />
......
......@@ -21,6 +21,7 @@ namespace Microsoft.CodeAnalysis.Execution
internal abstract class AbstractReferenceSerializationService : IReferenceSerializationService
{
private const int MetadataFailed = int.MaxValue;
private const string VisualStudioUnresolvedAnalyzerReference = "Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem.VisualStudioAnalyzer+VisualStudioUnresolvedAnalyzerReference";
protected const byte NoEncodingSerialization = 0;
protected const byte EncodingSerialization = 1;
......@@ -101,13 +102,17 @@ public Checksum CreateChecksum(AnalyzerReference reference, CancellationToken ca
WriteUnresolvedAnalyzerReferenceTo(unresolved, writer);
break;
case AnalyzerReference analyzerReference when analyzerReference.GetType().FullName == VisualStudioUnresolvedAnalyzerReference:
WriteUnresolvedAnalyzerReferenceTo(analyzerReference, writer);
break;
case AnalyzerImageReference _:
// TODO: think a way to support this or a way to deal with this kind of situation.
// https://github.com/dotnet/roslyn/issues/15783
throw new NotSupportedException(nameof(AnalyzerImageReference));
default:
throw ExceptionUtilities.UnexpectedValue(reference.GetType());
throw ExceptionUtilities.UnexpectedValue(reference);
}
stream.Position = 0;
......@@ -184,6 +189,12 @@ public void WriteTo(AnalyzerReference reference, ObjectWriter writer, bool usePa
return;
}
case AnalyzerReference analyzerReference when analyzerReference.GetType().FullName == VisualStudioUnresolvedAnalyzerReference:
{
WriteUnresolvedAnalyzerReferenceTo(analyzerReference, writer);
return;
}
case AnalyzerImageReference _:
{
// TODO: think a way to support this or a way to deal with this kind of situation.
......@@ -192,7 +203,7 @@ public void WriteTo(AnalyzerReference reference, ObjectWriter writer, bool usePa
}
default:
throw ExceptionUtilities.UnexpectedValue(reference.GetType());
throw ExceptionUtilities.UnexpectedValue(reference);
}
}
......
......@@ -72,9 +72,7 @@
<Compile Include="DependentTypeFinderTests.cs" />
<Compile Include="Differencing\LongestCommonSubsequenceTests.cs" />
<Compile Include="Editting\SyntaxEditorTests.cs" />
<Compile Include="Execution\Extensions.cs" />
<Compile Include="EditorConfigStorageLocation\EditorConfigStorageLocationTests.cs" />
<Compile Include="Execution\SnapshotSerializationTestBase.cs" />
<Compile Include="Execution\SnapshotSerializationTests.cs" />
<Compile Include="ExtensionOrdererTests.cs" />
<Compile Include="Host\WorkspaceServices\TestOptionsServiceFactory.cs" />
......
......@@ -58,8 +58,10 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Compile Include="Extensions.cs" />
<Compile Include="FileSet.cs" />
<Compile Include="Formatting\FormattingTestBase.cs" />
<Compile Include="SnapshotSerializationTestBase.cs" />
<Compile Include="SolutionTestUtilities.cs" />
<Compile Include="SolutionUtilities.cs" />
<EmbeddedResource Include="TestFiles\CSharpProject_App.xaml.cs">
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册