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

Hot reload does not work for Razor page in OC 1.8.3. #15946

Closed
aaronamm opened this issue May 2, 2024 · 32 comments · Fixed by #16624
Closed

Hot reload does not work for Razor page in OC 1.8.3. #15946

aaronamm opened this issue May 2, 2024 · 32 comments · Fixed by #16624
Labels
Milestone

Comments

@aaronamm
Copy link
Contributor

aaronamm commented May 2, 2024

Describe the bug

Hot reload stop working Razor page in OC 1.8.3.
It does work perfectly if we rollback to OC 1.7.2.

To Reproduce

Steps to reproduce the behavior:

  1. Update your existing OC project to version 1.8.3 + .NET 8.
  2. Start a project with dotnet watch run
  3. Find any *.cshtml that you want to edit.
  4. Open a browser and native to URL that will render .cshtml we are going to edit.
  5. Edit that .cshtml file.
  6. Verify if a browser reload with a new content that we have just edited.

Expected behavior

  • A browser should reload a page with a new content that we have just edited.

Development environment

  • WSL 2 (Ubuntu 22.04LTS)
  • VS Code Version: 1.88.1 on Windows 10 x64 10.0.19045
  • OC 1.8.3
  • .NET SDK 8.0.204
@hishamco
Copy link
Member

hishamco commented May 2, 2024

Did you try a "Hot Reload" option from VS?

@aaronamm
Copy link
Contributor Author

aaronamm commented May 2, 2024

@hishamco thank you so much for prompt reply.
I have updated my development environment. I use VS Code not Visual Studio.

@aaronamm
Copy link
Contributor Author

aaronamm commented May 2, 2024

This issue is related to this discussion => #15321.

@hishamco
Copy link
Member

hishamco commented May 2, 2024

I have updated my development environment. I use VS Code not Visual Studio.

I know :) what I'm asking is to try, we need to make sure is happen for both options or only related to dotnet watch command

@Skrypt
Copy link
Contributor

Skrypt commented May 2, 2024

It has never really worked well with VS Code as far as I remember but the VS Code launcher option should be there. If you need Hot Reload I believe you should use Visual Studio as it is better supported.

@MikeAlhayek
Copy link
Member

Can you checkout this issue and see if it helps

#8662

@hyzx86
Copy link
Contributor

hyzx86 commented May 4, 2024

@aaronamm
Have you noticed this line?

<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Condition="'$(RazorRuntimeCompilation)' == 'true'" />

that option is disabled by default ,Because opening it requires more resources, such as memory and compilation time.

You can add the following configuration lines to your local file OrchardCore.Cms.Web.csproj.user

<ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"  />
  </ItemGroup>

@hyzx86
Copy link
Contributor

hyzx86 commented May 6, 2024

@MikeAlhayek ,It works fine on my end with no problems, I think this question should be converted to a discussion

@MikeAlhayek
Copy link
Member

@hyzx86 I think all you need is to add <RazorRuntimeCompilation>true</RazorRuntimeCompilation> to your web csproj without having the need to add reference to

    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"  />

I have not had time to test it and see if this is a problem of not.

@hyzx86

This comment was marked as outdated.

@hyzx86
Copy link
Contributor

hyzx86 commented May 6, 2024

@hyzx86 I think all you need is to add <RazorRuntimeCompilation>true</RazorRuntimeCompilation> to your web csproj without having the need to add reference to

    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"  />

I have not had time to test it and see if this is a problem of not.

I tested it and it does work, no problem

@MikeAlhayek
Copy link
Member

@hyzx86 I just tested it real quick to provide you feedback and I am getting a runtime error

An error occurred during the compilation of a resource required to process this request. Please review the following specific error details and modify your source code appropriately.

All of the views seems to have similar error


    Invalid expression term '['
    Syntax error; value expected

image

@hyzx86
Copy link
Contributor

hyzx86 commented May 6, 2024

Which branch is this? 1.8.3 ?

@MikeAlhayek
Copy link
Member

Which branch is this? 1.8.3?

main

@hyzx86
Copy link
Contributor

hyzx86 commented May 6, 2024

There should be no problem with the main branch. I was still using it a few days ago.

@hyzx86
Copy link
Contributor

hyzx86 commented May 7, 2024

Which branch is this? 1.8.3?

main 

I tried it today, and the main branch code is fine, but Visual Studio may have some caches that need to be cleared when switching branches, or else strange problems may occur.
You can do this by right clicking on the solution and batch generate -> select all projects -> then click on clean.

I even found that I was able to hot update the cs file when I tweaked it. But the second time I made the changes Visual Studio hit a fatal error and the debugger crashed 🤣

@MikeAlhayek
Copy link
Member

MikeAlhayek commented May 7, 2024

You can do this by right clicking on the solution and batch generate -> select all projects -> then click on clean.

I did that too but that does not work. Remember that I am doing this is a project that references the OC packages not the OC project itself.

