-
Notifications
You must be signed in to change notification settings - Fork 4k
/
MSBuildWorkspace.cs
702 lines (606 loc) · 31.2 KB
/
MSBuildWorkspace.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
// 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.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Build.Framework;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.MSBuild
{
/// <summary>
/// A workspace that can be populated by opening MSBuild solution and project files.
/// </summary>
public sealed class MSBuildWorkspace : Workspace
{
// used to serialize access to public methods
private readonly NonReentrantLock _serializationLock = new();
private readonly MSBuildProjectLoader _loader;
private readonly Microsoft.Extensions.Logging.ILoggerFactory _loggerFactory;
private readonly ProjectFileExtensionRegistry _projectFileExtensionRegistry;
private readonly DiagnosticReporter _reporter;
private MSBuildWorkspace(
HostServices hostServices,
ImmutableDictionary<string, string> properties)
: base(hostServices, WorkspaceKind.MSBuild)
{
_reporter = new DiagnosticReporter(this);
_projectFileExtensionRegistry = new ProjectFileExtensionRegistry(Services.SolutionServices, _reporter);
_loggerFactory = DiagnosticReporterLoggerProvider.CreateLoggerFactoryForDiagnosticReporter(_reporter);
_loader = new MSBuildProjectLoader(Services.SolutionServices, _reporter, _loggerFactory, _projectFileExtensionRegistry, properties);
}
/// <summary>
/// Create a new instance of a workspace that can be populated by opening solution and project files.
/// </summary>
public static MSBuildWorkspace Create()
{
return Create(ImmutableDictionary<string, string>.Empty);
}
/// <summary>
/// Create a new instance of a workspace that can be populated by opening solution and project files.
/// </summary>
/// <param name="properties">An optional set of MSBuild properties used when interpreting project files.
/// These are the same properties that are passed to msbuild via the /property:<n>=<v> command line argument.</param>
public static MSBuildWorkspace Create(IDictionary<string, string> properties)
{
return Create(properties, MSBuildMefHostServices.DefaultServices);
}
/// <summary>
/// Create a new instance of a workspace that can be populated by opening solution and project files.
/// </summary>
/// <param name="hostServices">The <see cref="HostServices"/> used to configure this workspace.</param>
public static MSBuildWorkspace Create(HostServices hostServices)
{
return Create(ImmutableDictionary<string, string>.Empty, hostServices);
}
/// <summary>
/// Create a new instance of a workspace that can be populated by opening solution and project files.
/// </summary>
/// <param name="properties">The MSBuild properties used when interpreting project files.
/// These are the same properties that are passed to msbuild via the /property:<n>=<v> command line argument.</param>
/// <param name="hostServices">The <see cref="HostServices"/> used to configure this workspace.</param>
public static MSBuildWorkspace Create(IDictionary<string, string> properties, HostServices hostServices)
{
if (properties == null)
{
throw new ArgumentNullException(nameof(properties));
}
if (hostServices == null)
{
throw new ArgumentNullException(nameof(hostServices));
}
return new MSBuildWorkspace(hostServices, properties.ToImmutableDictionary());
}
/// <summary>
/// The MSBuild properties used when interpreting project files.
/// These are the same properties that are passed to msbuild via the /property:<n>=<v> command line argument.
/// </summary>
public ImmutableDictionary<string, string> Properties => _loader.Properties;
/// <summary>
/// Diagnostics logged while opening solutions, projects and documents.
/// </summary>
public ImmutableList<WorkspaceDiagnostic> Diagnostics => _reporter.Diagnostics;
protected internal override void OnWorkspaceFailed(WorkspaceDiagnostic diagnostic)
{
_reporter.AddDiagnostic(diagnostic);
base.OnWorkspaceFailed(diagnostic);
}
/// <summary>
/// Determines if metadata from existing output assemblies is loaded instead of opening referenced projects.
/// If the referenced project is already opened, the metadata will not be loaded.
/// If the metadata assembly cannot be found the referenced project will be opened instead.
/// </summary>
public bool LoadMetadataForReferencedProjects
{
get { return _loader.LoadMetadataForReferencedProjects; }
set { _loader.LoadMetadataForReferencedProjects = value; }
}
/// <summary>
/// Determines if unrecognized projects are skipped when solutions or projects are opened.
///
/// An project is unrecognized if it either has
/// a) an invalid file path,
/// b) a non-existent project file,
/// c) has an unrecognized file extension or
/// d) a file extension associated with an unsupported language.
///
/// If unrecognized projects cannot be skipped a corresponding exception is thrown.
/// </summary>
public bool SkipUnrecognizedProjects
{
get => _loader.SkipUnrecognizedProjects;
set => _loader.SkipUnrecognizedProjects = value;
}
/// <summary>
/// Associates a project file extension with a language name.
/// </summary>
public void AssociateFileExtensionWithLanguage(string projectFileExtension, string language)
{
_loader.AssociateFileExtensionWithLanguage(projectFileExtension, language);
}
/// <summary>
/// Close the open solution, and reset the workspace to a new empty solution.
/// </summary>
public void CloseSolution()
{
using (_serializationLock.DisposableWait())
{
this.ClearSolution();
}
}
private static string GetAbsolutePath(string path, string baseDirectoryPath)
{
return Path.GetFullPath(FileUtilities.ResolveRelativePath(path, baseDirectoryPath) ?? path);
}
#region Open Solution & Project
/// <summary>
/// Open a solution file and all referenced projects.
/// </summary>
/// <param name="solutionFilePath">The path to the solution file to be opened. This may be an absolute path or a path relative to the
/// current working directory.</param>
/// <param name="progress">An optional <see cref="IProgress{T}"/> that will receive updates as the solution is opened.</param>
/// <param name="cancellationToken">An optional <see cref="CancellationToken"/> to allow cancellation of this operation.</param>
#pragma warning disable RS0026 // Special case to avoid ILogger type getting loaded in downstream clients
public Task<Solution> OpenSolutionAsync(
#pragma warning restore RS0026
string solutionFilePath,
IProgress<ProjectLoadProgress>? progress = null,
CancellationToken cancellationToken = default)
=> OpenSolutionAsync(solutionFilePath, msbuildLogger: null, progress, cancellationToken);
/// <summary>
/// Open a solution file and all referenced projects.
/// </summary>
/// <param name="solutionFilePath">The path to the solution file to be opened. This may be an absolute path or a path relative to the
/// current working directory.</param>
/// <param name="progress">An optional <see cref="IProgress{T}"/> that will receive updates as the solution is opened.</param>
/// <param name="msbuildLogger">An optional <see cref="ILogger"/> that will log msbuild results.</param>
/// <param name="cancellationToken">An optional <see cref="CancellationToken"/> to allow cancellation of this operation.</param>
#pragma warning disable RS0026 // Special case to avoid ILogger type getting loaded in downstream clients
public async Task<Solution> OpenSolutionAsync(
#pragma warning restore RS0026
string solutionFilePath,
ILogger? msbuildLogger,
IProgress<ProjectLoadProgress>? progress = null,
CancellationToken cancellationToken = default)
{
if (solutionFilePath == null)
{
throw new ArgumentNullException(nameof(solutionFilePath));
}
this.ClearSolution();
var solutionInfo = await _loader.LoadSolutionInfoAsync(solutionFilePath, progress, msbuildLogger, cancellationToken).ConfigureAwait(false);
// construct workspace from loaded project infos
this.OnSolutionAdded(solutionInfo);
this.UpdateReferencesAfterAdd();
return this.CurrentSolution;
}
/// <summary>
/// Open a project file and all referenced projects.
/// </summary>
/// <param name="projectFilePath">The path to the project file to be opened. This may be an absolute path or a path relative to the
/// current working directory.</param>
/// <param name="progress">An optional <see cref="IProgress{T}"/> that will receive updates as the project is opened.</param>
/// <param name="cancellationToken">An optional <see cref="CancellationToken"/> to allow cancellation of this operation.</param>
#pragma warning disable RS0026 // Special case to avoid ILogger type getting loaded in downstream clients
public Task<Project> OpenProjectAsync(
#pragma warning restore RS0026
string projectFilePath,
IProgress<ProjectLoadProgress>? progress = null,
CancellationToken cancellationToken = default)
=> OpenProjectAsync(projectFilePath, msbuildLogger: null, progress, cancellationToken);
/// <summary>
/// Open a project file and all referenced projects.
/// </summary>
/// <param name="projectFilePath">The path to the project file to be opened. This may be an absolute path or a path relative to the
/// current working directory.</param>
/// <param name="progress">An optional <see cref="IProgress{T}"/> that will receive updates as the project is opened.</param>
/// <param name="msbuildLogger">An optional <see cref="ILogger"/> that will log msbuild results..</param>
/// <param name="cancellationToken">An optional <see cref="CancellationToken"/> to allow cancellation of this operation.</param>
#pragma warning disable RS0026 // Special case to avoid ILogger type getting loaded in downstream clients
public async Task<Project> OpenProjectAsync(
#pragma warning restore RS0026
string projectFilePath,
ILogger? msbuildLogger,
IProgress<ProjectLoadProgress>? progress = null,
CancellationToken cancellationToken = default)
{
if (projectFilePath == null)
{
throw new ArgumentNullException(nameof(projectFilePath));
}
var projectMap = ProjectMap.Create(this.CurrentSolution);
var projects = await _loader.LoadProjectInfoAsync(projectFilePath, projectMap, progress, msbuildLogger, cancellationToken).ConfigureAwait(false);
// add projects to solution
foreach (var project in projects)
{
this.OnProjectAdded(project);
}
this.UpdateReferencesAfterAdd();
var projectResult = this.CurrentSolution.GetProject(projects[0].Id);
RoslynDebug.AssertNotNull(projectResult);
return projectResult;
}
#endregion
#region Apply Changes
public override bool CanApplyChange(ApplyChangesKind feature)
{
return feature is
ApplyChangesKind.ChangeDocument or
ApplyChangesKind.AddDocument or
ApplyChangesKind.RemoveDocument or
ApplyChangesKind.AddMetadataReference or
ApplyChangesKind.RemoveMetadataReference or
ApplyChangesKind.AddProjectReference or
ApplyChangesKind.RemoveProjectReference or
ApplyChangesKind.AddAnalyzerReference or
ApplyChangesKind.RemoveAnalyzerReference or
ApplyChangesKind.ChangeAdditionalDocument;
}
private static bool HasProjectFileChanges(ProjectChanges changes)
{
return changes.GetAddedDocuments().Any() ||
changes.GetRemovedDocuments().Any() ||
changes.GetAddedMetadataReferences().Any() ||
changes.GetRemovedMetadataReferences().Any() ||
changes.GetAddedProjectReferences().Any() ||
changes.GetRemovedProjectReferences().Any() ||
changes.GetAddedAnalyzerReferences().Any() ||
changes.GetRemovedAnalyzerReferences().Any();
}
private BuildHostProcessManager? _applyChangesBuildHostProcessManager;
/// <summary>
/// The loaded project file that we are currently applying changes to. This is set in <see cref="ApplyProjectChanges(ProjectChanges)"/> if we're modifying a project that is going to require
/// file changes; it's cleared once we're done with that project.
/// </summary>
private RemoteProjectFile? _applyChangesProjectFile;
public override bool TryApplyChanges(Solution newSolution)
{
return TryApplyChanges(newSolution, CodeAnalysisProgress.None);
}
internal override bool TryApplyChanges(Solution newSolution, IProgress<CodeAnalysisProgress> progressTracker)
{
using (_serializationLock.DisposableWait())
{
try
{
Debug.Assert(_applyChangesBuildHostProcessManager == null);
_applyChangesBuildHostProcessManager = new BuildHostProcessManager(Properties, loggerFactory: _loggerFactory);
return base.TryApplyChanges(newSolution, progressTracker);
}
finally
{
_applyChangesBuildHostProcessManager!.DisposeAsync().AsTask().Wait();
_applyChangesBuildHostProcessManager = null;
}
}
}
protected override void ApplyProjectChanges(ProjectChanges projectChanges)
{
Debug.Assert(_applyChangesBuildHostProcessManager != null);
Debug.Assert(_applyChangesProjectFile == null);
var project = projectChanges.OldProject ?? projectChanges.NewProject;
try
{
// if we need to modify the project file, load it first.
if (HasProjectFileChanges(projectChanges))
{
var projectPath = project.FilePath;
if (projectPath is null)
{
_reporter.Report(new ProjectDiagnostic(WorkspaceDiagnosticKind.Failure,
string.Format(WorkspaceMSBuildResources.Project_path_for_0_was_null, project.Name),
projectChanges.ProjectId));
return;
}
if (_projectFileExtensionRegistry.TryGetLanguageNameFromProjectPath(projectPath, DiagnosticReportingMode.Log, out var languageName))
{
try
{
var buildHost = _applyChangesBuildHostProcessManager.GetBuildHostWithFallbackAsync(projectPath, CancellationToken.None).Result;
_applyChangesProjectFile = buildHost.LoadProjectFileAsync(projectPath, languageName, CancellationToken.None).Result;
}
catch (IOException exception)
{
_reporter.Report(new ProjectDiagnostic(WorkspaceDiagnosticKind.Failure, exception.Message, projectChanges.ProjectId));
}
}
}
// do normal apply operations
base.ApplyProjectChanges(projectChanges);
// save project file
if (_applyChangesProjectFile != null)
{
try
{
_applyChangesProjectFile.SaveAsync(CancellationToken.None).Wait();
}
catch (IOException exception)
{
_reporter.Report(new ProjectDiagnostic(WorkspaceDiagnosticKind.Failure, exception.Message, projectChanges.ProjectId));
}
}
}
finally
{
_applyChangesProjectFile = null;
}
}
protected override void ApplyDocumentTextChanged(DocumentId documentId, SourceText text)
{
var document = this.CurrentSolution.GetDocument(documentId);
if (document != null)
{
var encoding = DetermineEncoding(text, document);
if (document.FilePath is null)
{
var message = string.Format(WorkspaceMSBuildResources.Path_for_document_0_was_null, document.Name);
_reporter.Report(new DocumentDiagnostic(WorkspaceDiagnosticKind.Failure, message, document.Id));
return;
}
this.SaveDocumentText(documentId, document.FilePath, text, encoding ?? new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
this.OnDocumentTextChanged(documentId, text, PreservationMode.PreserveValue);
}
}
protected override void ApplyAdditionalDocumentTextChanged(DocumentId documentId, SourceText text)
{
var document = this.CurrentSolution.GetAdditionalDocument(documentId);
if (document != null)
{
var encoding = DetermineEncoding(text, document);
if (document.FilePath is null)
{
var message = string.Format(WorkspaceMSBuildResources.Path_for_additional_document_0_was_null, document.Name);
_reporter.Report(new DocumentDiagnostic(WorkspaceDiagnosticKind.Failure, message, document.Id));
return;
}
this.SaveDocumentText(documentId, document.FilePath, text, encoding ?? new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
this.OnAdditionalDocumentTextChanged(documentId, text, PreservationMode.PreserveValue);
}
}
private static Encoding? DetermineEncoding(SourceText text, TextDocument document)
{
if (text.Encoding != null)
{
return text.Encoding;
}
try
{
if (document.FilePath is null)
{
return null;
}
using var stream = new FileStream(document.FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
var onDiskText = EncodedStringText.Create(stream);
return onDiskText.Encoding;
}
catch (IOException)
{
}
catch (InvalidDataException)
{
}
return null;
}
protected override void ApplyDocumentAdded(DocumentInfo info, SourceText text)
{
Debug.Assert(_applyChangesProjectFile != null);
var project = this.CurrentSolution.GetRequiredProject(info.Id.ProjectId);
var extension = project.Language switch
{
LanguageNames.CSharp => ".cs",
LanguageNames.VisualBasic => ".vb",
_ => throw ExceptionUtilities.UnexpectedValue(project.Language)
};
var fileName = Path.ChangeExtension(info.Name, extension);
var relativePath = (info.Folders != null && info.Folders.Count > 0)
? Path.Combine(Path.Combine([.. info.Folders]), fileName)
: fileName;
var fullPath = GetAbsolutePath(relativePath, Path.GetDirectoryName(project.FilePath)!);
var newDocumentInfo = info.WithName(fileName)
.WithFilePath(fullPath)
.WithTextLoader(new WorkspaceFileTextLoader(Services.SolutionServices, fullPath, text.Encoding));
// add document to project file
_applyChangesProjectFile.AddDocumentAsync(relativePath, logicalPath: null, CancellationToken.None).Wait();
// add to solution
this.OnDocumentAdded(newDocumentInfo);
// save text to disk
if (text != null)
{
this.SaveDocumentText(info.Id, fullPath, text, text.Encoding ?? Encoding.UTF8);
}
}
private void SaveDocumentText(DocumentId id, string fullPath, SourceText newText, Encoding encoding)
{
try
{
var dir = Path.GetDirectoryName(fullPath);
Contract.ThrowIfNull(dir);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
Debug.Assert(encoding != null);
using var writer = new StreamWriter(fullPath, append: false, encoding: encoding);
newText.Write(writer);
}
catch (IOException exception)
{
_reporter.Report(new DocumentDiagnostic(WorkspaceDiagnosticKind.Failure, exception.Message, id));
}
}
protected override void ApplyDocumentRemoved(DocumentId documentId)
{
Debug.Assert(_applyChangesProjectFile != null);
var document = this.CurrentSolution.GetDocument(documentId);
if (document?.FilePath is not null)
{
_applyChangesProjectFile.RemoveDocumentAsync(document.FilePath, CancellationToken.None).Wait();
this.DeleteDocumentFile(document.Id, document.FilePath);
this.OnDocumentRemoved(documentId);
}
}
private void DeleteDocumentFile(DocumentId documentId, string fullPath)
{
try
{
if (File.Exists(fullPath))
{
File.Delete(fullPath);
}
}
catch (IOException exception)
{
_reporter.Report(new DocumentDiagnostic(WorkspaceDiagnosticKind.Failure, exception.Message, documentId));
}
catch (NotSupportedException exception)
{
_reporter.Report(new DocumentDiagnostic(WorkspaceDiagnosticKind.Failure, exception.Message, documentId));
}
catch (UnauthorizedAccessException exception)
{
_reporter.Report(new DocumentDiagnostic(WorkspaceDiagnosticKind.Failure, exception.Message, documentId));
}
}
private static bool IsInGAC(string filePath)
{
return GlobalAssemblyCacheLocation.RootLocations.Any(static (gloc, filePath) => PathUtilities.IsChildPath(gloc, filePath), filePath);
}
private static string? s_frameworkRoot;
private static string FrameworkRoot
{
get
{
if (RoslynString.IsNullOrEmpty(s_frameworkRoot))
{
var runtimeDir = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory();
s_frameworkRoot = Path.GetDirectoryName(runtimeDir); // back out one directory level to be root path of all framework versions
}
return s_frameworkRoot ?? throw new InvalidOperationException($"Unable to get {nameof(FrameworkRoot)}");
}
}
private static bool IsFrameworkReferenceAssembly(string filePath)
{
return PathUtilities.IsChildPath(FrameworkRoot, filePath);
}
protected override void ApplyMetadataReferenceAdded(ProjectId projectId, MetadataReference metadataReference)
{
RoslynDebug.AssertNotNull(_applyChangesProjectFile);
var identity = GetAssemblyIdentity(projectId, metadataReference);
if (identity is null)
{
var message = string.Format(WorkspaceMSBuildResources.Unable_to_add_metadata_reference_0, metadataReference.Display);
_reporter.Report(new ProjectDiagnostic(WorkspaceDiagnosticKind.Failure, message, projectId));
return;
}
if (metadataReference is PortableExecutableReference peRef && peRef.FilePath != null)
{
if (IsInGAC(peRef.FilePath) && identity != null)
{
// Since the location of the reference is in GAC, need to use full identity name to find it again.
// This typically happens when you base the reference off of a reflection assembly location.
_applyChangesProjectFile.AddMetadataReferenceAsync(identity.GetDisplayName(), metadataReference.Properties.Aliases, hintPath: null, CancellationToken.None).Wait();
}
else if (IsFrameworkReferenceAssembly(peRef.FilePath))
{
// just use short name since this will be resolved by msbuild relative to the known framework reference assemblies.
var fileName = identity != null ? identity.Name : Path.GetFileNameWithoutExtension(peRef.FilePath);
_applyChangesProjectFile.AddMetadataReferenceAsync(fileName, metadataReference.Properties.Aliases, hintPath: null, CancellationToken.None).Wait();
}
else // other location -- need hint to find correct assembly
{
var relativePath = PathUtilities.GetRelativePath(Path.GetDirectoryName(CurrentSolution.GetRequiredProject(projectId).FilePath)!, peRef.FilePath);
var fileName = Path.GetFileNameWithoutExtension(peRef.FilePath);
_applyChangesProjectFile.AddMetadataReferenceAsync(fileName, metadataReference.Properties.Aliases, relativePath, CancellationToken.None).Wait();
}
}
this.OnMetadataReferenceAdded(projectId, metadataReference);
}
protected override void ApplyMetadataReferenceRemoved(ProjectId projectId, MetadataReference metadataReference)
{
RoslynDebug.AssertNotNull(_applyChangesProjectFile);
var identity = GetAssemblyIdentity(projectId, metadataReference);
if (identity is null)
{
var message = string.Format(WorkspaceMSBuildResources.Unable_to_remove_metadata_reference_0, metadataReference.Display);
_reporter.Report(new ProjectDiagnostic(WorkspaceDiagnosticKind.Failure, message, projectId));
return;
}
if (metadataReference is PortableExecutableReference peRef && peRef.FilePath != null)
{
_applyChangesProjectFile.RemoveMetadataReferenceAsync(identity.Name, identity.GetDisplayName(), peRef.FilePath, CancellationToken.None).Wait();
}
this.OnMetadataReferenceRemoved(projectId, metadataReference);
}
private AssemblyIdentity? GetAssemblyIdentity(ProjectId projectId, MetadataReference metadataReference)
{
var project = this.CurrentSolution.GetProject(projectId);
if (project is null)
{
return null;
}
if (!project.MetadataReferences.Contains(metadataReference))
{
project = project.AddMetadataReference(metadataReference);
}
var compilation = project.GetCompilationAsync(CancellationToken.None).WaitAndGetResult_CanCallOnBackground(CancellationToken.None);
if (compilation is null)
{
return null;
}
var symbol = compilation.GetAssemblyOrModuleSymbol(metadataReference) as IAssemblySymbol;
return symbol?.Identity;
}
protected override void ApplyProjectReferenceAdded(ProjectId projectId, ProjectReference projectReference)
{
Debug.Assert(_applyChangesProjectFile != null);
var project = this.CurrentSolution.GetProject(projectReference.ProjectId);
if (project?.FilePath is not null)
{
// Only "ReferenceOutputAssembly=true" project references are represented in the workspace:
var reference = new ProjectFileReference(project.FilePath, projectReference.Aliases, referenceOutputAssembly: true);
_applyChangesProjectFile.AddProjectReferenceAsync(project.Name, reference, CancellationToken.None).Wait();
}
this.OnProjectReferenceAdded(projectId, projectReference);
}
protected override void ApplyProjectReferenceRemoved(ProjectId projectId, ProjectReference projectReference)
{
Debug.Assert(_applyChangesProjectFile != null);
var project = this.CurrentSolution.GetProject(projectReference.ProjectId);
if (project?.FilePath is not null)
{
_applyChangesProjectFile.RemoveProjectReferenceAsync(project.Name, project.FilePath, CancellationToken.None).Wait();
}
this.OnProjectReferenceRemoved(projectId, projectReference);
}
protected override void ApplyAnalyzerReferenceAdded(ProjectId projectId, AnalyzerReference analyzerReference)
{
Debug.Assert(_applyChangesProjectFile != null);
if (analyzerReference is AnalyzerFileReference fileRef)
{
_applyChangesProjectFile.AddAnalyzerReferenceAsync(fileRef.FullPath, CancellationToken.None).Wait();
}
this.OnAnalyzerReferenceAdded(projectId, analyzerReference);
}
protected override void ApplyAnalyzerReferenceRemoved(ProjectId projectId, AnalyzerReference analyzerReference)
{
Debug.Assert(_applyChangesProjectFile != null);
if (analyzerReference is AnalyzerFileReference fileRef)
{
_applyChangesProjectFile.RemoveAnalyzerReferenceAsync(fileRef.FullPath, CancellationToken.None).Wait();
}
this.OnAnalyzerReferenceRemoved(projectId, analyzerReference);
}
}
#endregion
}