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

CSS hot reload fails in .NET MAUI Blazor app after 2nd edit #9197

Closed
danroth27 opened this issue Aug 4, 2022 · 19 comments · Fixed by #9645
Closed

CSS hot reload fails in .NET MAUI Blazor app after 2nd edit #9197

danroth27 opened this issue Aug 4, 2022 · 19 comments · Fixed by #9645
Assignees
Labels
area-blazor Blazor Hybrid / Desktop, BlazorWebView discussed Created by mkArtakMSFT to help with planning temporarily. It will be removed after planning is done. fixed-in-6.0.540 Look for this fix in 6.0.540 SR5! fixed-in-7.0.0-rc.1.6683 t/bug Something isn't working

Comments

@danroth27
Copy link
Member

danroth27 commented Aug 4, 2022

Repro steps:

  • VS: 17.3 P6
  • Create a new .NET MAUI Blazor app
  • Edit app.css
  • Save to hot reload the change
  • Edit app.css again

Expected result: 2nd edit is successfully applied

Actual result:

  • No UI update
  • Error in Hot Reload output window: "Applying changes to app.css failed. The process cannot access the file 'C:\Users\user\source\repos\MauiApp1\MauiApp1\wwwroot\css\app.css' because it is being used by another process."

Process Explorer shows that the app is holding onto the file:
image

@TanayParikh @mkArtakMSFT @mcumming

This issue is similar to the CSS hot reload issue with WinForms, but the behavior is different and the way Blazor handles the file stream is different on .NET MAUI which should avoid the WebView2 locking issue. In this case it seems like Blazor or .NET MAUI may be the culprit.

@danroth27 danroth27 changed the title CSS hot reload fails after 2nd edit CSS hot reload fails in .NET MAUI Blazor app after 2nd edit Aug 4, 2022
@jfversluis jfversluis added t/bug Something isn't working area-blazor Blazor Hybrid / Desktop, BlazorWebView labels Aug 4, 2022
@TanayParikh
Copy link
Contributor

This may just require closing out the content stream after it's been copied:

// NOTE: This is stream copying is to work around a hanging bug in WinRT with managed streams.
// See issue https://github.com/microsoft/CsWinRT/issues/670
var memStream = new MemoryStream();
content.CopyTo(memStream);
var ms = new InMemoryRandomAccessStream();
await ms.WriteAsync(memStream.GetWindowsRuntimeBuffer());

@TanayParikh TanayParikh self-assigned this Aug 4, 2022
@TanayParikh
Copy link
Contributor

@danroth27 I'm unable to repro this, did you have any other steps you'd recommend trying?

image

@TanayParikh
Copy link
Contributor

I ended up being able to repro using a clean maui-blazor app. Above screenshot is using the MAUI sample app in the maui repo.

I think this'll be resolved if we can resolve #9206, at which point, we'll likely end up running into MicrosoftEdge/WebView2Feedback#2513 as we'd no longer be copying the content stream into a separate memory stream. Not the ideal resolution, but it would be good to get these related but different issues into the same "base" issue.

@TanayParikh
Copy link
Contributor

#9254 has been merged. Once the builds for that are available, it would be good to revalidate this.

@TanayParikh TanayParikh modified the milestone: 7.0-rc2 Aug 8, 2022
@TanayParikh
Copy link
Contributor

it would be good to revalidate this.

Things should've now propagated. I've requested revalidation for this scenario using the latest (internal) builds.

@anirugu
Copy link

anirugu commented Aug 16, 2022

I tried to build a project in maui-blazor in 17.4 Preview 1. Changing CSS doesn't reflect in the running app. I am not sure if it's happening after the 2nd exit.

This is the stack track I receive from VS, I am running it in Windows machine (Same machine where VS is running)

StreamJsonRpc.RemoteInvocationException: The solution does not contain the specified document.
at StreamJsonRpc.JsonRpc.d__1431.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Threading.Tasks.ValueTask1.get_Result()
at Microsoft.CodeAnalysis.Remote.BrokeredServiceConnection1.<TryInvokeAsync>d__201.MoveNext()
RPC server exception:
System.InvalidOperationException: The solution does not contain the specified document.
at Microsoft.CodeAnalysis.Shared.Extensions.ISolutionExtensions.d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.CodeAnalysis.EditAndContinue.RemoteEditAndContinueService.<>c__DisplayClass9_0.<b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Threading.Tasks.ValueTask1.get_Result() at Microsoft.CodeAnalysis.Remote.RemoteWorkspace.<>c__DisplayClass10_01.<g__TryFastGetSolutionAndRunAsync|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Threading.Tasks.ValueTask1.get_Result() at Microsoft.CodeAnalysis.Remote.RemoteWorkspace.<RunWithSolutionAsync>d__101.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Threading.Tasks.ValueTask1.get_Result() at Microsoft.CodeAnalysis.Remote.BrokeredServiceBase.<RunWithSolutionAsync>d__111.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Threading.Tasks.ValueTask1.get_Result() at Microsoft.CodeAnalysis.Remote.BrokeredServiceBase.<RunServiceImplAsync>d__141.MoveNext()

@TanayParikh
Copy link
Contributor

@anirugu that appears to be a different issue. The issue we're discussing here doesn't explicitly throw an exception as far as I know. Based on the stack trace you provided, I suspect you may be running into dotnet/sdk#26762 which we're in the middle of patching.

@TanayParikh
Copy link
Contributor

it would be good to revalidate this.

