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

Latest OrchardCore packages from Cloudsmith break Razor hot reload #8662

Closed
xperiandri opened this issue Feb 23, 2021 · 21 comments
Closed

Latest OrchardCore packages from Cloudsmith break Razor hot reload #8662

xperiandri opened this issue Feb 23, 2021 · 21 comments
Labels
Milestone

Comments

@xperiandri
Copy link
Contributor

Describe the bug

Razor hot reload does not work

To Reproduce

Steps to reproduce the behavior:

  1. Use a project with the latest Orchard Core packages from Cloudsmith and Razor page
  2. Run the project with Ctrl+F5
  3. Change the Razor of the page
  4. Reload the page - nothing changes

But with RC2 packages it works

Expected behavior

Razor hot reload works

@xperiandri
Copy link
Contributor Author

@sebastienros I've tested all the available CI packages till 1.0.0-rc2-14367 and no one works.
But as soon as I switch to 1.0.0-rc2-13450 it works again.

@deanmarcussen
Copy link
Member

Have you tried with dotnet run ?

I can't repro the issue on mac, and equally can't test in in Visual Studio.

Perhaps paste your .csproj host startup file, there maybe missing elements that are required for runtime compilation of views.

@xperiandri
Copy link
Contributor Author

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace SecretCircle.BmwCms
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddOrchardCms();
            var contentTypeProvider = new Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider();
            contentTypeProvider.Mappings.Add(".scss", "text/scss");
            services.Configure<StaticFileOptions>(o => o.ContentTypeProvider = contentTypeProvider);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBrowserLink();
            }

            app.UseOrchardCore();
        }
    }
}

@deanmarcussen
Copy link
Member

Sorry I meant .csproj file

@xperiandri
Copy link
Contributor Author

<Project Sdk="Microsoft.NET.Sdk.Web">
  <Import Project="..\Solution.Build.props" />

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
    <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
    <UserSecretsId>$(AssemblyBaseName)</UserSecretsId>
    <RootNamespace>$(BaseRootNamespace)</RootNamespace>
    <AssemblyName>$(AssemblyBaseName)</AssemblyName>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)'=='Release'">
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
    <WarningsAsErrors />
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Boxed.AspNetCore" Version="5.1.2" />
    <PackageReference Include="Boxed.AspNetCore.TagHelpers" Version="4.0.0" />
    <PackageReference Include="DisposableFixer" Version="3.3.0" />
    <PackageReference Include="ErrorProne.NET.CoreAnalyzers" Version="0.1.2">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="ErrorProne.NET.Structs" Version="0.1.2">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Flurl" Version="3.0.1" />
    <!--<PackageReference Include="LigerShark.WebOptimizer.Core" Version="3.0.295" />-->
    <!--<PackageReference Include="LigerShark.WebOptimizer.Sass" Version="3.0.43-beta" />-->
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.16.0" />
    <PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="5.0.3">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.2.0" />
    <PackageReference Include="OrchardCore.Application.Cms.Targets" Version="$(OrcahrdCoreVersion)" />
    <PackageReference Include="OrchardCore.Templates" Version="$(OrcahrdCoreVersion)" />
    <PackageReference Include="Roslynator.Analyzers" Version="3.0.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Roslynator.CSharp" Version="1.0.0" />
    <PackageReference Include="Syncfusion.EJ2.AspNet.Core" Version="18.4.0.34" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\OrchardCore.FavIcon\OrchardCore.FavIcon.csproj" />
    <ProjectReference Include="..\SecretCircle.BmwTheme\SecretCircle.BmwTheme.csproj" />
    <ProjectReference Include="..\SharpScss\src\SharpScss\SharpScss.csproj" />
    <ProjectReference Include="..\WebOptimizer.Sass\src\WebOptimizer.Sass.csproj" />
    <ProjectReference Include="..\WebOptimizer\src\WebOptimizer.Core\WebOptimizer.Core.csproj" />
  </ItemGroup>

  <ProjectExtensions>
    <VisualStudio>
      <UserProperties properties_4launchsettings_1json__JsonSchema="" />
    </VisualStudio>
  </ProjectExtensions>