By the way, executing npm run cleanup is better than clean in VS because it ensures that ALL the bin and obj folders are removed from every project.

@hishamco
Copy link
Member

hishamco commented May 7, 2024

You can use git clean -xdf

@MikeAlhayek
Copy link
Member

@hishamco the command git clean -xdf will remove things like App_Data folder which is not good in most cases. The npm run clean will only remove the bin and obj folder. And if you want to clean all npm_modules folders, rune npm run clean all this will clean all 3 folders from all projects.

@hyzx86
Copy link
Contributor

hyzx86 commented May 8, 2024

I did that too but that does not work. Remember that I am doing this is a project that references the OC packages not the OC project itself.

@MikeAlhayek , If you need to debug OC source code during development, you can try this module.
https://github.com/EasyOC/EasyOC.Modules/tree/main/src/Modules/EasyOC.AssemblyLoader

@hishamco
Copy link
Member

hishamco commented May 8, 2024

You can step through code or use a debugger from within VS Code or VS, is there any special about the OC.AssemblyLoader?

@hyzx86
Copy link
Contributor

hyzx86 commented May 8, 2024

I have a framework based on OC, the business code is in another project, then the framework can be separated from the business, before the module I use the submodule but the submodule needs to be modified sln file

In turn, I can create a local branch of OC and add a reference to this module, and I can debug my other OC modules in OC source code

@shinexyt
Copy link
Contributor

shinexyt commented May 9, 2024

I have the same issue in VS 2022 + OC 1.8.2 + .NET 8.0

OC 1.7.2 works well.

@sebastienros sebastienros added this to the 2.x milestone May 9, 2024
@hyzx86
Copy link
Contributor

hyzx86 commented May 9, 2024

My project is organized according to OC's dependency package management.
Also several .props files It's working fine so far!

But just now when I modify the cshtml file, it pops up a message that hot reloading is not supported,

After ignoring this window, refresh the page and still can apply the changes

image

@Piedone
Copy link
Member

Piedone commented Aug 21, 2024

More context in this duplicate: #16504.

@shinexyt
Copy link
Contributor

I think I may have located the source of this issue. It was introduced by PR #14348.
For performance reasons, the following code registers a static compiler provider, but this may cause hot reload to fail.

services.AddSingleton<IViewCompilerProvider, SharedViewCompilerProvider>();

Could we register this provider only in the production environment? Something like this:

// Support razor runtime compilation only if in dev mode and if the 'refs' folder exists.
var refsFolderExists = Directory.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "refs"));

if (_hostingEnvironment.IsDevelopment())
{
    if (refsFolderExists)
    {
        builder.AddRazorRuntimeCompilation();
    }
}
else
{
    // Share across tenants a static compiler even if there is no runtime compilation
    // because the compiler still uses its internal cache to retrieve compiled items.
    services.AddSingleton<IViewCompilerProvider, SharedViewCompilerProvider>();
}

I'm not sure if the changes are correct. If they are, would it be possible for me to submit my first pull request?

@Piedone
Copy link
Member

Piedone commented Aug 26, 2024

Hmm, interesting, and if this fixes the issue then certainly, please open a PR. But I'm still wondering why hot reload works for me with the same code. @DavidStania could you please test the above code changes?

@gvkries
Copy link
Contributor

gvkries commented Aug 26, 2024

// Support razor runtime compilation only if in dev mode and if the 'refs' folder exists.
var refsFolderExists = Directory.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "refs"));

if (_hostingEnvironment.IsDevelopment())
{
    if (refsFolderExists)
    {
        builder.AddRazorRuntimeCompilation();
    }
}
else
{
    // Share across tenants a static compiler even if there is no runtime compilation
    // because the compiler still uses its internal cache to retrieve compiled items.
    services.AddSingleton<IViewCompilerProvider, SharedViewCompilerProvider>();
}

I'm not sure if the changes are correct. If they are, would it be possible for me to submit my first pull request?

I can confirm that this fixes the hot reload issue, which I have as well. We would be happy if you want to submit your first PR.

@DavidStania
Copy link
Contributor

Not working for me!
image

In my case, the 'refs' folder does not exists and
builder.AddRazorRuntimeCompilation();
will not be hit.

@shinexyt
Copy link
Contributor

@DavidStania

It doesn't matter whether builder.AddRazorRuntimeCompilation() is hit or not; we just need to ensure SharedViewCompilerProvider is not registered in development mode. Please try again and check if hot reload works properly.

@DavidStania
Copy link
Contributor

Genius! It works for me!
Your next coffee is on me. That was a really big problem for us and made us very inefficient.

@gvkries
Copy link
Contributor

gvkries commented Aug 27, 2024

Just to note, this issue is about hot reload. Runtime compilation is a different beast 😊

@MikeAlhayek MikeAlhayek modified the milestones: 2.x, 2.0 Sep 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants