|
15 | 15 | using Microsoft.CodeAnalysis.LanguageServer.Handler.Diagnostics; |
16 | 16 | using Microsoft.CodeAnalysis.SolutionCrawler; |
17 | 17 | using Microsoft.CodeAnalysis.Test.Utilities; |
| 18 | +using Microsoft.CodeAnalysis.Text; |
18 | 19 | using Roslyn.Test.Utilities; |
| 20 | +using Roslyn.Test.Utilities.TestGenerators; |
19 | 21 | using Xunit; |
20 | 22 | using Xunit.Abstractions; |
21 | 23 | using LSP = Roslyn.LanguageServer.Protocol; |
@@ -151,6 +153,42 @@ public async Task TestWorkspaceDiagnosticsWithAdditionalFileInMultipleProjects(b |
151 | 153 | AssertEx.Empty(results2); |
152 | 154 | } |
153 | 155 |
|
| 156 | + [Theory, CombinatorialData] |
| 157 | + public async Task TestWorkspaceDiagnosticsReportsSourceGeneratorDiagnosticInAdditionalFile(bool useVSDiagnostics, bool mutatingLspWorkspace) |
| 158 | + { |
| 159 | + var additionaFilePath = @"C:\File.razor"; |
| 160 | + var workspaceXml = |
| 161 | + $""" |
| 162 | + <Workspace> |
| 163 | + <Project Language="C#" CommonReferences="true" AssemblyName="CSProj1" FilePath="C:\CSProj1.csproj"> |
| 164 | + <Document FilePath="C:\C.cs"></Document> |
| 165 | + <AdditionalDocument FilePath="{additionaFilePath}">Hello</AdditionalDocument> |
| 166 | + </Project> |
| 167 | + </Workspace> |
| 168 | + """; |
| 169 | + |
| 170 | + await using var testLspServer = await CreateTestWorkspaceFromXmlAsync(workspaceXml, mutatingLspWorkspace, BackgroundAnalysisScope.FullSolution, useVSDiagnostics); |
| 171 | + |
| 172 | + // Add a generator to the solution that reports a source generator diagnostic in an additional file. |
| 173 | + var generator = new DiagnosticProducingGenerator(context => |
| 174 | + { |
| 175 | + return Location.Create(additionaFilePath, TextSpan.FromBounds(0, 1), new LinePositionSpan(new LinePosition(0, 0), new LinePosition(0, 1))); |
| 176 | + }); |
| 177 | + |
| 178 | + testLspServer.TestWorkspace.OnAnalyzerReferenceAdded( |
| 179 | + testLspServer.GetCurrentSolution().Projects.Single().Id, |
| 180 | + new TestGeneratorReference(generator)); |
| 181 | + await testLspServer.WaitForSourceGeneratorsAsync(); |
| 182 | + |
| 183 | + var results = await RunGetWorkspacePullDiagnosticsAsync(testLspServer, useVSDiagnostics); |
| 184 | + AssertEx.Equal( |
| 185 | + [ |
| 186 | + @"C:\C.cs: []", |
| 187 | + @$"C:\File.razor: [{DiagnosticProducingGenerator.Descriptor.Id}, {MockAdditionalFileDiagnosticAnalyzer.Id}]", |
| 188 | + @"C:\CSProj1.csproj: []" |
| 189 | + ], results.Select(r => $"{r.Uri.GetRequiredParsedUri().LocalPath}: [{string.Join(", ", r.Diagnostics!.Select(d => d.Code?.Value?.ToString()))}]")); |
| 190 | + } |
| 191 | + |
154 | 192 | protected override TestComposition Composition => base.Composition.AddParts(typeof(MockAdditionalFileDiagnosticAnalyzer), typeof(TestAdditionalFileDocumentSourceProvider)); |
155 | 193 |
|
156 | 194 | private protected override TestAnalyzerReferenceByLanguage CreateTestAnalyzersReference() |
|
0 commit comments