Closed sungaila closed 10 months ago
@sungaila thanks for contacting us.
@SteveSandersonMS can this be related to some of the compiler changes we did in 8.0? I suspect they should have been keyed off based on the Razor Lang Version?
@javiercn Yes, we did make changes in this area, so it's possible there was a Razor compiler regression.
@sungaila To help us track this down quickly, would you be willing to supply the Razor compiler generated files in these two cases? If you add <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
to a property group in your .csproj
, then on build, the compiler will output .cs
files into your obj
directory that correspond to each of your .razor
files. If you could supply the compiler output for net7.0
and net8.0
so we could see how they differ, that will probably lead us immediately to confirming if there was a regression (and if so, what we have to fix).
Thanks for the fast response!
@SteveSandersonMS Done that and compared the output in obj\Debug\netX.0\generated\Microsoft.NET.Sdk.Razor.SourceGenerators
.
_Imports_razor.g.cs
, MainLayout_razor.g.cs
and Pages_Index_razor.g.cs
are identical.App_razor.g.cs
uses RenderTreeBuilder.AddAttribute
for net7.0
and RenderTreeBuilder.AddComponentParameter
for net8.0
Here are the full App_razor.g.cs
:
// .NET 7
#pragma checksum "X:\Repos\sungaila\RazorIssueReproduction\BlazorAppNet7\App.razor" "{8829d00f-11b8-4213-878b-770e8597ac16}" "ca69fbc161c0130d6d7831728befc975abb17b04491a271bc49266261055543b"
// <auto-generated/>
#pragma warning disable 1591
namespace BlazorAppNet7
{
#line hidden
using global::System;
using global::System.Collections.Generic;
using global::System.Linq;
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Components;
#nullable restore
#line 1 "X:\Repos\sungaila\RazorIssueReproduction\BlazorAppNet7\_Imports.razor"
using System.Net.Http;
#line default
#line hidden
#nullable disable
#nullable restore
#line 2 "X:\Repos\sungaila\RazorIssueReproduction\BlazorAppNet7\_Imports.razor"
using System.Net.Http.Json;
#line default
#line hidden
#nullable disable
#nullable restore
#line 3 "X:\Repos\sungaila\RazorIssueReproduction\BlazorAppNet7\_Imports.razor"
using Microsoft.AspNetCore.Components.Routing;
#line default
#line hidden
#nullable disable
#nullable restore
#line 4 "X:\Repos\sungaila\RazorIssueReproduction\BlazorAppNet7\_Imports.razor"
using Microsoft.AspNetCore.Components.Web;
#line default
#line hidden
#nullable disable
#nullable restore
#line 5 "X:\Repos\sungaila\RazorIssueReproduction\BlazorAppNet7\_Imports.razor"
using Microsoft.AspNetCore.Components.WebAssembly.Http;
#line default
#line hidden
#nullable disable
#nullable restore
#line 6 "X:\Repos\sungaila\RazorIssueReproduction\BlazorAppNet7\_Imports.razor"
using Microsoft.JSInterop;
#line default
#line hidden
#nullable disable
#nullable restore
#line 7 "X:\Repos\sungaila\RazorIssueReproduction\BlazorAppNet7\_Imports.razor"
using BlazorAppNet7;
#line default
#line hidden
#nullable disable
public partial class App : global::Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 1998
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__builder.OpenComponent<global::Microsoft.AspNetCore.Components.Routing.Router>(0);
__builder.AddAttribute(1, "AppAssembly", (object)(global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Reflection.Assembly>(
#nullable restore
#line 1 "X:\Repos\sungaila\RazorIssueReproduction\BlazorAppNet7\App.razor"
typeof(App).Assembly
#line default
#line hidden
#nullable disable
)));
__builder.AddAttribute(2, "Found", (global::Microsoft.AspNetCore.Components.RenderFragment<Microsoft.AspNetCore.Components.RouteData>)((routeData) => (__builder2) => {
__builder2.OpenComponent<global::Microsoft.AspNetCore.Components.RouteView>(3);
__builder2.AddAttribute(4, "RouteData", (object)(global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::Microsoft.AspNetCore.Components.RouteData>(
#nullable restore
#line 3 "X:\Repos\sungaila\RazorIssueReproduction\BlazorAppNet7\App.razor"
routeData
#line default
#line hidden
#nullable disable
)));
__builder2.AddAttribute(5, "DefaultLayout", (object)(global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Type>(
#nullable restore
#line 3 "X:\Repos\sungaila\RazorIssueReproduction\BlazorAppNet7\App.razor"
typeof(MainLayout)
#line default
#line hidden
#nullable disable
)));
__builder2.CloseComponent();
__builder2.AddMarkupContent(6, "\r\n ");
__builder2.OpenComponent<global::Microsoft.AspNetCore.Components.Routing.FocusOnNavigate>(7);
__builder2.AddAttribute(8, "RouteData", (object)(global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::Microsoft.AspNetCore.Components.RouteData>(
#nullable restore
#line 4 "X:\Repos\sungaila\RazorIssueReproduction\BlazorAppNet7\App.razor"
routeData
#line default
#line hidden
#nullable disable
)));
__builder2.AddAttribute(9, "Selector", (object)("h1"));
__builder2.CloseComponent();
}
));
__builder.AddAttribute(10, "NotFound", (global::Microsoft.AspNetCore.Components.RenderFragment)((__builder2) => {
__builder2.OpenComponent<global::Microsoft.AspNetCore.Components.Web.PageTitle>(11);
__builder2.AddAttribute(12, "ChildContent", (global::Microsoft.AspNetCore.Components.RenderFragment)((__builder3) => {
__builder3.AddContent(13, "Not found");
}
));
__builder2.CloseComponent();
__builder2.AddMarkupContent(14, "\r\n ");
__builder2.OpenComponent<global::Microsoft.AspNetCore.Components.LayoutView>(15);
__builder2.AddAttribute(16, "Layout", (object)(global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Type>(
#nullable restore
#line 8 "X:\Repos\sungaila\RazorIssueReproduction\BlazorAppNet7\App.razor"
typeof(MainLayout)
#line default
#line hidden
#nullable disable
)));
__builder2.AddAttribute(17, "ChildContent", (global::Microsoft.AspNetCore.Components.RenderFragment)((__builder3) => {
__builder3.AddMarkupContent(18, "<p role=\"alert\">Sorry, there\'s nothing at this address.</p>");
}
));
__builder2.CloseComponent();
}
));
__builder.CloseComponent();
}
#pragma warning restore 1998
}
}
#pragma warning restore 1591
// .NET 8
#pragma checksum "X:\Repos\sungaila\RazorIssueReproduction\BlazorAppNet8\App.razor" "{8829d00f-11b8-4213-878b-770e8597ac16}" "ca69fbc161c0130d6d7831728befc975abb17b04491a271bc49266261055543b"
// <auto-generated/>
#pragma warning disable 1591
namespace BlazorAppNet8
{
#line hidden
using global::System;
using global::System.Collections.Generic;
using global::System.Linq;
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Components;
#nullable restore
#line 1 "X:\Repos\sungaila\RazorIssueReproduction\BlazorAppNet8\_Imports.razor"
using System.Net.Http;
#line default
#line hidden
#nullable disable
#nullable restore
#line 2 "X:\Repos\sungaila\RazorIssueReproduction\BlazorAppNet8\_Imports.razor"
using System.Net.Http.Json;
#line default
#line hidden
#nullable disable
#nullable restore
#line 3 "X:\Repos\sungaila\RazorIssueReproduction\BlazorAppNet8\_Imports.razor"
using Microsoft.AspNetCore.Components.Routing;
#line default
#line hidden
#nullable disable
#nullable restore
#line 4 "X:\Repos\sungaila\RazorIssueReproduction\BlazorAppNet8\_Imports.razor"
using Microsoft.AspNetCore.Components.Web;
#line default
#line hidden
#nullable disable
#nullable restore
#line 5 "X:\Repos\sungaila\RazorIssueReproduction\BlazorAppNet8\_Imports.razor"
using Microsoft.AspNetCore.Components.WebAssembly.Http;
#line default
#line hidden
#nullable disable
#nullable restore
#line 6 "X:\Repos\sungaila\RazorIssueReproduction\BlazorAppNet8\_Imports.razor"
using Microsoft.JSInterop;
#line default
#line hidden
#nullable disable
#nullable restore
#line 7 "X:\Repos\sungaila\RazorIssueReproduction\BlazorAppNet8\_Imports.razor"
using BlazorAppNet8;
#line default
#line hidden
#nullable disable
public partial class App : global::Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 1998
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__builder.OpenComponent<global::Microsoft.AspNetCore.Components.Routing.Router>(0);
__builder.AddComponentParameter(1, "AppAssembly", global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Reflection.Assembly>(
#nullable restore
#line 1 "X:\Repos\sungaila\RazorIssueReproduction\BlazorAppNet8\App.razor"
typeof(App).Assembly
#line default
#line hidden
#nullable disable
));
__builder.AddAttribute(2, "Found", (global::Microsoft.AspNetCore.Components.RenderFragment<Microsoft.AspNetCore.Components.RouteData>)((routeData) => (__builder2) => {
__builder2.OpenComponent<global::Microsoft.AspNetCore.Components.RouteView>(3);
__builder2.AddComponentParameter(4, "RouteData", global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::Microsoft.AspNetCore.Components.RouteData>(
#nullable restore
#line 3 "X:\Repos\sungaila\RazorIssueReproduction\BlazorAppNet8\App.razor"
routeData
#line default
#line hidden
#nullable disable
));
__builder2.AddComponentParameter(5, "DefaultLayout", global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Type>(
#nullable restore
#line 3 "X:\Repos\sungaila\RazorIssueReproduction\BlazorAppNet8\App.razor"
typeof(MainLayout)
#line default
#line hidden
#nullable disable
));
__builder2.CloseComponent();
__builder2.AddMarkupContent(6, "\r\n ");
__builder2.OpenComponent<global::Microsoft.AspNetCore.Components.Routing.FocusOnNavigate>(7);
__builder2.AddComponentParameter(8, "RouteData", global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::Microsoft.AspNetCore.Components.RouteData>(
#nullable restore
#line 4 "X:\Repos\sungaila\RazorIssueReproduction\BlazorAppNet8\App.razor"
routeData
#line default
#line hidden
#nullable disable
));
__builder2.AddComponentParameter(9, "Selector", "h1");
__builder2.CloseComponent();
}
));
__builder.AddAttribute(10, "NotFound", (global::Microsoft.AspNetCore.Components.RenderFragment)((__builder2) => {
__builder2.OpenComponent<global::Microsoft.AspNetCore.Components.Web.PageTitle>(11);
__builder2.AddAttribute(12, "ChildContent", (global::Microsoft.AspNetCore.Components.RenderFragment)((__builder3) => {
__builder3.AddContent(13, "Not found");
}
));
__builder2.CloseComponent();
__builder2.AddMarkupContent(14, "\r\n ");
__builder2.OpenComponent<global::Microsoft.AspNetCore.Components.LayoutView>(15);
__builder2.AddComponentParameter(16, "Layout", global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<global::System.Type>(
#nullable restore
#line 8 "X:\Repos\sungaila\RazorIssueReproduction\BlazorAppNet8\App.razor"
typeof(MainLayout)
#line default
#line hidden
#nullable disable
));
__builder2.AddAttribute(17, "ChildContent", (global::Microsoft.AspNetCore.Components.RenderFragment)((__builder3) => {
__builder3.AddMarkupContent(18, "<p role=\"alert\">Sorry, there\'s nothing at this address.</p>");
}
));
__builder2.CloseComponent();
}
));
__builder.CloseComponent();
}
#pragma warning restore 1998
}
}
#pragma warning restore 1591
Thanks @sungaila. I'm surprised that the Pages_Index_razor.g.cs
files are identical. It seems impossible.
Can you confirm that they were generated by the two different SDKs? That is, one was generated when you have the .NET 8 SDK installed (but targeting net7.0
), and the other was generated when you do not have .NET 8 SDK installed?
Edit: I realise now that my phrasing in the previous comment implied I was interested in the differences between targeting net7.0
and net8.0
, but that was very misleading of me. The difference we should be concerned with is between the two SDKs when your project targets net7.0
in both cases.
@SteveSandersonMS Yes, both outputs were generated with the .NET 8 SDK. I'll try again with .NET 7 SDK vs. .NET 8 SDK and post my results here.
Hi @sungaila. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.
@SteveSandersonMS So I retested building the same project targeting net7.0
with the .NET 7 SDK and .NET 8 SDK. And there is a major difference in the Pages_Index_razor.g.cs
file that looks like the cause of the problem:
// built with .NET 7 SDK and working
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__builder.AddMarkupContent(0, "<h1>Hello, world!</h1>\r\n");
__builder.OpenElement(1, "script");
__builder.AddContent(2, "alert(\"Hello, world!\");");
__builder.CloseElement();
}
// built with .NET 8 SDK and BROKEN
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__builder.AddMarkupContent(0, "<h1>Hello, world!</h1>\r\n");
__builder.AddMarkupContent(1, "<script>alert(\"Hello, world!\");</script>");
}
The .NET 7 SDK uses OpenElement()
, AddContent()
and CloseElement()
to include the JavaScript stuff (and it works).
The .NET 8 SDK uses AddMarkupContent()
and prevents the JavaScript from executing.
Edit: Targeting net8.0
with the .NET 8 SDK results in the same output as the second code above (JS works). So my guess is that the .NET 8 SDK generates code "too new" for the older net7.0
project.
Thanks for the extra info! This confirms the suspicion about what's going on, so we should move this discussion over to the Razor repo for a possible fix. I've filed https://github.com/dotnet/razor/issues/9584 and will close this in favour of that one.
Is there an existing issue for this?
Describe the bug
After upgrading to the final .NET 8 SDK release my Blazor WebAssembly project still running on .NET 7 broke. All JavaScript within the
<script>
tags in the Razor pages are not executed.Upgrading the Blazor WebAssembly project to .NET 8 as target framework fixes this issue.
Workaround: Uninstall .NET 8 SDK and fallback to .NET 7 SDK.
Expected Behavior
I expect that upgrading to the .NET 8 SDK will not break my older Blazor WebAssembly projects still targeting .NET 7. JavaScript should be executed if included in a Razor page.
Steps To Reproduce
Minimalistic project reproduction (created with the
Blazor WebAssembly app empty
template) can be found here: https://github.com/sungaila/RazorIssueReproductionMicrosoft.AspNetCore.App 7.0.14
Microsoft.AspNetCore.App 8.0.0
dotnet run BlazorAppNet8.csproj
and open the website in the browserHello, world!
as a messagedotnet run BlazorAppNet7.csproj
and open the website in the browser<script>alert("Hello, world!");</script>
inPages\Index.razor
was not executedExceptions (if any)
No response
.NET Version
8.0.100
Anything else?
ASP.NET Core version:
Microsoft.AspNetCore.App 8.0.0
No IDE justdotnet
in console.donet --info output: