Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C# Hot Reload inconsistent and unreliable behavior #1389

Closed
davidortinau opened this issue Aug 22, 2024 · 7 comments
Closed

C# Hot Reload inconsistent and unreliable behavior #1389

davidortinau opened this issue Aug 22, 2024 · 7 comments
Labels
area-hotreload Issues with Hot Reload experiences bug Something isn't working duplicate This issue or pull request already exists external External to this extension

Comments

@davidortinau
Copy link
Member

Type: Bug

I use a MetadataUpdateHandler to direct .NET hot reloads of C# to rebuild UI in my project. In order to do this I need to look at the Type[] types being updated in the UpdateApplication method, however they are empty in .NET 9 Preview 7. This was working fine previously.

I tested iOS, Mac Catalyst, and Android.

#if DEBUG
[assembly: System.Reflection.Metadata.MetadataUpdateHandler(typeof(ControlGallery.Services.HotReloadService))]
namespace ControlGallery.Services
{
    public static class HotReloadService
    {
#pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
        public static event Action<Type[]?>? UpdateApplicationEvent;
#pragma warning restore CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

        internal static void ClearCache(Type[]? types) { }
        internal static void UpdateApplication(Type[]? types)
        {
            if ((types != null && types.Length > 0 && types[0] != null))
            {
                UpdateApplicationEvent?.Invoke(types);
            }
        }
    }
}
#endif

Extension version: 1.10.6
VS Code version: Code - Insiders 1.93.0-insider (e2b54301a5745870f6b95d81c91fb3e9557d4f08, 2024-08-20T08:04:15.567Z)
OS version: Darwin arm64 23.6.0
Modes:

System Info
Item Value
CPUs Apple M1 Pro (8 x 2400)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: enabled_on
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
video_decode: enabled
video_encode: enabled
webgl: enabled
webgl2: enabled
webgpu: enabled
webnn: disabled_off
Load (avg) 2, 3, 3
Memory (System) 16.00GB (0.07GB free)
Process Argv --crash-reporter-id fc80353a-ef2a-479f-83ff-be5b061d3542
Screen Reader no
VM 0%
A/B Experiments
vsliv368:30146709
vspor879:30202332
vspor708:30202333
vspor363:30204092
vsc_aacf:30263846
vscod805:30301674
vsaa593:30376534
py29gd2263:31024238
c4g48928:30535728
962ge761:30841072
pythongtdpath:30726887
welcomedialog:30812478
pythonnoceb:30776497
asynctok:30898717
dsvsc014:30777825
dsvsc015:30821418
pythonregdiag2:30926734
pythonmypyd1:30859725
2e7ec940:31000449
pythontbext0:30879054
accentitlementst:30870582
dsvsc016:30879898
dsvsc017:30880771
dsvsc018:30880772
cppperfnew:30980852
pythonait:30973460
9c06g630:31013171
a69g1124:31018687
dvdeprecation:31040973
dwnewjupytercf:31046870
nb_pkg_only:31057982
nativerepl1:31104042
refactort:31084545
pythonrstrctxt:31093868
flighttreat:31119334
wkspc-onlycs-t:31111718
nativeloc1:31118317
wkspc-ranged-t:31118572
cf971741:31111988
93e6f237:31111927
e80f6927:31120813
ei213698:31121563
i21gd607:31121876

@davidortinau
Copy link
Member Author

Doing more troubleshooting I'm finding it's now working again, but I've been through a few bumps. Why is this so inconsistent and fragile?

  • I've confirmed .NET 9 Preview 6 (9.0.100-preview.6.24328.19) is working. I did update the extensions and VS Code Insider to the versions below as well.
  • I reverted then back to 9.0.100-preview.7.24407.12, deleted bin/obj, and got errors
2024-08-22 12:02:48.047 [error]  /Users/davidortinau/work/SentenceStudio/src/SentenceStudio/SentenceStudio.csproj (line 1): error CS7038: Failed to emit module 'SentenceStudio': Changing the version of an assembly reference is not allowed during debugging: 'Microsoft.iOS, Version=17.5.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065' changed version to '17.2.0.0'.
2024-08-22 12:02:48.048 [info]  Invalid changes were found. Please refer to the Error List window to fix those issues.

Not sure what that's all about.

  • Cleaned bin/obj again and then restarted VS Code. Now it's working as expected.
2024-08-22 12:08:18.136 [info]  💡 Possible changes were found.
2024-08-22 12:08:18.819 [info]  Code changes were successfully applied.