Things should've now propagated. I've requested revalidation for this scenario using the latest (internal) builds.

Unfortunately, the issue seems to continue to reproduce despite the recent changes.

fyi/ @javiercn

@TanayParikh TanayParikh added this to the 7.0-rc2 milestone Aug 16, 2022
@TanayParikh
Copy link
Contributor

Adding to RC2 milestone.

@TanayParikh
Copy link
Contributor

@javiercn could this have any relation to dotnet/sdk#26762 / dotnet/sdk#26765 / dotnet/sdk#27102?

@danroth27 just confirming your issue here was on .NET 6?

@TanayParikh
Copy link
Contributor

Reopening as @jinzhao1127 is still repro'ing this issue. #9645 should've resolved this. Specifically:

https://github.com/dotnet/maui/pull/9645/files#diff-5cf51db2a43b59a7b8ce04f82cc5e21e2e57a2eeba4b02b5aae132f1dc086820R70

@TanayParikh TanayParikh reopened this Aug 31, 2022
@TanayParikh
Copy link
Contributor

I haven't been able to repro this given I'm running into https://github.com/aspnet/AspNetCore-ManualTests/issues/1525 first. Will have to hold off on further investigation here till https://github.com/aspnet/AspNetCore-ManualTests/issues/1525 is resolved.

@mkArtakMSFT mkArtakMSFT modified the milestones: 7.0-rc2, 7.0 Sep 19, 2022
@TanayParikh
Copy link
Contributor

@mcumming pointed out the following discrepency:

image

vs. in the VS Hot reload output:

14:01 46.51 MauiApp14: [Warning] Endpoint has unsupported platform WinUI for .NET Hot Reload, skipping.
14:01 46.57 MauiApp14: Hot Reload session started
14:01 55.73 MauiApp14: Applying the changes to app.css to the running application
14:02 00.63 MauiApp14: Applying the changes to app.css to the running application
14:02 03.57 MauiApp14: Applying changes to app.css failed. The process cannot access the file 'C:\Users\taparik\source\repos\MauiApp14\MauiApp14\wwwroot\css\app.css' because it is being used by another process.

Notice the difference in path.

@TanayParikh
Copy link
Contributor

Making this change:

image

here:

if (_isPackagedApp)
{
var winUIItem = await Package.Current.InstalledLocation.TryGetItemAsync(relativePath);
if (winUIItem != null)
{
var contentStream = await Package.Current.InstalledLocation.OpenStreamForReadAsync(relativePath);
stream = contentStream.AsRandomAccessStream();
}
}

results in Process Explorer reporting no processes holding the app.css handle:

image

However, hot reload still fails on:

16:27 36.93 MauiApp14: Applying changes to app.css failed. The process cannot access the file 'C:\Users\taparik\source\repos\MauiApp14\MauiApp14\wwwroot\css\app.css' because it is being used by another process.

Given this is a distinctly different file from what was being reported by Process Explorer (see above post), I think we may actually have two separate bugs here. One would be resolved by the code change above, while there appears to be another bug that's resulting in the underlying failed hot reload issue persisting.

Note, @MackinnonBuck was able to confirm, it does in fact repro on Android (thanks Mackinnon!). So this definitely doesn't appear to be platform specific.

@mcumming why is this reading from the project folder, whereas we're performing hot reload from the packaged app (screenshot below). (cc/ @javiercn as I believe he may have worked with packaging in this context).

image

@mcumming
Copy link
Contributor

@TanayParikh For MAUI we read from the project folder because that is the file that the user is modifying. The instance of that file in the packaged folder is not updated that I know of after the initial build. That is why for content files HotReload sends the entire contents of the file to the agent in the running application.

@TanayParikh
Copy link
Contributor

Thanks for clarifying. What's unclear in that case is what is actually holding on to the file handle. Nothing shows up in process explorer.

Did you have a chance to experiment with alternatives to the current File.ReadAllBytes in UITools? Perhaps we could try to open a file stream (with file sharing explicitly configured) and then copy to a memory stream to see if that potentially results in different behavior?

@mcumming
Copy link
Contributor

Yes, I switched the File.ReadAllBytes to explicitly openening the file with using var stream = new (fullPathToCssFile, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);, unfortunately it hasn't had an effect on this issue.

@TanayParikh
Copy link
Contributor

We're trying out a temporary mitigation of this within VS at the moment.

@Redth Redth modified the milestones: 7.0, .NET 7 + Servicing Oct 31, 2022
@mkArtakMSFT mkArtakMSFT added the discussed Created by mkArtakMSFT to help with planning temporarily. It will be removed after planning is done. label Nov 2, 2022
@TanayParikh TanayParikh assigned mcumming and unassigned TanayParikh Dec 1, 2022
@mkArtakMSFT
Copy link
Member

Closing this, as a mitigation from @mcumming has landed already.
Basically, the mitigation is a retry on the VS side, which seems to work. we still don't know why exactly this was happening, but to timebox the investigation going to move forward with the mitigation for now. Michael will file a follow-up backlog issue to track the underlying issue for future resolution.

@samhouts samhouts added the fixed-in-6.0.540 Look for this fix in 6.0.540 SR5! label Feb 23, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Mar 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Blazor Hybrid / Desktop, BlazorWebView discussed Created by mkArtakMSFT to help with planning temporarily. It will be removed after planning is done. fixed-in-6.0.540 Look for this fix in 6.0.540 SR5! fixed-in-7.0.0-rc.1.6683 t/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants