-
Notifications
You must be signed in to change notification settings - Fork 419
/
CSharpDiagnosticWorkerWithAnalyzers.cs
370 lines (315 loc) · 17 KB
/
CSharpDiagnosticWorkerWithAnalyzers.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Options;
using Microsoft.Extensions.Logging;
using OmniSharp.Helpers;
using OmniSharp.Models.Diagnostics;
using OmniSharp.Models.Events;
using OmniSharp.Options;
using OmniSharp.Roslyn.CSharp.Workers.Diagnostics;
using OmniSharp.Services;
namespace OmniSharp.Roslyn.CSharp.Services.Diagnostics
{
public class CSharpDiagnosticWorkerWithAnalyzers : ICsDiagnosticWorker, IDisposable
{
private readonly AnalyzerWorkQueue _workQueue;
private readonly ILogger<CSharpDiagnosticWorkerWithAnalyzers> _logger;
private readonly ConcurrentDictionary<DocumentId, DocumentDiagnostics> _currentDiagnosticResultLookup =
new ConcurrentDictionary<DocumentId, DocumentDiagnostics>();
private readonly ImmutableArray<ICodeActionProvider> _providers;
private readonly DiagnosticEventForwarder _forwarder;
private readonly OmniSharpOptions _options;
private readonly OmniSharpWorkspace _workspace;
// This is workaround.
// Currently roslyn doesn't expose official way to use IDE analyzers during analysis.
// This options gives certain IDE analysis access for services that are not yet publicly available.
private readonly ConstructorInfo _workspaceAnalyzerOptionsConstructor;
public CSharpDiagnosticWorkerWithAnalyzers(
OmniSharpWorkspace workspace,
[ImportMany] IEnumerable<ICodeActionProvider> providers,
ILoggerFactory loggerFactory,
DiagnosticEventForwarder forwarder,
OmniSharpOptions options)
{
_logger = loggerFactory.CreateLogger<CSharpDiagnosticWorkerWithAnalyzers>();
_providers = providers.ToImmutableArray();
_workQueue = new AnalyzerWorkQueue(loggerFactory, timeoutForPendingWorkMs: options.RoslynExtensionsOptions.DocumentAnalysisTimeoutMs * 3);
_forwarder = forwarder;
_options = options;
_workspace = workspace;
_workspaceAnalyzerOptionsConstructor = Assembly
.Load("Microsoft.CodeAnalysis.Features")
.GetType("Microsoft.CodeAnalysis.Diagnostics.WorkspaceAnalyzerOptions")
.GetConstructor(new Type[] { typeof(AnalyzerOptions), typeof(Solution) })
?? throw new InvalidOperationException("Could not resolve 'Microsoft.CodeAnalysis.Diagnostics.WorkspaceAnalyzerOptions' for IDE analyzers.");
_workspace.WorkspaceChanged += OnWorkspaceChanged;
_workspace.OnInitialized += OnWorkspaceInitialized;
Task.Factory.StartNew(() => Worker(AnalyzerWorkType.Foreground), TaskCreationOptions.LongRunning);
Task.Factory.StartNew(() => Worker(AnalyzerWorkType.Background), TaskCreationOptions.LongRunning);
OnWorkspaceInitialized(_workspace.Initialized);
}
public void OnWorkspaceInitialized(bool isInitialized)
{
if (isInitialized)
{
var documentIds = QueueDocumentsForDiagnostics();
_logger.LogInformation($"Solution initialized -> queue all documents for code analysis. Initial document count: {documentIds.Length}.");
}
}
public async Task<ImmutableArray<DocumentDiagnostics>> GetDiagnostics(ImmutableArray<string> documentPaths)
{
var documentIds = GetDocumentIdsFromPaths(documentPaths);
return await GetDiagnosticsByDocumentIds(documentIds, waitForDocuments: true);
}
private async Task<ImmutableArray<DocumentDiagnostics>> GetDiagnosticsByDocumentIds(ImmutableArray<DocumentId> documentIds, bool waitForDocuments)
{
if (waitForDocuments)
{
foreach (var documentId in documentIds)
{
_workQueue.TryPromote(documentId);
}
await _workQueue.WaitForegroundWorkComplete();
}
return documentIds
.Where(x => _currentDiagnosticResultLookup.ContainsKey(x))
.Select(x => _currentDiagnosticResultLookup[x])
.ToImmutableArray();
}
private ImmutableArray<DocumentId> GetDocumentIdsFromPaths(ImmutableArray<string> documentPaths)
{
return documentPaths
.Select(docPath => _workspace.GetDocumentId(docPath))
.Where(x => x != default)
.ToImmutableArray();
}
private async Task Worker(AnalyzerWorkType workType)
{
while (true)
{
try
{
var solution = _workspace.CurrentSolution;
var currentWorkGroupedByProjects = _workQueue
.TakeWork(workType)
.Select(documentId => (projectId: solution.GetDocument(documentId)?.Project?.Id, documentId))
.Where(x => x.projectId != null)
.GroupBy(x => x.projectId, x => x.documentId)
.ToImmutableArray();
foreach (var projectGroup in currentWorkGroupedByProjects)
{
var projectPath = solution.GetProject(projectGroup.Key).FilePath;
EventIfBackgroundWork(workType, projectPath, ProjectDiagnosticStatus.Started);
await AnalyzeProject(solution, projectGroup);
EventIfBackgroundWork(workType, projectPath, ProjectDiagnosticStatus.Ready);
}
_workQueue.WorkComplete(workType);
await Task.Delay(50);
}
catch (Exception ex)
{
_logger.LogError($"Analyzer worker failed: {ex}");
}
}
}
private void EventIfBackgroundWork(AnalyzerWorkType workType, string projectPath, ProjectDiagnosticStatus status)
{
if (workType == AnalyzerWorkType.Background)
_forwarder.ProjectAnalyzedInBackground(projectPath, status);
}
private void QueueForAnalysis(ImmutableArray<DocumentId> documentIds, AnalyzerWorkType workType)
{
_workQueue.PutWork(documentIds, workType);
}
private void OnWorkspaceChanged(object sender, WorkspaceChangeEventArgs changeEvent)
{
switch (changeEvent.Kind)
{
case WorkspaceChangeKind.DocumentChanged:
case WorkspaceChangeKind.DocumentAdded:
case WorkspaceChangeKind.DocumentReloaded:
case WorkspaceChangeKind.DocumentInfoChanged:
QueueForAnalysis(ImmutableArray.Create(changeEvent.DocumentId), AnalyzerWorkType.Foreground);
break;
case WorkspaceChangeKind.DocumentRemoved:
if (!_currentDiagnosticResultLookup.TryRemove(changeEvent.DocumentId, out _))
{
_logger.LogDebug($"Tried to remove non existent document from analysis, document: {changeEvent.DocumentId}");
}
break;
case WorkspaceChangeKind.AnalyzerConfigDocumentChanged:
_logger.LogDebug($"Analyzer config document {changeEvent.DocumentId} changed, which triggered re-analysis of project {changeEvent.ProjectId}.");
QueueForAnalysis(_workspace.CurrentSolution.GetProject(changeEvent.ProjectId).Documents.Select(x => x.Id).ToImmutableArray(), AnalyzerWorkType.Background);
break;
case WorkspaceChangeKind.ProjectAdded:
case WorkspaceChangeKind.ProjectChanged:
case WorkspaceChangeKind.ProjectReloaded:
_logger.LogDebug($"Project {changeEvent.ProjectId} updated, reanalyzing its diagnostics.");
QueueForAnalysis(_workspace.CurrentSolution.GetProject(changeEvent.ProjectId).Documents.Select(x => x.Id).ToImmutableArray(), AnalyzerWorkType.Background);
break;
case WorkspaceChangeKind.SolutionAdded:
case WorkspaceChangeKind.SolutionChanged:
case WorkspaceChangeKind.SolutionReloaded:
QueueDocumentsForDiagnostics();
break;
}
}
public async Task<IEnumerable<Diagnostic>> AnalyzeDocumentAsync(Document document, CancellationToken cancellationToken)
{
Project project = document.Project;
var allAnalyzers = _providers
.SelectMany(x => x.CodeDiagnosticAnalyzerProviders)
.Concat(project.AnalyzerReferences.SelectMany(x => x.GetAnalyzers(project.Language)))
.ToImmutableArray();
var compilation = await project.GetCompilationAsync();
var workspaceAnalyzerOptions = (AnalyzerOptions)_workspaceAnalyzerOptionsConstructor.Invoke(new object[] { project.AnalyzerOptions, project.Solution });
cancellationToken.ThrowIfCancellationRequested();
return await AnalyzeDocument(project, allAnalyzers, compilation, workspaceAnalyzerOptions, document);
}
public async Task<IEnumerable<Diagnostic>> AnalyzeProjectsAsync(Project project, CancellationToken cancellationToken)
{
var diagnostics = new List<Diagnostic>();
var allAnalyzers = _providers
.SelectMany(x => x.CodeDiagnosticAnalyzerProviders)
.Concat(project.AnalyzerReferences.SelectMany(x => x.GetAnalyzers(project.Language)))
.ToImmutableArray();
var compilation = await project.GetCompilationAsync();
var workspaceAnalyzerOptions = (AnalyzerOptions)_workspaceAnalyzerOptionsConstructor.Invoke(new object[] { project.AnalyzerOptions, project.Solution });
foreach (var document in project.Documents)
{
cancellationToken.ThrowIfCancellationRequested();
diagnostics.AddRange(await AnalyzeDocument(project, allAnalyzers, compilation, workspaceAnalyzerOptions, document));
}
return diagnostics;
}
private async Task AnalyzeProject(Solution solution, IGrouping<ProjectId, DocumentId> documentsGroupedByProject)
{
try
{
var project = solution.GetProject(documentsGroupedByProject.Key);
var allAnalyzers = _providers
.SelectMany(x => x.CodeDiagnosticAnalyzerProviders)
.Concat(project.AnalyzerReferences.SelectMany(x => x.GetAnalyzers(project.Language)))
.ToImmutableArray();
var compilation = await project.GetCompilationAsync();
var workspaceAnalyzerOptions = (AnalyzerOptions)_workspaceAnalyzerOptionsConstructor.Invoke(new object[] { project.AnalyzerOptions, project.Solution });
foreach (var documentId in documentsGroupedByProject)
{
var document = project.GetDocument(documentId);
var diagnostics = await AnalyzeDocument(project, allAnalyzers, compilation, workspaceAnalyzerOptions, document);
UpdateCurrentDiagnostics(project, document, diagnostics);
}
}
catch (Exception ex)
{
_logger.LogError($"Analysis of project {documentsGroupedByProject.Key} failed, underlaying error: {ex}");
}
}
private async Task<ImmutableArray<Diagnostic>> AnalyzeDocument(Project project, ImmutableArray<DiagnosticAnalyzer> allAnalyzers, Compilation compilation, AnalyzerOptions workspaceAnalyzerOptions, Document document)
{
try
{
// There's real possibility that bug in analyzer causes analysis hang at document.
var perDocumentTimeout =
new CancellationTokenSource(_options.RoslynExtensionsOptions.DocumentAnalysisTimeoutMs);
var documentSemanticModel = await document.GetSemanticModelAsync(perDocumentTimeout.Token);
var diagnostics = ImmutableArray<Diagnostic>.Empty;
// Only basic syntax check is available if file is miscellanous like orphan .cs file.
// Those projects are on hard coded virtual project
if (project.Name == $"{Configuration.OmniSharpMiscProjectName}.csproj")
{
var syntaxTree = await document.GetSyntaxTreeAsync();
diagnostics = syntaxTree.GetDiagnostics().ToImmutableArray();
}
else if (allAnalyzers.Any()) // Analyzers cannot be called with empty analyzer list.
{
var compilationWithAnalyzers = compilation.WithAnalyzers(allAnalyzers, new CompilationWithAnalyzersOptions(
workspaceAnalyzerOptions,
onAnalyzerException: OnAnalyzerException,
concurrentAnalysis: false,
logAnalyzerExecutionTime: false,
reportSuppressedDiagnostics: false));
var semanticDiagnosticsWithAnalyzers = await compilationWithAnalyzers
.GetAnalyzerSemanticDiagnosticsAsync(documentSemanticModel, filterSpan: null, perDocumentTimeout.Token);
var syntaxDiagnosticsWithAnalyzers = await compilationWithAnalyzers
.GetAnalyzerSyntaxDiagnosticsAsync(documentSemanticModel.SyntaxTree, perDocumentTimeout.Token);
diagnostics = semanticDiagnosticsWithAnalyzers
.Concat(syntaxDiagnosticsWithAnalyzers)
.Where(d => !d.IsSuppressed)
.Concat(documentSemanticModel.GetDiagnostics())
.ToImmutableArray();
}
else
{
diagnostics = documentSemanticModel.GetDiagnostics();
}
return diagnostics;
}
catch (Exception ex)
{
_logger.LogError($"Analysis of document {document.Name} failed or cancelled by timeout: {ex.Message}, analysers: {string.Join(", ", allAnalyzers)}");
return ImmutableArray<Diagnostic>.Empty;
}
}
private void OnAnalyzerException(Exception ex, DiagnosticAnalyzer analyzer, Diagnostic diagnostic)
{
_logger.LogDebug($"Exception in diagnostic analyzer." +
$"\n analyzer: {analyzer}" +
$"\n diagnostic: {diagnostic}" +
$"\n exception: {ex.Message}");
}
private void UpdateCurrentDiagnostics(Project project, Document document, ImmutableArray<Diagnostic> diagnosticsWithAnalyzers)
{
_currentDiagnosticResultLookup[document.Id] = new DocumentDiagnostics(document.Id, document.FilePath, project.Id, project.Name, diagnosticsWithAnalyzers);
EmitDiagnostics(_currentDiagnosticResultLookup[document.Id]);
}
private void EmitDiagnostics(DocumentDiagnostics results)
{
_forwarder.Forward(new DiagnosticMessage
{
Results = new[]
{
new DiagnosticResult
{
FileName = results.DocumentPath, QuickFixes = results.Diagnostics
.Select(x => x.ToDiagnosticLocation())
.ToList()
}
}
});
}
public ImmutableArray<DocumentId> QueueDocumentsForDiagnostics()
{
var documentIds = _workspace.CurrentSolution.Projects.SelectMany(x => x.DocumentIds).ToImmutableArray();
QueueForAnalysis(documentIds, AnalyzerWorkType.Background);
return documentIds;
}
public async Task<ImmutableArray<DocumentDiagnostics>> GetAllDiagnosticsAsync()
{
var allDocumentsIds = _workspace.CurrentSolution.Projects.SelectMany(x => x.DocumentIds).ToImmutableArray();
return await GetDiagnosticsByDocumentIds(allDocumentsIds, waitForDocuments: false);
}
public ImmutableArray<DocumentId> QueueDocumentsForDiagnostics(ImmutableArray<ProjectId> projectIds)
{
var documentIds = projectIds
.SelectMany(projectId => _workspace.CurrentSolution.GetProject(projectId).Documents.Select(x => x.Id))
.ToImmutableArray();
QueueForAnalysis(documentIds, AnalyzerWorkType.Background);
return documentIds;
}
public void Dispose()
{
_workspace.WorkspaceChanged -= OnWorkspaceChanged;
_workspace.OnInitialized -= OnWorkspaceInitialized;
}
}
}