Extension version: 1.10.10
VS Code version: Code - Insiders 1.93.0-insider (e1db90f81da28c0994b5d50dc2b51e964359e62d, 2024-08-21T05:04:11.586Z)
OS version: Darwin arm64 23.6.0
Modes:

System Info
Item Value
CPUs Apple M1 Pro (8 x 2400)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: enabled_on
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
video_decode: enabled
video_encode: enabled
webgl: enabled
webgl2: enabled
webgpu: enabled
webnn: disabled_off
Load (avg) 6, 7, 8
Memory (System) 16.00GB (0.05GB free)
Process Argv --crash-reporter-id fc80353a-ef2a-479f-83ff-be5b061d3542
Screen Reader no
VM 0%
A/B Experiments
vsliv368:30146709
vspor879:30202332
vspor708:30202333
vspor363:30204092
vsc_aacf:30263846
vscod805:30301674
vsaa593:30376534
py29gd2263:31024238
c4g48928:30535728
962ge761:30841072
pythongtdpath:30726887
welcomedialog:30812478
pythonnoceb:30776497
asynctok:30898717
dsvsc014:30777825
dsvsc015:30821418
pythonregdiag2:30926734
pythonmypyd1:30859725
2e7ec940:31000449
pythontbext0:30879054
accentitlementst:30870582
dsvsc016:30879898
dsvsc017:30880771
dsvsc018:30880772
cppperfnew:30980852
pythonait:30973460
9c06g630:31013171
a69g1124:31018687
dvdeprecation:31040973
dwnewjupytercf:31046870
nb_pkg_only:31057982
nativerepl1:31104042
refactort:31084545
pythonrstrctxt:31093868
flighttreat:31119334
wkspc-onlycs-t:31111718
nativeloc1:31118317
cf971741:31111988
93e6f237:31111927
e80f6927:31120813
ei213698:31121563
i21gd607:31121876

@davidortinau davidortinau changed the title C# hot reload missing types C# Hot Reload inconsistent and unreliable behavior Aug 22, 2024
@AbhitejJohn AbhitejJohn added the area-hotreload Issues with Hot Reload experiences label Aug 22, 2024
@spadapet
Copy link
Member

spadapet commented Aug 23, 2024