</Project>

@deanmarcussen
Copy link
Member

try adding <PreserveCompilationReferences>true</PreserveCompilationReferences>

@xperiandri
Copy link
Contributor Author

@deanmarcussen it indeed helps! Works after adding that setting

@xperiandri
Copy link
Contributor Author

It is only required in an app project. On a theme project, it does not affect anything.

@sebastienros
Copy link
Member

JT told me that you need to add this property when you have your ASPNETCORE_ENVIRONMENT defined to Production. This should appear in the console when the app starts. When you run your app from VS you should always have Development, and it will allow you to change the razor files dynamically. Otherwise you will need this PreserveCompilationReferences property and it will build all razor files at runtime which will make the app much slower.

@sebastienros
Copy link
Member

This value is also usually set in the launchSettings.json file. Please check what it contains for you if you don't have the ENV defined on your system, or the project settings.

@sebastienros
Copy link
Member

@xperiandri
Copy link
Contributor Author

But if I run with Ctrl+F5 is still sets the environment to Development. But the Razor hot reload does not work.
This is what we used to, doing Razor development on the fly without debugging.

@jtkech
Copy link
Member

jtkech commented Feb 28, 2021

Just tried with Ctrl+F5, it works

Having PreserveCompilationReferences = true in the main app project file
Also Sdk="Microsoft.NET.Sdk.Web in the first line of the project file
And "ASPNETCORE_ENVIRONMENT": "Development" for "IIS Express" in the launchSettings.json

@xperiandri
Copy link
Contributor Author

Yes, it is.
@sebastienros told that it forces the same behavior in production scenarios which is bad for performance.
Was it the same in .NET 3.1?

@xperiandri
Copy link
Contributor Author

Does it mean the PreserveCompilationReferences must be enabled for Debug project configuration only or can it be set in launchSettings.json?

@jtkech
Copy link
Member

jtkech commented Feb 28, 2021

To have razor runtime compilation you need both

PreserveCompilationReferences = true and ASPNETCORE_ENVIRONMENT = Development

If in production you actually have e.g. ASPNETCORE_ENVIRONMENT = Production, not Development

That's okay, you can keep PreserveCompilationReferences = true, you will not have runtime compilation

@sebastienros
Copy link
Member

How does this last comment make sense?

To have razor runtime compilation you need both PreserveCompilationReferences = true and ASPNETCORE_ENVIRONMENT = Development

you can keep PreserveCompilationReferences = true, you will not have runtime compilation

@jtkech
Copy link
Member

jtkech commented Mar 3, 2021

Razor runtime compilation needs compilation libraries in the refs folder, otherwise it fails, we check it at runtime before registering the related services, so to have runtime compilation we first need PreserveCompilationReferences = true to create and populate the refs folder . Then, we need our custom file provider to serve the view contents from their project locations, we activate it only if we are in Development mode.

So, to have razor runtime compilation, we need both PreserveCompilationReferences = true and ASPNETCORE_ENVIRONMENT = Development. If we are not in Development, e.g. Production, we never have razor runtime compilation, having PreserveCompilationReferences or not will just populate or not the refs folder, but in both case there is no runtime compilation

As i remember, at some point PreserveCompilationReferences was true by default, which is no more the case

@sebastienros
Copy link
Member

As i remember, at some point PreserveCompilationReferences was true by default, which is no more the case

Does it mean that the default experience for ASP.NET is to not have runtime compilation, and that if you change a Razor file, even in Development then it doesn't refresh?

@jtkech
Copy link
Member

jtkech commented Mar 3, 2021

Yes, as i remember, razor runtime compilation is no more the default, and if you want it you now need to reference explicitly the razor compilation package in a given application project

Because of our multi tenants structure, we reference this package automatically per tenant, and we register the related services, but only if the refs folder exisits.

@ns8482e
Copy link
Contributor

ns8482e commented Jun 15, 2021

In VS 2019, while creating new Razor/MVC project, you have a choice to enable "Razor runtime compilation".

It can also be enabled by setting env var in launchsettings.json
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants