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

Umbraco .cshtml files not compiled into <project>.Views.dll after clean build #11758

Closed
PerplexDaniel opened this issue Dec 14, 2021 · 0 comments · Fixed by #11759
Closed

Umbraco .cshtml files not compiled into <project>.Views.dll after clean build #11758

PerplexDaniel opened this issue Dec 14, 2021 · 0 comments · Fixed by #11759
Labels

Comments

@PerplexDaniel
Copy link
Contributor

Which exact Umbraco version are you using? For example: 9.0.1 - don't just write v9

9.1.2

Bug summary

Umbraco .cshtml files (<projectDir>\umbraco\**\*.cshtml, e.g. umbraco\UmbracoInstall\Index.cshtml) are not compiled by the built-in Razor generator when building the project after a dotnet clean, or if the files were simply not there before you start the build. The latter happens all the time on a build server. Normally, all .cshtml files are compiled into <project>.Views.dll and you do not need to put any .cshtml files on your webserver anymore which is great.

The target responsible for gathering the input .cshtml files for the Razor generation is
"ResolveRazorGenerateInputs". As you can see it simply includes any <Content> items that have a .cshtml extension.

The Umbraco .cshtml files are copied before build using a target defined in Umbraco.Cms.StaticAssets.targets, but apparently not picked up by the Razor generation target on time.

A possible fix

The simplest fix is to manually include the Umbraco .cshtml files before the "ResolveRazorGenerateInputs" target runs.
This way we do not have to change anything about the existing "CopyUmbracoAssets" target so the impact should be minimal.

I will link a PR to this issue that includes this target in Umbraco.Cms.StaticAssets.targets so you can easily merge it if you agree with this solution. Of course if you prefer to solve it in another way then that's totally fine!

  • Add this to the bottom of your .csproj (before </Project>):
<Target Name="IncludeUmbracoRazorFiles" BeforeTargets="ResolveRazorGenerateInputs">
    <ItemGroup>
        <Content Include="$(MSBuildProjectDirectory)\umbraco\**\*.cshtml" />
    </ItemGroup>
</Target>

Specifics

No response

Steps to reproduce

The quickest way to show this is by opening a newly installed Umbraco project because this will run the umbraco\UmbracoInstall\Index.cshtml file which is one of the files not being compiled after a clean build.
We should be able to remove that .cshtml file just fine but when we do after a clean build we see an exception:

> dotnet new umbraco -n Razor
> cd Razor

  • open Razor.csproj in some editor, set RazorCompileOn{Build,Publish} to true.
    • Do not worry about ModelsBuilder comment that HQ put there we will show the bug before any models are generated.
<RazorCompileOnBuild>true</RazorCompileOnBuild>
<RazorCompileOnPublish>true</RazorCompileOnPublish>

> dotnet clean && dotnet publish -o pub
> cd pub
> del umbraco\UmbracoInstall\Index.cshtml

  • This removes the Installation .cshtml file which should work fine if it is compiled.

> set ASPNETCORE_ENVIRONMENT=Development

  • This way you will see the actual exception

> Razor.exe

To see it will work if the .cshtml files are already on disk, repeat this process again but without a dotnet clean you will notice it simply works:

> cd ..
> rmdir /s /q pub && dotnet publish -o pub && cd pub && del umbraco\UmbracoInstall\Index.cshtml && set ASPNETCORE_ENVIRONMENT=Development&&Razor

  • All required commands together, just copy + paste into cmd
  • goto https://localhost:5001 (or http://localhost:5000 for HTTP)
  • Observe it works fine now, the cshtml files are compiled into Razor.Views.dll and the installer loads fine without UmbracoInstall\Index.cshtml on disk.

If you apply the fix mentioned in the summary you can run a clean + publish again and it will still work.

Expected result / actual result

No response

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.

3 participants