dotnet watch stops detecting changes, crashes and corrupts terminal
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 281
Description
### Description
I have a solution consisting of 3 projects:
* Secret.Api (Asp.NET webapi running on https://localhost:5001)
* Secret.Shared (classlib)
* Secret.WebApp (blazorwasm hosted by Secret.Api)
I develop running `dotnet watch run` running in a terminal next to my editor.
Sometimes when I change files (does not matter of which project), the dotnet watch process hangs up.
The last output often looks something like this:
```
watch : Files changed: /path/to/repo/Secret/Secret.Api/SomeThing/SomeClass.cs, /path/to/repo/Secret.Api/Something/4913
```
The second file with the 4 digit numeric filename is only displayed when I use Vim I think, but the hangup happens independently of the editor. I think its filename is always 4913 and it apparently comes from the same directory as the file that got changed.
When I hit Ctrl-C, this gets printed:
```
Unhandled exception. System.Threading.Tasks.TaskCanceledException: A task was canceled.
at Microsoft.DotNet.Watcher.Tools.BlazorWebAssemblyDeltaApplier.<>c__DisplayClass6_0.<g__GetApplyUpdateCapabilitiesCoreAsync|0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.DotNet.Watcher.Tools.BlazorWebAssemblyHostedDeltaApplier.GetApplyUpdateCapabilitiesAsync(DotNetWatchContext context, CancellationToken cancellationToken)
at Microsoft.DotNet.Watcher.Tools.CompilationWorkspaceProvider.CreateProject(TaskCompletionSource`1 taskCompletionSource, Task`1 hotReloadCapabilitiesTask, String projectPath, IReporter reporter, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.<>c.b__127_1(Object state)
at System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()
at System.Threading.Thread.StartCallback()
```
Typing "reset" (while no characters are appearing in the console) fixes the broken terminal problem, but it happens quite frequently. Sometimes it happens many times in a row, and at other times I can make ~5 changes without it hanging up.
### Configuration
dotnet 6.0.100-rc.1.21463.6
Tested with Linux Mint 20.2, Debian 12 (bookworm, testing), Windows 10, MacBook pro 2017 using Vim, VS Code, VS 2022 Preview
All are x64
Since this is a blazor project: This happens independently of the browser (Firefox or Chromium).
### Regression?
Since dotnet 5 did not have hot-reload capabilities, these problems did not exist back when we were using dotnet 5.
When I create a new blazorwasm project using `dotnet new blazorwasm -o TestProject` hot reloading and everything works as expected.
I have rewritten `Properties/launchSettings.json`, copied it over from a freshly created blazor WASM project because my only guess was the problems originated from there.
Also I tried to set hotReloadProfile in launchSettings.json to any possible value I could find in the web, used the profile explicitly with `dotnet watch run --profile ...`, but none of that had an effect.
### Other information
When `dotnet watch run` detects a change and rebuilds the backend, the blazor WASM application does not reload at all.
The blazor error UI appears at the bottom of the page and this exception gets printed to the browser console:
```
Error: System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.AspNetCore.Components.WebAssembly.HotReload.WebAssemblyHotReload.ApplyHotReloadDelta(String moduleIdString, Byte[] metadataDelta, Byte[] ilDeta)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
--- End of stack trace from previous location ---
at Microsoft.JSInterop.Infrastructure.DotNetDispatcher.InvokeSynchronously(JSRuntime jsRuntime, DotNetInvocationInfo& callInfo, IDotNetObjectReference objectReference, String argsJson)
at Microsoft.JSInterop.Infrastructure.DotNetDispatcher.Invoke(JSRuntime jsRuntime, DotNetInvocationInfo& invocationInfo, String argsJson)
at Microsoft.AspNetCore.Components.WebAssembly.Services.DefaultWebAssemblyJSRuntime.InvokeDotNet(String assemblyName, String methodIdentifier, String dotNetObjectId, String argsJson)
_convert_exception_for_method_call https://localhost:5001/_framework/dotnet.6.0.0-rc.1.21451.13.js:1
_handle_exception_for_call https://localhost:5001/_framework/dotnet.6.0.0-rc.1.21451.13.js:1
managed__Microsoft_AspNetCore_Components_WebAssembly__Microsoft_AspNetCore_Components_WebAssembly_Services_DefaultWebAssemblyJSRuntime_InvokeDotNet https://mono-wasm.invalid/managed__Microsoft_AspNetCore_Components_WebAssembly__Microsoft_AspNetCore_Components_WebAssembly_Services_DefaultWebAssemblyJSRuntime_InvokeDotNet:20
invokeDotNetFromJS https://localhost:5001/_framework/blazor.webassembly.js:1
g https://localhost:5001/_framework/blazor.webassembly.js:1
invokeMethod https://localhost:5001/_framework/blazor.webassembly.js:1
applyHotReload https://localhost:5001/_framework/blazor.webassembly.js:1
applyBlazorDeltas https://localhost:5001/_framework/aspnetcore-browser-refresh.js:122
applyBlazorDeltas https://localhost:5001/_framework/aspnetcore-browser-refresh.js:120
BlazorHotReloadDeltav1 https://localhost:5001/_framework/aspnetcore-browser-refresh.js:38
onmessage
```
### Edit:
I should mention that I am running `dotnet watch run` in the directory of Secret.Api (which is hosting Secret.WebApp).
When running `dotnet watch run` in Secret.WebApp (which shows some CORS errors for obvious reasons but they don't matter so far for testing), the page at least reloads when a razor page or component is modified.
But still no hot reload and still throwing exceptions.
This gets then printed to the browser console (now it is a index out of bounds error, not NullReferenceException):
```
RuntimeError: index out of bounds
_mono_wasm_invoke_method https://localhost:7233/_framework/dotnet.6.0.0-rc.1.21451.13.js:1
managed__Microsoft_AspNetCore_Components_WebAssembly__Microsoft_AspNetCore_Components_WebAssembly_Services_DefaultWebAssemblyJSRuntime_InvokeDotNet https://mono-wasm.invalid/managed__Microsoft_AspNetCore_Components_WebAssembly__Microsoft_AspNetCore_Components_WebAssembly_Services_DefaultWebAssemblyJSRuntime_InvokeDotNet:19
invokeDotNetFromJS https://localhost:7233/_framework/blazor.webassembly.js:1
g https://localhost:7233/_framework/blazor.webassembly.js:1
invokeMethod https://localhost:7233/_framework/blazor.webassembly.js:1
applyHotReload https://localhost:7233/_framework/blazor.webassembly.js:1
applyBlazorDeltas https://localhost:7233/_framework/aspnetcore-browser-refresh.js:122
applyBlazorDeltas https://localhost:7233/_framework/aspnetcore-browser-refresh.js:120
BlazorHotReloadDeltav1 https://localhost:7233/_framework/aspnetcore-browser-refresh.js:38
onmessage
```
Before any error is shown, this gets printed to the browser console:
```
Firefox can’t establish a connection to the server at wss://localhost:42689/.
```
Where the port in the connection error seems to be random.
Immediately following this:
```
WebSocket failed to connect.
```
Contributor guide
Assessment
This issue has not been assessed yet.