-
Notifications
You must be signed in to change notification settings - Fork 4k
/
VisualStudioProject.cs
1274 lines (1073 loc) · 56.9 KB
/
VisualStudioProject.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Collections;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Internal.Log;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Telemetry;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServices.Implementation.TaskList;
using Roslyn.Utilities;
namespace Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem
{
internal sealed partial class VisualStudioProject
{
private static readonly ImmutableArray<MetadataReferenceProperties> s_defaultMetadataReferenceProperties = ImmutableArray.Create(default(MetadataReferenceProperties));
private readonly VisualStudioWorkspaceImpl _workspace;
private readonly HostDiagnosticUpdateSource _hostDiagnosticUpdateSource;
/// <summary>
/// Provides dynamic source files for files added through <see cref="AddDynamicSourceFile" />.
/// </summary>
private readonly ImmutableArray<Lazy<IDynamicFileInfoProvider, FileExtensionsMetadata>> _dynamicFileInfoProviders;
/// <summary>
/// A semaphore taken for all mutation of any mutable field in this type.
/// </summary>
/// <remarks>This is, for now, intentionally pessimistic. There are no doubt ways that we could allow more to run in parallel,
/// but the current tradeoff is for simplicity of code and "obvious correctness" than something that is subtle, fast, and wrong.</remarks>
private readonly SemaphoreSlim _gate = new SemaphoreSlim(initialCount: 1);
/// <summary>
/// The number of active batch scopes. If this is zero, we are not batching, non-zero means we are batching.
/// </summary>
private int _activeBatchScopes = 0;
private readonly List<(string path, MetadataReferenceProperties properties)> _metadataReferencesAddedInBatch = new();
private readonly List<(string path, MetadataReferenceProperties properties)> _metadataReferencesRemovedInBatch = new();
private readonly List<ProjectReference> _projectReferencesAddedInBatch = new();
private readonly List<ProjectReference> _projectReferencesRemovedInBatch = new();
private readonly Dictionary<string, VisualStudioAnalyzer> _analyzerPathsToAnalyzers = new();
private readonly List<VisualStudioAnalyzer> _analyzersAddedInBatch = new();
/// <summary>
/// The list of <see cref="VisualStudioAnalyzer"/> that will be removed in this batch. They have not yet
/// been disposed, and will be disposed once the batch is applied.
/// </summary>
private readonly List<VisualStudioAnalyzer> _analyzersRemovedInBatch = new();
private readonly List<Action<SolutionChangeAccumulator>> _projectPropertyModificationsInBatch = new();
private string _assemblyName;
private string _displayName;
private string? _filePath;
private CompilationOptions? _compilationOptions;
private ParseOptions? _parseOptions;
private bool _hasAllInformation = true;
private string? _compilationOutputAssemblyFilePath;
private string? _outputFilePath;
private string? _outputRefFilePath;
private string? _defaultNamespace;
/// <summary>
/// If this project is the 'primary' project the project system cares about for a group of Roslyn projects that
/// correspond to different configurations of a single project system project. <see langword="true"/> by
/// default.
/// </summary>
internal bool IsPrimary { get; set; } = true;
// Actual property values for 'RunAnalyzers' and 'RunAnalyzersDuringLiveAnalysis' properties from the project file.
// Both these properties can be used to configure running analyzers, with RunAnalyzers overriding RunAnalyzersDuringLiveAnalysis.
private bool? _runAnalyzersPropertyValue;
private bool? _runAnalyzersDuringLiveAnalysisPropertyValue;
// Effective boolean value to determine if analyzers should be executed based on _runAnalyzersPropertyValue and _runAnalyzersDuringLiveAnalysisPropertyValue.
private bool _runAnalyzers = true;
/// <summary>
/// The full list of all metadata references this project has. References that have internally been converted to project references
/// will still be in this.
/// </summary>
private readonly Dictionary<string, ImmutableArray<MetadataReferenceProperties>> _allMetadataReferences = new();
/// <summary>
/// The file watching tokens for the documents in this project. We get the tokens even when we're in a batch, so the files here
/// may not be in the actual workspace yet.
/// </summary>
private readonly Dictionary<DocumentId, FileChangeWatcher.IFileWatchingToken> _documentFileWatchingTokens = new();
/// <summary>
/// A file change context used to watch source files, additional files, and analyzer config files for this project. It's automatically set to watch the user's project
/// directory so we avoid file-by-file watching.
/// </summary>
private readonly FileChangeWatcher.IContext _documentFileChangeContext;
/// <summary>
/// track whether we have been subscribed to <see cref="IDynamicFileInfoProvider.Updated"/> event
/// </summary>
private readonly HashSet<IDynamicFileInfoProvider> _eventSubscriptionTracker = new();
/// <summary>
/// Map of the original dynamic file path to the <see cref="DynamicFileInfo.FilePath"/> that was associated with it.
///
/// For example, the key is something like Page.cshtml which is given to us from the project system calling
/// <see cref="AddDynamicSourceFile(string, ImmutableArray{string})"/>. The value of the map is a generated file that
/// corresponds to the original path, say Page.g.cs. If we were given a file by the project system but no
/// <see cref="IDynamicFileInfoProvider"/> provided a file for it, we will record the value as null so we still can track
/// the addition of the .cshtml file for a later call to <see cref="RemoveDynamicSourceFile(string)"/>.
///
/// The workspace snapshot will only have a document with <see cref="DynamicFileInfo.FilePath"/> (the value) but not the
/// original dynamic file path (the key).
/// </summary>
/// <remarks>
/// We use the same string comparer as in the <see cref="BatchingDocumentCollection"/> used by _sourceFiles, below, as these
/// files are added to that collection too.
/// </remarks>
private readonly Dictionary<string, string?> _dynamicFilePathMaps = new(StringComparer.OrdinalIgnoreCase);
private readonly BatchingDocumentCollection _sourceFiles;
private readonly BatchingDocumentCollection _additionalFiles;
private readonly BatchingDocumentCollection _analyzerConfigFiles;
private readonly AsyncBatchingWorkQueue<string> _fileChangesToProcess;
private readonly CancellationTokenSource _asynchronousFileChangeProcessingCancellationTokenSource = new();
public ProjectId Id { get; }
public string Language { get; }
internal VisualStudioProject(
VisualStudioWorkspaceImpl workspace,
ImmutableArray<Lazy<IDynamicFileInfoProvider, FileExtensionsMetadata>> dynamicFileInfoProviders,
HostDiagnosticUpdateSource hostDiagnosticUpdateSource,
ProjectId id,
string displayName,
string language,
string assemblyName,
CompilationOptions? compilationOptions,
string? filePath,
ParseOptions? parseOptions)
{
_workspace = workspace;
_dynamicFileInfoProviders = dynamicFileInfoProviders;
_hostDiagnosticUpdateSource = hostDiagnosticUpdateSource;
Id = id;
Language = language;
_displayName = displayName;
_sourceFiles = new BatchingDocumentCollection(
this,
documentAlreadyInWorkspace: (s, d) => s.ContainsDocument(d),
documentAddAction: (w, d) => w.OnDocumentAdded(d),
documentRemoveAction: (w, documentId) => w.OnDocumentRemoved(documentId),
documentTextLoaderChangedAction: (s, d, loader) => s.WithDocumentTextLoader(d, loader, PreservationMode.PreserveValue),
documentChangedWorkspaceKind: WorkspaceChangeKind.DocumentChanged);
_additionalFiles = new BatchingDocumentCollection(this,
(s, d) => s.ContainsAdditionalDocument(d),
(w, d) => w.OnAdditionalDocumentAdded(d),
(w, documentId) => w.OnAdditionalDocumentRemoved(documentId),
documentTextLoaderChangedAction: (s, d, loader) => s.WithAdditionalDocumentTextLoader(d, loader, PreservationMode.PreserveValue),
documentChangedWorkspaceKind: WorkspaceChangeKind.AdditionalDocumentChanged);
_analyzerConfigFiles = new BatchingDocumentCollection(this,
(s, d) => s.ContainsAnalyzerConfigDocument(d),
(w, d) => w.OnAnalyzerConfigDocumentAdded(d),
(w, documentId) => w.OnAnalyzerConfigDocumentRemoved(documentId),
documentTextLoaderChangedAction: (s, d, loader) => s.WithAnalyzerConfigDocumentTextLoader(d, loader, PreservationMode.PreserveValue),
documentChangedWorkspaceKind: WorkspaceChangeKind.AnalyzerConfigDocumentChanged);
_fileChangesToProcess = new AsyncBatchingWorkQueue<string>(
TimeSpan.FromMilliseconds(200), // 200 chosen with absolutely no evidence whatsoever
ProcessFileChangesAsync,
StringComparer.Ordinal,
workspace.Services.GetRequiredService<IWorkspaceAsynchronousOperationListenerProvider>().GetListener(),
_asynchronousFileChangeProcessingCancellationTokenSource.Token);
_assemblyName = assemblyName;
_compilationOptions = compilationOptions;
_filePath = filePath;
_parseOptions = parseOptions;
var fileExtensionToWatch = language switch { LanguageNames.CSharp => ".cs", LanguageNames.VisualBasic => ".vb", _ => null };
if (filePath != null && fileExtensionToWatch != null)
{
// Since we have a project directory, we'll just watch all the files under that path; that'll avoid extra overhead of
// having to add explicit file watches everywhere.
var projectDirectoryToWatch = new FileChangeWatcher.WatchedDirectory(Path.GetDirectoryName(filePath), fileExtensionToWatch);
_documentFileChangeContext = _workspace.FileChangeWatcher.CreateContext(projectDirectoryToWatch);
}
else
{
_documentFileChangeContext = workspace.FileChangeWatcher.CreateContext();
}
_documentFileChangeContext.FileChanged += DocumentFileChangeContext_FileChanged;
}
private void ChangeProjectProperty<T>(ref T field, T newValue, Func<Solution, Solution> updateSolution, bool logThrowAwayTelemetry = false)
{
ChangeProjectProperty(
ref field,
newValue,
(solutionChanges, oldValue) => solutionChanges.UpdateSolutionForProjectAction(Id, updateSolution(solutionChanges.Solution)),
logThrowAwayTelemetry);
}
private void ChangeProjectProperty<T>(ref T field, T newValue, Action<SolutionChangeAccumulator, T> updateSolution, bool logThrowAwayTelemetry = false)
{
using (_gate.DisposableWait())
{
// If nothing is changing, we can skip entirely
if (object.Equals(field, newValue))
{
return;
}
var oldValue = field;
field = newValue;
if (logThrowAwayTelemetry)
{
var telemetryService = _workspace.Services.GetService<IWorkspaceTelemetryService>();
if (telemetryService?.HasActiveSession == true)
{
var workspaceStatusService = _workspace.Services.GetService<IWorkspaceStatusService>();
// We only log telemetry during solution open
// Importantly, we do not await/wait on the fullyLoadedStateTask. We do not want to ever be waiting on work
// that may end up touching the UI thread (As we can deadlock if GetTagsSynchronous waits on us). Instead,
// we only check if the Task is completed. Prior to that we will assume we are still loading. Once this
// task is completed, we know that the WaitUntilFullyLoadedAsync call will have actually finished and we're
// fully loaded.
var isFullyLoadedTask = workspaceStatusService?.IsFullyLoadedAsync(CancellationToken.None);
var isFullyLoaded = isFullyLoadedTask is { IsCompleted: true } && isFullyLoadedTask.GetAwaiter().GetResult();
if (!isFullyLoaded)
{
TryReportCompilationThrownAway(_workspace.CurrentSolution.State, Id);
}
}
}
if (_activeBatchScopes > 0)
{
_projectPropertyModificationsInBatch.Add(solutionChanges => updateSolution(solutionChanges, oldValue));
}
else
{
_workspace.ApplyBatchChangeToWorkspace(solutionChanges =>
{
updateSolution(solutionChanges, oldValue);
});
}
}
}
/// <summary>
/// Reports a telemetry event if compilation information is being thrown away after being previously computed
/// </summary>
private static void TryReportCompilationThrownAway(SolutionState solutionState, ProjectId projectId)
{
// We log the number of syntax trees that have been parsed even if there was no compilation created yet
var projectState = solutionState.GetRequiredProjectState(projectId);
var parsedTrees = 0;
foreach (var (_, documentState) in projectState.DocumentStates.States)
{
if (documentState.TryGetSyntaxTree(out _))
{
parsedTrees++;
}
}
// But we also want to know if a compilation was created
var hadCompilation = solutionState.TryGetCompilation(projectId, out _);
if (parsedTrees > 0 || hadCompilation)
{
Logger.Log(FunctionId.Workspace_Project_CompilationThrownAway, KeyValueLogMessage.Create(m =>
{
// Note: Not using our project Id. This is the same ProjectGuid that the project system uses
// so data can be correlated
m["ProjectGuid"] = projectState.ProjectInfo.Attributes.TelemetryId.ToString("B");
m["SyntaxTreesParsed"] = parsedTrees;
m["HadCompilation"] = hadCompilation;
}));
}
}
private void ChangeProjectOutputPath(ref string? field, string? newValue, Func<Solution, Solution> withNewValue)
{
ChangeProjectProperty(ref field, newValue, (solutionChanges, oldValue) =>
{
// First, update the property itself that's exposed on the Project.
solutionChanges.UpdateSolutionForProjectAction(Id, withNewValue(solutionChanges.Solution));
if (oldValue != null)
{
_workspace.RemoveProjectOutputPath_NoLock(solutionChanges, Id, oldValue);
}
if (newValue != null)
{
_workspace.AddProjectOutputPath_NoLock(solutionChanges, Id, newValue);
}
});
}
public string AssemblyName
{
get => _assemblyName;
set => ChangeProjectProperty(ref _assemblyName, value, s => s.WithProjectAssemblyName(Id, value), logThrowAwayTelemetry: true);
}
// The property could be null if this is a non-C#/VB language and we don't have one for it. But we disallow assigning null, because C#/VB cannot end up null
// again once they already had one.
[DisallowNull]
public CompilationOptions? CompilationOptions
{
get => _compilationOptions;
set => ChangeProjectProperty(ref _compilationOptions, value, s => s.WithProjectCompilationOptions(Id, value), logThrowAwayTelemetry: true);
}
// The property could be null if this is a non-C#/VB language and we don't have one for it. But we disallow assigning null, because C#/VB cannot end up null
// again once they already had one.
[DisallowNull]
public ParseOptions? ParseOptions
{
get => _parseOptions;
set => ChangeProjectProperty(ref _parseOptions, value, s => s.WithProjectParseOptions(Id, value), logThrowAwayTelemetry: true);
}
/// <summary>
/// The path to the output in obj.
/// </summary>
internal string? CompilationOutputAssemblyFilePath
{
get => _compilationOutputAssemblyFilePath;
set => ChangeProjectOutputPath(
ref _compilationOutputAssemblyFilePath,
value,
s => s.WithProjectCompilationOutputInfo(Id, s.GetRequiredProject(Id).CompilationOutputInfo.WithAssemblyPath(value)));
}
public string? OutputFilePath
{
get => _outputFilePath;
set => ChangeProjectOutputPath(ref _outputFilePath, value, s => s.WithProjectOutputFilePath(Id, value));
}
public string? OutputRefFilePath
{
get => _outputRefFilePath;
set => ChangeProjectOutputPath(ref _outputRefFilePath, value, s => s.WithProjectOutputRefFilePath(Id, value));
}
public string? FilePath
{
get => _filePath;
set => ChangeProjectProperty(ref _filePath, value, s => s.WithProjectFilePath(Id, value));
}
public string DisplayName
{
get => _displayName;
set => ChangeProjectProperty(ref _displayName, value, s => s.WithProjectName(Id, value));
}
// internal to match the visibility of the Workspace-level API -- this is something
// we use but we haven't made officially public yet.
internal bool HasAllInformation
{
get => _hasAllInformation;
set => ChangeProjectProperty(ref _hasAllInformation, value, s => s.WithHasAllInformation(Id, value));
}
internal bool? RunAnalyzers
{
get => _runAnalyzersPropertyValue;
set
{
_runAnalyzersPropertyValue = value;
UpdateRunAnalyzers();
}
}
internal bool? RunAnalyzersDuringLiveAnalysis
{
get => _runAnalyzersDuringLiveAnalysisPropertyValue;
set
{
_runAnalyzersDuringLiveAnalysisPropertyValue = value;
UpdateRunAnalyzers();
}
}
private void UpdateRunAnalyzers()
{
// Property RunAnalyzers overrides RunAnalyzersDuringLiveAnalysis, and default when both properties are not set is 'true'.
var runAnalyzers = _runAnalyzersPropertyValue ?? _runAnalyzersDuringLiveAnalysisPropertyValue ?? true;
ChangeProjectProperty(ref _runAnalyzers, runAnalyzers, s => s.WithRunAnalyzers(Id, runAnalyzers));
}
/// <summary>
/// The default namespace of the project.
/// </summary>
/// <remarks>
/// In C#, this is defined as the value of "rootnamespace" msbuild property. Right now VB doesn't
/// have the concept of "default namespace", but we conjure one in workspace by assigning the value
/// of the project's root namespace to it. So various features can choose to use it for their own purpose.
///
/// In the future, we might consider officially exposing "default namespace" for VB project
/// (e.g.through a "defaultnamespace" msbuild property)
/// </remarks>
internal string? DefaultNamespace
{
get => _defaultNamespace;
set => ChangeProjectProperty(ref _defaultNamespace, value, s => s.WithProjectDefaultNamespace(Id, value));
}
/// <summary>
/// The max language version supported for this project, if applicable. Useful to help indicate what
/// language version features should be suggested to a user, as well as if they can be upgraded.
/// </summary>
internal string? MaxLangVersion
{
set => _workspace.SetMaxLanguageVersion(Id, value);
}
internal string DependencyNodeTargetIdentifier
{
set => _workspace.SetDependencyNodeTargetIdentifier(Id, value);
}
private bool HasBeenRemoved => !_workspace.CurrentSolution.ContainsProject(Id);
#region Batching
public BatchScope CreateBatchScope()
{
using (_gate.DisposableWait())
{
_activeBatchScopes++;
return new BatchScope(this);
}
}
public sealed class BatchScope : IDisposable, IAsyncDisposable
{
private readonly VisualStudioProject _project;
/// <summary>
/// Flag to control if this has already been disposed. Not a boolean only so it can be used with Interlocked.CompareExchange.
/// </summary>
private volatile int _disposed = 0;
internal BatchScope(VisualStudioProject visualStudioProject)
=> _project = visualStudioProject;
public void Dispose()
{
if (Interlocked.CompareExchange(ref _disposed, 1, 0) == 0)
{
_project.OnBatchScopeDisposedMaybeAsync(useAsync: false).VerifyCompleted();
}
}
public async ValueTask DisposeAsync()
{
if (Interlocked.CompareExchange(ref _disposed, 1, 0) == 0)
{
await _project.OnBatchScopeDisposedMaybeAsync(useAsync: true).ConfigureAwait(false);
}
}
}
private async Task OnBatchScopeDisposedMaybeAsync(bool useAsync)
{
using (useAsync ? await _gate.DisposableWaitAsync().ConfigureAwait(false) : _gate.DisposableWait())
{
_activeBatchScopes--;
if (_activeBatchScopes > 0)
{
return;
}
// If the project was already removed, we'll just ignore any further requests to complete batches.
if (HasBeenRemoved)
{
return;
}
var documentFileNamesAdded = ImmutableArray.CreateBuilder<string>();
var documentsToOpen = new List<(DocumentId documentId, SourceTextContainer textContainer)>();
var additionalDocumentsToOpen = new List<(DocumentId documentId, SourceTextContainer textContainer)>();
var analyzerConfigDocumentsToOpen = new List<(DocumentId documentId, SourceTextContainer textContainer)>();
await _workspace.ApplyBatchChangeToWorkspaceMaybeAsync(useAsync, solutionChanges =>
{
_sourceFiles.UpdateSolutionForBatch(
solutionChanges,
documentFileNamesAdded,
documentsToOpen,
(s, documents) => s.AddDocuments(documents),
WorkspaceChangeKind.DocumentAdded,
(s, ids) => s.RemoveDocuments(ids),
WorkspaceChangeKind.DocumentRemoved);
_additionalFiles.UpdateSolutionForBatch(
solutionChanges,
documentFileNamesAdded,
additionalDocumentsToOpen,
(s, documents) =>
{
foreach (var document in documents)
{
s = s.AddAdditionalDocument(document);
}
return s;
},
WorkspaceChangeKind.AdditionalDocumentAdded,
(s, ids) => s.RemoveAdditionalDocuments(ids),
WorkspaceChangeKind.AdditionalDocumentRemoved);
_analyzerConfigFiles.UpdateSolutionForBatch(
solutionChanges,
documentFileNamesAdded,
analyzerConfigDocumentsToOpen,
(s, documents) => s.AddAnalyzerConfigDocuments(documents),
WorkspaceChangeKind.AnalyzerConfigDocumentAdded,
(s, ids) => s.RemoveAnalyzerConfigDocuments(ids),
WorkspaceChangeKind.AnalyzerConfigDocumentRemoved);
// Metadata reference removing. Do this before adding in case this removes a project reference that
// we are also going to add in the same batch. This could happen if case is changing, or we're targeting
// a different output path (say bin vs. obj vs. ref).
foreach (var (path, properties) in _metadataReferencesRemovedInBatch)
{
var projectReference = _workspace.TryRemoveConvertedProjectReference_NoLock(Id, path, properties);
if (projectReference != null)
{
solutionChanges.UpdateSolutionForProjectAction(
Id,
solutionChanges.Solution.RemoveProjectReference(Id, projectReference));
}
else
{
// TODO: find a cleaner way to fetch this
var metadataReference = _workspace.CurrentSolution.GetRequiredProject(Id).MetadataReferences.Cast<PortableExecutableReference>()
.Single(m => m.FilePath == path && m.Properties == properties);
_workspace.FileWatchedReferenceFactory.StopWatchingReference(metadataReference);
solutionChanges.UpdateSolutionForProjectAction(
Id,
newSolution: solutionChanges.Solution.RemoveMetadataReference(Id, metadataReference));
}
}
ClearAndZeroCapacity(_metadataReferencesRemovedInBatch);
// Metadata reference adding...
if (_metadataReferencesAddedInBatch.Count > 0)
{
var projectReferencesCreated = new List<ProjectReference>();
var metadataReferencesCreated = new List<MetadataReference>();
foreach (var (path, properties) in _metadataReferencesAddedInBatch)
{
var projectReference = _workspace.TryCreateConvertedProjectReference_NoLock(Id, path, properties);
if (projectReference != null)
{
projectReferencesCreated.Add(projectReference);
}
else
{
var metadataReference = _workspace.FileWatchedReferenceFactory.CreateReferenceAndStartWatchingFile(path, properties);
metadataReferencesCreated.Add(metadataReference);
}
}
solutionChanges.UpdateSolutionForProjectAction(
Id,
solutionChanges.Solution.AddProjectReferences(Id, projectReferencesCreated)
.AddMetadataReferences(Id, metadataReferencesCreated));
ClearAndZeroCapacity(_metadataReferencesAddedInBatch);
}
// Project reference adding...
solutionChanges.UpdateSolutionForProjectAction(
Id,
newSolution: solutionChanges.Solution.AddProjectReferences(Id, _projectReferencesAddedInBatch));
ClearAndZeroCapacity(_projectReferencesAddedInBatch);
// Project reference removing...
foreach (var projectReference in _projectReferencesRemovedInBatch)
{
solutionChanges.UpdateSolutionForProjectAction(
Id,
newSolution: solutionChanges.Solution.RemoveProjectReference(Id, projectReference));
}
ClearAndZeroCapacity(_projectReferencesRemovedInBatch);
// Analyzer reference adding...
solutionChanges.UpdateSolutionForProjectAction(
Id,
newSolution: solutionChanges.Solution.AddAnalyzerReferences(Id, _analyzersAddedInBatch.Select(a => a.GetReference())));
ClearAndZeroCapacity(_analyzersAddedInBatch);
// Analyzer reference removing...
foreach (var analyzerReference in _analyzersRemovedInBatch)
{
solutionChanges.UpdateSolutionForProjectAction(
Id,
newSolution: solutionChanges.Solution.RemoveAnalyzerReference(Id, analyzerReference.GetReference()));
analyzerReference.Dispose();
}
ClearAndZeroCapacity(_analyzersRemovedInBatch);
// Other property modifications...
foreach (var propertyModification in _projectPropertyModificationsInBatch)
{
propertyModification(solutionChanges);
}
ClearAndZeroCapacity(_projectPropertyModificationsInBatch);
}).ConfigureAwait(false);
foreach (var (documentId, textContainer) in documentsToOpen)
{
await _workspace.ApplyChangeToWorkspaceMaybeAsync(useAsync, w => w.OnDocumentOpened(documentId, textContainer)).ConfigureAwait(false);
}
foreach (var (documentId, textContainer) in additionalDocumentsToOpen)
{
await _workspace.ApplyChangeToWorkspaceMaybeAsync(useAsync, w => w.OnAdditionalDocumentOpened(documentId, textContainer)).ConfigureAwait(false);
}
foreach (var (documentId, textContainer) in analyzerConfigDocumentsToOpen)
{
await _workspace.ApplyChangeToWorkspaceMaybeAsync(useAsync, w => w.OnAnalyzerConfigDocumentOpened(documentId, textContainer)).ConfigureAwait(false);
}
// Check for those files being opened to start wire-up if necessary
_workspace.QueueCheckForFilesBeingOpen(documentFileNamesAdded.ToImmutable());
}
}
#endregion
#region Source File Addition/Removal
public void AddSourceFile(string fullPath, SourceCodeKind sourceCodeKind = SourceCodeKind.Regular, ImmutableArray<string> folders = default)
=> _sourceFiles.AddFile(fullPath, sourceCodeKind, folders);
/// <summary>
/// Adds a source file to the project from a text container (eg, a Visual Studio Text buffer)
/// </summary>
/// <param name="textContainer">The text container that contains this file.</param>
/// <param name="fullPath">The file path of the document.</param>
/// <param name="sourceCodeKind">The kind of the source code.</param>
/// <param name="folders">The names of the logical nested folders the document is contained in.</param>
/// <param name="designTimeOnly">Whether the document is used only for design time (eg. completion) or also included in a compilation.</param>
/// <param name="documentServiceProvider">A <see cref="IDocumentServiceProvider"/> associated with this document</param>
public DocumentId AddSourceTextContainer(
SourceTextContainer textContainer,
string fullPath,
SourceCodeKind sourceCodeKind = SourceCodeKind.Regular,
ImmutableArray<string> folders = default,
bool designTimeOnly = false,
IDocumentServiceProvider? documentServiceProvider = null)
{
return _sourceFiles.AddTextContainer(textContainer, fullPath, sourceCodeKind, folders, designTimeOnly, documentServiceProvider);
}
public bool ContainsSourceFile(string fullPath)
=> _sourceFiles.ContainsFile(fullPath);
public void RemoveSourceFile(string fullPath)
=> _sourceFiles.RemoveFile(fullPath);
public void RemoveSourceTextContainer(SourceTextContainer textContainer)
=> _sourceFiles.RemoveTextContainer(textContainer);
#endregion
#region Additional File Addition/Removal
// TODO: should AdditionalFiles have source code kinds?
public void AddAdditionalFile(string fullPath, SourceCodeKind sourceCodeKind = SourceCodeKind.Regular)
=> _additionalFiles.AddFile(fullPath, sourceCodeKind, folders: default);
public bool ContainsAdditionalFile(string fullPath)
=> _additionalFiles.ContainsFile(fullPath);
public void RemoveAdditionalFile(string fullPath)
=> _additionalFiles.RemoveFile(fullPath);
#endregion
#region Analyzer Config File Addition/Removal
public void AddAnalyzerConfigFile(string fullPath)
{
// TODO: do we need folders for analyzer config files?
_analyzerConfigFiles.AddFile(fullPath, SourceCodeKind.Regular, folders: default);
}
public bool ContainsAnalyzerConfigFile(string fullPath)
=> _analyzerConfigFiles.ContainsFile(fullPath);
public void RemoveAnalyzerConfigFile(string fullPath)
=> _analyzerConfigFiles.RemoveFile(fullPath);
#endregion
#region Non Source File Addition/Removal
public void AddDynamicSourceFile(string dynamicFilePath, ImmutableArray<string> folders)
{
DynamicFileInfo? fileInfo = null;
IDynamicFileInfoProvider? providerForFileInfo = null;
var extension = FileNameUtilities.GetExtension(dynamicFilePath)?.TrimStart('.');
if (extension?.Length == 0)
{
fileInfo = null;
}
else
{
foreach (var provider in _dynamicFileInfoProviders)
{
// skip unrelated providers
if (!provider.Metadata.Extensions.Any(e => string.Equals(e, extension, StringComparison.OrdinalIgnoreCase)))
{
continue;
}
// Don't get confused by _filePath and filePath.
// VisualStudioProject._filePath points to csproj/vbproj of the project
// and the parameter filePath points to dynamic file such as ASP.NET .g.cs files.
//
// Also, provider is free-threaded. so fine to call Wait rather than JTF.
fileInfo = provider.Value.GetDynamicFileInfoAsync(
projectId: Id, projectFilePath: _filePath, filePath: dynamicFilePath, CancellationToken.None).WaitAndGetResult_CanCallOnBackground(CancellationToken.None);
if (fileInfo != null)
{
fileInfo = FixUpDynamicFileInfo(fileInfo, dynamicFilePath);
providerForFileInfo = provider.Value;
break;
}
}
}
using (_gate.DisposableWait())
{
if (_dynamicFilePathMaps.ContainsKey(dynamicFilePath))
{
// TODO: if we have a duplicate, we are not calling RemoveDynamicFileInfoAsync since we
// don't want to call with that under a lock. If we are getting duplicates we have bigger problems
// at that point since our workspace is generally out of sync with the project system.
// Given we're taking this as a late fix prior to a release, I don't think it's worth the added
// risk to handle a case that wasn't handled before either.
throw new ArgumentException($"{dynamicFilePath} has already been added to this project.");
}
// Record the mapping from the dynamic file path to the source file it generated. We will record
// 'null' if no provider was able to produce a source file for this input file. That could happen
// if the provider (say ASP.NET Razor) doesn't recognize the file, or the wrong type of file
// got passed through the system. That's not a failure from the project system's perspective:
// adding dynamic files is a hint at best that doesn't impact it.
_dynamicFilePathMaps.Add(dynamicFilePath, fileInfo?.FilePath);
if (fileInfo != null)
{
// If fileInfo is not null, that means we found a provider so this should be not-null as well
// since we had to go through the earlier assignment.
Contract.ThrowIfNull(providerForFileInfo);
_sourceFiles.AddDynamicFile_NoLock(providerForFileInfo, fileInfo, folders);
}
}
}
private static DynamicFileInfo FixUpDynamicFileInfo(DynamicFileInfo fileInfo, string filePath)
{
// we might change contract and just throw here. but for now, we keep existing contract where one can return null for DynamicFileInfo.FilePath.
// In this case we substitute the file being generated from so we still have some path.
if (string.IsNullOrEmpty(fileInfo.FilePath))
{
return new DynamicFileInfo(filePath, fileInfo.SourceCodeKind, fileInfo.TextLoader, fileInfo.DesignTimeOnly, fileInfo.DocumentServiceProvider);
}
return fileInfo;
}
public void RemoveDynamicSourceFile(string dynamicFilePath)
{
IDynamicFileInfoProvider provider;
using (_gate.DisposableWait())
{
if (!_dynamicFilePathMaps.TryGetValue(dynamicFilePath, out var sourceFilePath))
{
throw new ArgumentException($"{dynamicFilePath} wasn't added by a previous call to {nameof(AddDynamicSourceFile)}");
}
_dynamicFilePathMaps.Remove(dynamicFilePath);
// If we got a null path back, it means we never had a source file to add. In that case,
// we're done
if (sourceFilePath == null)
{
return;
}
provider = _sourceFiles.RemoveDynamicFile_NoLock(sourceFilePath);
}
// provider is free-threaded. so fine to call Wait rather than JTF
provider.RemoveDynamicFileInfoAsync(
projectId: Id, projectFilePath: _filePath, filePath: dynamicFilePath, CancellationToken.None).Wait(CancellationToken.None);
}
private void OnDynamicFileInfoUpdated(object sender, string dynamicFilePath)
{
string? fileInfoPath;
using (_gate.DisposableWait())
{
if (!_dynamicFilePathMaps.TryGetValue(dynamicFilePath, out fileInfoPath))
{
// given file doesn't belong to this project.
// this happen since the event this is handling is shared between all projects
return;
}
}
if (fileInfoPath != null)
{
_sourceFiles.ProcessDynamicFileChange(dynamicFilePath, fileInfoPath);
}
}
#endregion
#region Analyzer Addition/Removal
public void AddAnalyzerReference(string fullPath)
{
CompilerPathUtilities.RequireAbsolutePath(fullPath, nameof(fullPath));
using (_gate.DisposableWait())
{
if (_analyzerPathsToAnalyzers.ContainsKey(fullPath))
{
throw new ArgumentException($"'{fullPath}' has already been added to this project.", nameof(fullPath));
}
// Are we adding one we just recently removed? If so, we can just keep using that one, and avoid removing
// it once we apply the batch
var analyzerPendingRemoval = _analyzersRemovedInBatch.FirstOrDefault(a => a.FullPath == fullPath);
if (analyzerPendingRemoval != null)
{
_analyzersRemovedInBatch.Remove(analyzerPendingRemoval);
_analyzerPathsToAnalyzers.Add(fullPath, analyzerPendingRemoval);
}
else
{
// Nope, we actually need to make a new one.
var visualStudioAnalyzer = new VisualStudioAnalyzer(
fullPath,
_hostDiagnosticUpdateSource,
Id,
Language);
_analyzerPathsToAnalyzers.Add(fullPath, visualStudioAnalyzer);
if (_activeBatchScopes > 0)
{
_analyzersAddedInBatch.Add(visualStudioAnalyzer);
}
else
{
_workspace.ApplyChangeToWorkspace(w => w.OnAnalyzerReferenceAdded(Id, visualStudioAnalyzer.GetReference()));
}
}
}
}
public void RemoveAnalyzerReference(string fullPath)
{
if (string.IsNullOrEmpty(fullPath))
{
throw new ArgumentException("message", nameof(fullPath));
}
using (_gate.DisposableWait())
{
if (!_analyzerPathsToAnalyzers.TryGetValue(fullPath, out var visualStudioAnalyzer))
{
throw new ArgumentException($"'{fullPath}' is not an analyzer of this project.", nameof(fullPath));
}
_analyzerPathsToAnalyzers.Remove(fullPath);
if (_activeBatchScopes > 0)
{
// This analyzer may be one we've just added in the same batch; in that case, just don't add
// it in the first place.
if (_analyzersAddedInBatch.Remove(visualStudioAnalyzer))
{
// Nothing is holding onto this analyzer now, so get rid of it
visualStudioAnalyzer.Dispose();
}
else
{
_analyzersRemovedInBatch.Add(visualStudioAnalyzer);
}
}
else
{
_workspace.ApplyChangeToWorkspace(w => w.OnAnalyzerReferenceRemoved(Id, visualStudioAnalyzer.GetReference()));
visualStudioAnalyzer.Dispose();
}
}
}
#endregion
private void DocumentFileChangeContext_FileChanged(object sender, string fullFilePath)
{
_fileChangesToProcess.AddWork(fullFilePath);
}
private async ValueTask ProcessFileChangesAsync(ImmutableSegmentedList<string> filePaths, CancellationToken cancellationToken)
{
await _sourceFiles.ProcessRegularFileChangesAsync(filePaths).ConfigureAwait(false);
await _additionalFiles.ProcessRegularFileChangesAsync(filePaths).ConfigureAwait(false);
await _analyzerConfigFiles.ProcessRegularFileChangesAsync(filePaths).ConfigureAwait(false);
}
#region Metadata Reference Addition/Removal
public void AddMetadataReference(string fullPath, MetadataReferenceProperties properties)
{
if (string.IsNullOrEmpty(fullPath))
{
throw new ArgumentException($"{nameof(fullPath)} isn't a valid path.", nameof(fullPath));
}
using (_gate.DisposableWait())
{
if (ContainsMetadataReference_NoLock(fullPath, properties))
{
throw new InvalidOperationException("The metadata reference has already been added to the project.");
}
_allMetadataReferences.MultiAdd(fullPath, properties, s_defaultMetadataReferenceProperties);
if (_activeBatchScopes > 0)
{
if (!_metadataReferencesRemovedInBatch.Remove((fullPath, properties)))
{
_metadataReferencesAddedInBatch.Add((fullPath, properties));
}
}
else
{
_workspace.ApplyChangeToWorkspace(w =>
{
var projectReference = _workspace.TryCreateConvertedProjectReference_NoLock(Id, fullPath, properties);