diff --git a/src/VisualStudio/Core/Def/Implementation/ProjectSystem/VisualStudioAnalyzer.cs b/src/VisualStudio/Core/Def/Implementation/ProjectSystem/VisualStudioAnalyzer.cs index 536f6bb852f564c18afc6752a087b033ec145ed3..29af6b55698ace7fb3f1610946fbf66d8bf2f9ee 100644 --- a/src/VisualStudio/Core/Def/Implementation/ProjectSystem/VisualStudioAnalyzer.cs +++ b/src/VisualStudio/Core/Def/Implementation/ProjectSystem/VisualStudioAnalyzer.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Reflection; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; using Microsoft.VisualStudio.LanguageServices.Implementation.TaskList; @@ -31,7 +32,6 @@ public VisualStudioAnalyzer(string fullPath, IVsFileChangeEx fileChangeService, _tracker = new FileChangeTracker(fileChangeService, fullPath); _tracker.UpdatedOnDisk += OnUpdatedOnDisk; _tracker.StartFileChangeListeningAsync(); - _tracker.EnsureSubscription(); _hostDiagnosticUpdateSource = hostDiagnosticUpdateSource; _projectId = projectId; _workspace = workspace; @@ -55,7 +55,9 @@ public AnalyzerReference GetReference() { if (File.Exists(_fullPath)) { - _analyzerReference = new AnalyzerFileReference(_fullPath, _loader); + // Pass down a custom loader that will ensure we are watching for file changes once we actually load the assembly. + var assemblyLoaderForFileTracker = new AnalyzerAssemblyLoaderThatEnsuresFileBeingWatched(this); + _analyzerReference = new AnalyzerFileReference(_fullPath, assemblyLoaderForFileTracker); ((AnalyzerFileReference)_analyzerReference).AnalyzerLoadFailed += OnAnalyzerLoadError; } else @@ -108,5 +110,30 @@ private void OnUpdatedOnDisk(object sender, EventArgs e) { UpdatedOnDisk?.Invoke(this, EventArgs.Empty); } + + /// + /// This custom loader just wraps an existing loader, but ensures that we start listening to the file + /// for changes once we've actually looked at the file. + /// + private class AnalyzerAssemblyLoaderThatEnsuresFileBeingWatched : IAnalyzerAssemblyLoader + { + private readonly VisualStudioAnalyzer _analyzer; + + public AnalyzerAssemblyLoaderThatEnsuresFileBeingWatched(VisualStudioAnalyzer analyzer) + { + _analyzer = analyzer; + } + + public void AddDependencyLocation(string fullPath) + { + _analyzer._loader.AddDependencyLocation(fullPath); + } + + public Assembly LoadFromPath(string fullPath) + { + _analyzer._tracker.EnsureSubscription(); + return _analyzer._loader.LoadFromPath(fullPath); + } + } } }