Skip to content

Commit 9a7cdc5

Browse files
committed
Work around Roslyn HotReload project-level analysis being disabled
dotnet/roslyn#81729 introduced AbstractEditAndContinueAnalyzer.EnableProjectLevelAnalysis (default false) and only sets it to true in the HotReloadService(HostWorkspaceServices, ...) constructor overload. dotnet-watch uses the other primary constructor (SolutionServices, Func<...>), so the flag stays false. This disables detection of project-level changes (e.g. newly added <ProjectReference>), causing hot reload to apply deltas against a stale project graph and the watched app to crash with FileNotFoundException on the missing dependency. Enable the flag via reflection until Roslyn sets it in both constructors.
1 parent 60f8828 commit 9a7cdc5

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

‎src/Dotnet.Watch/Watch/HotReload/CompilationHandler.cs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Collections.Immutable;
55
using System.Diagnostics;
6+
using System.Reflection;
67
using Microsoft.Build.Execution;
78
using Microsoft.Build.Graph;
89
using Microsoft.CodeAnalysis;
@@ -64,6 +65,19 @@ public CompilationHandler(DotNetWatchContext context)
6465
_context = context;
6566
Workspace = new HotReloadMSBuildWorkspace(context.Logger, projectFile => (instances: _projectInstances.GetValueOrDefault(projectFile, []), project: null));
6667
_hotReloadService = new HotReloadService(Workspace.CurrentSolution.Services, () => ValueTask.FromResult(GetAggregateCapabilities()));
68+
EnableRoslynProjectLevelAnalysis();
69+
}
70+
71+
// Workaround for https://github.com/dotnet/roslyn/issues/81728: Roslyn PR #81729 gated project-level
72+
// Hot Reload analysis behind a flag that is only enabled in HotReloadService(HostWorkspaceServices,
73+
// ImmutableArray<string>). dotnet-watch uses the other constructor, so we enable it via reflection.
74+
private static void EnableRoslynProjectLevelAnalysis()
75+
{
76+
const string TypeName = "Microsoft.CodeAnalysis.EditAndContinue.AbstractEditAndContinueAnalyzer";
77+
var type = AppDomain.CurrentDomain.GetAssemblies()
78+
.Select(a => a.GetType(TypeName, throwOnError: false))
79+
.FirstOrDefault(t => t != null);
80+
type?.GetField("EnableProjectLevelAnalysis", BindingFlags.NonPublic | BindingFlags.Static)?.SetValue(null, true);
6781
}
6882

6983
public void Dispose()

0 commit comments

Comments
 (0)