Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
using Microsoft.CodeAnalysis.LanguageServer.Handler.Diagnostics;
using Microsoft.CodeAnalysis.SolutionCrawler;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Test.Utilities;
using Roslyn.Test.Utilities.TestGenerators;
using Xunit;
using Xunit.Abstractions;
using LSP = Roslyn.LanguageServer.Protocol;
Expand Down Expand Up @@ -151,6 +153,42 @@ public async Task TestWorkspaceDiagnosticsWithAdditionalFileInMultipleProjects(b
AssertEx.Empty(results2);
}

[Theory, CombinatorialData]
public async Task TestWorkspaceDiagnosticsReportsSourceGeneratorDiagnosticInAdditionalFile(bool useVSDiagnostics, bool mutatingLspWorkspace)
{
var additionaFilePath = @"C:\File.razor";
var workspaceXml =
$"""
<Workspace>
<Project Language="C#" CommonReferences="true" AssemblyName="CSProj1" FilePath="C:\CSProj1.csproj">
<Document FilePath="C:\C.cs"></Document>
<AdditionalDocument FilePath="{additionaFilePath}">Hello</AdditionalDocument>
</Project>
</Workspace>
""";

await using var testLspServer = await CreateTestWorkspaceFromXmlAsync(workspaceXml, mutatingLspWorkspace, BackgroundAnalysisScope.FullSolution, useVSDiagnostics);

// Add a generator to the solution that reports a source generator diagnostic in an additional file.
var generator = new DiagnosticProducingGenerator(context =>
{
return Location.Create(additionaFilePath, TextSpan.FromBounds(0, 1), new LinePositionSpan(new LinePosition(0, 0), new LinePosition(0, 1)));
});

testLspServer.TestWorkspace.OnAnalyzerReferenceAdded(
testLspServer.GetCurrentSolution().Projects.Single().Id,
new TestGeneratorReference(generator));
await testLspServer.WaitForSourceGeneratorsAsync();

var results = await RunGetWorkspacePullDiagnosticsAsync(testLspServer, useVSDiagnostics);
AssertEx.Equal(
[
@"C:\C.cs: []",
@$"C:\File.razor: [{DiagnosticProducingGenerator.Descriptor.Id}, {MockAdditionalFileDiagnosticAnalyzer.Id}]",
@"C:\CSProj1.csproj: []"
], results.Select(r => $"{r.Uri.GetRequiredParsedUri().LocalPath}: [{string.Join(", ", r.Diagnostics!.Select(d => d.Code?.Value?.ToString()))}]"));
}

protected override TestComposition Composition => base.Composition.AddParts(typeof(MockAdditionalFileDiagnosticAnalyzer), typeof(TestAdditionalFileDocumentSourceProvider));

private protected override TestAnalyzerReferenceByLanguage CreateTestAnalyzersReference()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ public void AddExternalSemanticDiagnostics(DocumentId documentId, ImmutableArray
if (diagnostics.Length == 0)
return;

// this is for diagnostic producer that doesnt use compiler based DiagnosticAnalyzer such as TypeScript.
Contract.ThrowIfTrue(Project.SupportsCompilation);

AddExternalDiagnostics(ref _lazySemanticLocals, documentId, diagnostics);
}

Expand Down
Loading