Skip to content

Commit 51ed186

Browse files
authored
Catch dotnet watch task cancellation (#23316) (#23386)
(cherry picked from commit abaa783)
1 parent 0f849fa commit 51ed186

File tree

1 file changed

+32
-33
lines changed

1 file changed

+32
-33
lines changed

src/BuiltInTools/dotnet-watch/HotReload/CompilationWorkspaceProvider.cs

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,7 @@ internal static class CompilationWorkspaceProvider
2424
CancellationToken cancellationToken)
2525
{
2626
var taskCompletionSource = new TaskCompletionSource<(Solution, WatchHotReloadService)>(TaskCreationOptions.RunContinuationsAsynchronously);
27-
try
28-
{
29-
CreateProject(taskCompletionSource, hotReloadCapabilitiesTask, projectPath, reporter, cancellationToken);
30-
}
31-
catch (Exception ex)
32-
{
33-
taskCompletionSource.TrySetException(ex);
34-
}
35-
27+
CreateProject(taskCompletionSource, hotReloadCapabilitiesTask, projectPath, reporter, cancellationToken);
3628
return taskCompletionSource.Task;
3729
}
3830

@@ -43,39 +35,46 @@ static async void CreateProject(
4335
IReporter reporter,
4436
CancellationToken cancellationToken)
4537
{
46-
var workspace = MSBuildWorkspace.Create();
47-
48-
workspace.WorkspaceFailed += (_sender, diag) =>
38+
try
4939
{
50-
if (diag.Diagnostic.Kind == WorkspaceDiagnosticKind.Warning)
51-
{
52-
reporter.Verbose($"MSBuildWorkspace warning: {diag.Diagnostic}");
53-
}
54-
else
40+
var workspace = MSBuildWorkspace.Create();
41+
42+
workspace.WorkspaceFailed += (_sender, diag) =>
5543
{
56-
taskCompletionSource.TrySetException(new InvalidOperationException($"Failed to create MSBuildWorkspace: {diag.Diagnostic}"));
57-
}
58-
};
44+
if (diag.Diagnostic.Kind == WorkspaceDiagnosticKind.Warning)
45+
{
46+
reporter.Verbose($"MSBuildWorkspace warning: {diag.Diagnostic}");
47+
}
48+
else
49+
{
50+
taskCompletionSource.TrySetException(new InvalidOperationException($"Failed to create MSBuildWorkspace: {diag.Diagnostic}"));
51+
}
52+
};
5953

60-
await workspace.OpenProjectAsync(projectPath, cancellationToken: cancellationToken);
61-
var currentSolution = workspace.CurrentSolution;
54+
await workspace.OpenProjectAsync(projectPath, cancellationToken: cancellationToken);
55+
var currentSolution = workspace.CurrentSolution;
6256

63-
var hotReloadCapabilities = await GetHotReloadCapabilitiesAsync(hotReloadCapabilitiesTask, reporter);
64-
var hotReloadService = new WatchHotReloadService(workspace.Services, await hotReloadCapabilitiesTask);
57+
var hotReloadCapabilities = await GetHotReloadCapabilitiesAsync(hotReloadCapabilitiesTask, reporter);
58+
var hotReloadService = new WatchHotReloadService(workspace.Services, await hotReloadCapabilitiesTask);
6559

66-
await hotReloadService.StartSessionAsync(currentSolution, cancellationToken);
60+
await hotReloadService.StartSessionAsync(currentSolution, cancellationToken);
6761

68-
// Read the documents to memory
69-
await Task.WhenAll(
70-
currentSolution.Projects.SelectMany(p => p.Documents.Concat(p.AdditionalDocuments)).Select(d => d.GetTextAsync(cancellationToken)));
62+
// Read the documents to memory
63+
await Task.WhenAll(
64+
currentSolution.Projects.SelectMany(p => p.Documents.Concat(p.AdditionalDocuments)).Select(d => d.GetTextAsync(cancellationToken)));
65+
66+
// Warm up the compilation. This would help make the deltas for first edit appear much more quickly
67+
foreach (var project in currentSolution.Projects)
68+
{
69+
await project.GetCompilationAsync(cancellationToken);
70+
}
7171

72-
// Warm up the compilation. This would help make the deltas for first edit appear much more quickly
73-
foreach (var project in currentSolution.Projects)
72+
taskCompletionSource.TrySetResult((currentSolution, hotReloadService));
73+
}
74+
catch (Exception ex)
7475
{
75-
await project.GetCompilationAsync(cancellationToken);
76+
taskCompletionSource.TrySetException(ex);
7677
}
77-
78-
taskCompletionSource.TrySetResult((currentSolution, hotReloadService));
7978
}
8079

8180
private static async Task<ImmutableArray<string>> GetHotReloadCapabilitiesAsync(Task<ImmutableArray<string>> hotReloadCapabilitiesTask, IReporter reporter)

0 commit comments

Comments
 (0)