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

Unable to hot reload C# in .NET 9 Preview 6 #1276

Closed
davidortinau opened this issue Jul 10, 2024 · 2 comments
Closed

Unable to hot reload C# in .NET 9 Preview 6 #1276

davidortinau opened this issue Jul 10, 2024 · 2 comments
Assignees
Labels
area-hotreload Issues with Hot Reload experiences area-maui bug Something isn't working

Comments

@davidortinau
Copy link
Member

davidortinau commented Jul 10, 2024

Create a new Maui Project add the code below in app.cs
#if DEBUG
[assembly: System.Reflection.Metadata.MetadataUpdateHandlerAttribute(typeof(HangulClock.HotReloadService))]

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);
        //}
    }
}

Then in content page add the code below: 

ContentPage:
C#
public MainPage()
{
#if DEBUG
       HotReloadService.UpdateApplicationEvent += (obj) =>
       {
           MainThread.BeginInvokeOnMainThread(() =>
           {
               Build();
           });
           
       };
#endif
}

#endif

Type: Bug

My code

void Build()
	{
		Content = new VerticalStackLayout
		{
			Spacing = 20,
			Margin = 20,
			Children = {
				(label = new Label {  
					Text = "Welcome to .NET MAUI!",
					FontSize = 64,
					TextColor = Colors.Blue
				}),
				(entry = new Entry { Placeholder = "Enter your name" }),
			}
		};

		label.SetBinding(Label.TextProperty, static (Entry e) => e.Text, source: entry);
	}

If I first hit CMD+S then the output I get is:

17:45:7 💡 Possible changes were found.
17:45:9 No changes were found.

And from then on in that debug session even hitting the hot reload fire button doesn't work.

When I instead hit fire button as the first action of the debug session, then CMD+S works as well as the button. I've confirmed that several times now.

Also, previously I encountered a situation with the button not appearing. Since the "solution" was updating VS Code Insider, I'm unable to repro right now. I have experience though several times when reloading the window and restarting extensions won't fix an issue, but upon applying a pending VS Code update the issue resolves.

Testing Android, the hot reload option (fire button) wasn't appearing in the debug button list. I reloaded the window, got some error about a connecting being closed, and reloaded again... Finally I saw that VS Code Insider wanted to apply an updated, so I figured what do I have to lose. After restarting I am now able to see the button for hot reload when debugging Android, except that I cannot debug the app and there is not .NET Hot Reload output window option. It's like the debugger is hung, probably on some invisible hot reload breakpoint. When I stop debugging I see the app (was frozen on the splash). Restarting debugging does fully initialize and I can interact with the app. After hitting the hot reload button I can now see the option for the output window.

Extension version: 1.9.2
VS Code version: Code - Insiders 1.92.0-insider (b23e791eb5afbd95f05aa24da7693ce89344a079, 2024-07-09T05:03:50.549Z)
OS version: Darwin arm64 23.5.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
Load (avg) 4, 5, 4
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
dsvsc013:30777762
dsvsc014:30777825
dsvsc015:30821418
pythonregdiag2:30926734
pythonmypyd1:30859725
2e7ec940:31000449
pythontbext0:30879054
accentitlementst:30870582
dsvsc016:30879898
dsvsc017:30880771
dsvsc018:30880772
cppperfnew:30980852
pythonait:30973460
jchc7451:31067543
chatpanelt:31014475
9c06g630:31013171
a69g1124:31018687
dvdeprecation:31040973
dwnewjupytercf:31046870
nb_pkg_only:31057982
nativerepl1:31093711
refactort:31084545
pythonrstrctxt:31089940
4930e709:31088354
1iic9185:31091668

@webreidi webreidi added bug Something isn't working area-hotreload Issues with Hot Reload experiences area-maui labels Jul 11, 2024
@spadapet
Copy link
Member

spadapet commented Jul 12, 2024

@davidortinau, I couldn't repro, here's what I did:

  • Installed .NET 9 preview 6 on MacOS
  • dotnet new maui
  • Opened that new maui folder in VSCode (has pre-release extensions for C#, C# Dev Kit, and .NET MAUI)
  • Added all the code from your description
  • Build and run on Mac Catalyst
  • Made a minor code change and save file - C# hot reload detects changes and succeeds
  • Made a minor code change and click fire button - also works
  • The MetadataUpdateHandler was also triggered and built the new content

I think the description wasn't precise enough and I'm missing something. Can you attach the repro project? Or record a video?
Thanks!

@spadapet spadapet self-assigned this Aug 6, 2024
@davidortinau
Copy link
Member Author

Yes Preview 6 now works. I've also confirmed that. Preview 7 is when it doesn't work and I've detailed that issue here #1389

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 area-maui bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants