OrchardCMS / OrchardCore

Orchard Core is an open-source modular and multi-tenant application framework built with ASP.NET Core, and a content management system (CMS) built on top of that framework.
https://orchardcore.net
BSD 3-Clause "New" or "Revised" License
7.35k stars 2.37k forks source link

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

Closed aaronamm closed 1 week ago

aaronamm commented 4 months ago

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

Development environment

hishamco commented 4 months ago

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

aaronamm commented 4 months ago

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

aaronamm commented 4 months ago

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

hishamco commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

Can you checkout this issue and see if it helps

https://github.com/OrchardCMS/OrchardCore/issues/8662

hyzx86 commented 4 months ago

@aaronamm Have you noticed this line?

https://github.com/OrchardCMS/OrchardCore/blob/aa5005be56cdcc2f56c0d555990eb6a52b7de76a/src/OrchardCore.Cms.Web/OrchardCore.Cms.Web.csproj#L32

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 commented 4 months ago

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

MikeAlhayek commented 4 months ago

@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 commented 4 months ago

Yes, it may or may not work, I guess csproj.user is executed after xxx.csproj, so I think the other way is more straightforward

hyzx86 commented 4 months ago

@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 commented 4 months ago

@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 commented 4 months ago

Which branch is this? 1.8.3 ?

MikeAlhayek commented 4 months ago

Which branch is this? 1.8.3?

main

hyzx86 commented 4 months ago

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

hyzx86 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

You can use git clean -xdf

MikeAlhayek commented 3 months ago

@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 commented 3 months ago

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 commented 3 months ago

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

hyzx86 commented 3 months ago

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 commented 3 months ago

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

OC 1.7.2 works well.

hyzx86 commented 3 months ago

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 commented 2 weeks ago

More context in this duplicate: https://github.com/OrchardCMS/OrchardCore/issues/16504.

shinexyt commented 1 week ago

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. https://github.com/OrchardCMS/OrchardCore/blob/4395dbb793cc166c3179049fc01d3121c603cf72/src/OrchardCore/OrchardCore.Mvc.Core/Startup.cs#L109 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 commented 1 week ago

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 commented 1 week ago
// 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 commented 1 week ago

Not working for me! image

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

shinexyt commented 1 week ago

@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 commented 1 week ago

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 commented 1 week ago

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