I tried that MetadataUpdateHandler on Windows and it works. I used:

  • VSCode Insiders
  • .NET 9.0.100-preview.7.24407.12
  • Pre-release of all extensions (.NET MAUI, C# DevKit, C#)
  • Debug Android emulator

I'm trying the same config on Mac now

@spadapet
Copy link
Member

The MetadataUpdateHandler works well on my Mac too, using the same setup:

  • VSCode Insiders
  • .NET 9.0.100-preview.7.24407.12
  • Pre-release of all extensions (.NET MAUI, C# DevKit, C#)
  • Debug Android emulator

Not sure what can be done here without a consistent repro.

@davidortinau
Copy link
Member Author

Is there logging that could be added to trap when things occur like this?

@spadapet
Copy link
Member

spadapet commented Sep 3, 2024

Maybe a diagnostic-level log can be added when UpdateApplication is called, and it could list out the changed Types. You can also add debug output when your methods are called.

Here's what's currently in the output window when "diagnostic" logs are enabled:

image

image

That's the VS option, but VSCode has the same option.

Would it help if there was new diagnostic output that said:

  • Calling MetadataUpdateHandlers with the following changed Types: MyApp.MainPage, MyApp.SecondPage, Foo.Bar

@webreidi webreidi added the bug Something isn't working label Sep 4, 2024
@davidortinau
Copy link
Member Author

davidortinau commented Sep 10, 2024

Now on v1.11.6 of DevKit.

I turned on diagnostic. Things were going well, until I noticed IntelliSense disappeared and then on the next debug the hot reload icon no longer appears when debugging.

The last 2 hot reloads didn't get caught and handled by my metadataupdatehandler, but the output said "Code changes were successfully applied."

Maybe this is all related. #1450 #1448

This is what I have in the output:

2024-09-10 17:43:24.112 [info]  Starting session.
2024-09-10 17:45:54.006 [info]  💡 Possible changes were found.
2024-09-10 17:46:15.237 [info]  Checking for code updates...
2024-09-10 17:46:15.683 [info]  Applying code updates...
2024-09-10 17:46:15.773 [info]  Code changes were successfully applied.
2024-09-10 17:47:56.137 [info]  💡 Possible changes were found.
2024-09-10 17:48:04.395 [info]  Checking for code updates...
2024-09-10 17:48:04.520 [info]  Applying code updates...
2024-09-10 17:48:04.546 [info]  Code changes were successfully applied.
2024-09-10 17:48:23.455 [info]  💡 Possible changes were found.
2024-09-10 17:48:27.401 [info]  Checking for code updates...
2024-09-10 17:48:27.458 [info]  Applying code updates...
2024-09-10 17:48:27.471 [info]  Code changes were successfully applied.
2024-09-10 17:50:17.824 [info]  💡 Possible changes were found.
2024-09-10 17:52:10.624 [info]  Checking for code updates...
2024-09-10 17:52:10.757 [info]  Applying code updates...
2024-09-10 17:52:10.822 [info]  Code changes were successfully applied.
2024-09-10 17:52:57.322 [info]  💡 Possible changes were found.
2024-09-10 17:53:01.604 [info]  Checking for code updates...
2024-09-10 17:53:01.687 [info]  Applying code updates...
2024-09-10 17:53:01.705 [info]  Code changes were successfully applied.
2024-09-10 17:53:21.365 [info]  💡 Possible changes were found.
2024-09-10 17:53:23.839 [info]  Checking for code updates...
2024-09-10 17:53:23.908 [info]  Applying code updates...
2024-09-10 17:53:23.928 [info]  Code changes were successfully applied.
2024-09-10 17:53:38.807 [info]  💡 Possible changes were found.
2024-09-10 17:53:43.550 [info]  Checking for code updates...
2024-09-10 17:53:43.636 [info]  Applying code updates...
2024-09-10 17:53:43.651 [info]  Code changes were successfully applied.
2024-09-10 17:53:59.412 [info]  💡 Possible changes were found.
2024-09-10 17:54:07.481 [info]  Checking for code updates...
2024-09-10 17:54:07.553 [info]  Applying code updates...
2024-09-10 17:54:07.569 [info]  Code changes were successfully applied.
2024-09-10 17:54:42.955 [info]  💡 Possible changes were found.
2024-09-10 17:54:47.822 [info]  Checking for code updates...
2024-09-10 17:54:47.886 [info]  Applying code updates...
2024-09-10 17:54:47.909 [info]  Code changes were successfully applied.
2024-09-10 17:55:40.089 [info]  💡 Possible changes were found.
2024-09-10 17:55:45.050 [info]  Checking for code updates...
2024-09-10 17:55:45.118 [info]  Applying code updates...
2024-09-10 17:55:45.144 [info]  Code changes were successfully applied.
2024-09-10 17:58:39.016 [info]  💡 Possible changes were found.
2024-09-10 17:58:48.699 [info]  Checking for code updates...
2024-09-10 17:58:48.784 [info]  Applying code updates...
2024-09-10 17:58:48.810 [info]  Code changes were successfully applied.
2024-09-10 18:02:45.213 [info]  💡 Possible changes were found.
2024-09-10 18:03:09.696 [info]  Checking for code updates...
2024-09-10 18:03:09.763 [info]  Applying code updates...
2024-09-10 18:03:09.790 [info]  Code changes were successfully applied.
2024-09-10 18:05:50.270 [info]  Checking for code updates...
2024-09-10 18:05:50.281 [info]  No code changes were found.
2024-09-10 18:05:50.289 [info]  Ending session.
2024-09-10 18:06:56.443 [info]  Starting session.
2024-09-10 18:08:48.134 [info]  💡 Possible changes were found.
2024-09-10 18:08:49.854 [info]  Checking for code updates...
2024-09-10 18:08:50.101 [info]  Applying code updates...
2024-09-10 18:08:50.170 [info]  Code changes were successfully applied.
2024-09-10 18:08:56.715 [info]  💡 Possible changes were found.
2024-09-10 18:09:24.040 [info]  Checking for code updates...
2024-09-10 18:09:24.104 [info]  Applying code updates...
2024-09-10 18:09:24.142 [info]  Code changes were successfully applied.
2024-09-10 18:10:42.065 [info]  Ending session.
2024-09-10 18:11:48.168 [info]  Starting session.
2024-09-10 18:11:48.169 [info]  Exception thrown for Session event: 0x80131500 - The JSON-RPC connection with the remote party was lost before the request could complete.
2024-09-10 18:11:48.169 [info]  Stack trace for ConnectionLostException:
   at StreamJsonRpc.JsonRpc.InvokeCoreAsync(JsonRpcRequest request, Type expectedResultType, CancellationToken cancellationToken)
   at StreamJsonRpc.JsonRpc.InvokeCoreAsync[TResult](RequestId id, String targetName, IReadOnlyList`1 arguments, IReadOnlyList`1 positionalArgumentDeclaredTypes, IReadOnlyDictionary`2 namedArgumentDeclaredTypes, CancellationToken cancellationToken, Boolean isParameterObject)
   at Microsoft.VisualStudio.Debugger.Shared.Utilities.LoggerExtensions.LogOnExceptionAsync(ValueTask this, LoggerInformation loggerInformation, Boolean propagateException)
2024-09-10 18:11:48.247 [info]  Exception thrown for Session event: 0x80131500 - The JSON-RPC connection with the remote party was lost before the request could complete.
2024-09-10 18:11:48.247 [info]  Stack trace for ConnectionLostException:
   at StreamJsonRpc.JsonRpc.InvokeCoreAsync(JsonRpcRequest request, Type expectedResultType, CancellationToken cancellationToken)
   at StreamJsonRpc.JsonRpc.InvokeCoreAsync[TResult](RequestId id, String targetName, IReadOnlyList`1 arguments, IReadOnlyList`1 positionalArgumentDeclaredTypes, IReadOnlyDictionary`2 namedArgumentDeclaredTypes, CancellationToken cancellationToken, Boolean isParameterObject)
   at Microsoft.VisualStudio.Debugger.Shared.Utilities.LoggerExtensions.LogOnExceptionAsync(ValueTask this, LoggerInformation loggerInformation, Boolean propagateException)
2024-09-10 18:11:48.249 [info]  Exception thrown for Apply code updates: 0x80131500 - The JSON-RPC connection with the remote party was lost before the request could complete.
2024-09-10 18:11:48.249 [info]  Stack trace for ConnectionLostException:
   at StreamJsonRpc.JsonRpc.InvokeCoreAsync(JsonRpcRequest request, Type expectedResultType, CancellationToken cancellationToken)
   at StreamJsonRpc.JsonRpc.InvokeCoreAsync[TResult](RequestId id, String targetName, IReadOnlyList`1 arguments, IReadOnlyList`1 positionalArgumentDeclaredTypes, IReadOnlyDictionary`2 namedArgumentDeclaredTypes, CancellationToken cancellationToken, Boolean isParameterObject)
   at Microsoft.VisualStudio.Debugger.Shared.Utilities.LoggerExtensions.LogOnExceptionAsync(ValueTask this, LoggerInformation loggerInformation, Boolean propagateException)
   at Microsoft.VisualStudio.Debugger.Shared.HotReload.LanguageServiceNotificationService.<>c__DisplayClass17_0.<<NotifySessionChangedAsync>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at Microsoft.VisualStudio.Debugger.Shared.HotReload.LanguageServiceNotificationService.<>c__DisplayClass19_0.<<NotifyListenersAsync>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at Microsoft.VisualStudio.Debugger.Shared.HotReload.LanguageServiceNotificationService.NotifyListenersAsync(Func`2 notification, CancellationToken cancellation)
   at Microsoft.VisualStudio.Debugger.Shared.HotReload.LanguageServiceNotificationService.NotifySessionChangedAsync(HotReloadSessionChangeInfo info, CancellationToken cancellation)
   at Microsoft.VisualStudio.Debugger.Shared.Utilities.LoggerExtensions.LogOnExceptionAsync(ValueTask this, LoggerInformation loggerInformation, Boolean propagateException)
2024-09-10 18:11:57.735 [info]  Exception thrown for Session event: 0x80131500 - The JSON-RPC connection with the remote party was lost before the request could complete.
2024-09-10 18:11:57.735 [info]  Stack trace for ConnectionLostException:
   at StreamJsonRpc.JsonRpc.InvokeCoreAsync(JsonRpcRequest request, Type expectedResultType, CancellationToken cancellationToken)
   at StreamJsonRpc.JsonRpc.InvokeCoreAsync[TResult](RequestId id, String targetName, IReadOnlyList`1 arguments, IReadOnlyList`1 positionalArgumentDeclaredTypes, IReadOnlyDictionary`2 namedArgumentDeclaredTypes, CancellationToken cancellationToken, Boolean isParameterObject)
   at Microsoft.VisualStudio.Debugger.Shared.Utilities.LoggerExtensions.LogOnExceptionAsync(ValueTask this, LoggerInformation loggerInformation, Boolean propagateException)

@spadapet
Copy link
Member

Based on that last comment, I'll close this as a duplicate of: dotnet/vscode-csharp#7514

@spadapet spadapet added duplicate This issue or pull request already exists external External to this extension labels Sep 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-hotreload Issues with Hot Reload experiences bug Something isn't working duplicate This issue or pull request already exists external External to this extension
Projects
None yet
Development

No branches or pull requests

4 participants