-
Notifications
You must be signed in to change notification settings - Fork 196
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
Bug while compiling #regions on SDK 8.0.400. #10737
Comments
@dotnet/roslyn-compiler - This looks like a potential compiler issue? Should I transfer to Roslyn? |
@mauriciocirelli Is this occurring in a C# file or a Razor file? |
In a razor cshtml file.
|
@MichaelSimons Can you transfer this to https://github.com/dotnet/razor ? Thanks. |
@mauriciocirelli Can you share any repro? A simplified version of the syntax you're using would be great if possible. |
/cc @333fred |
Directives aren't well handled by razor today; there's no explicit testing whatsoever, so it doesn't surprise me that some change broke them. The new tokenizer will have much better support for these scenarios, with explicit testing as well. |
Sure. Something as simple as this: @{
#region
ViewData["Title"] = "Title";
#endregion
<h3>@ViewData["Title"]</h3>
<hr />
} In my case, I could also fix the docker build by moving the html tags out of the |
Great, thanks for the code, I can repro on my end. It works on 8.0.303 and fails on 8.0.400 @333fred @janjones It looks like we might be parsing something differently that leads to us treating HTML code as C#, which then gets output verbatim into the code: 43c43
< #line (12,3)-(18,5) "C:\projects\scratch\WebApplication4\WebApplication4\Pages\Index.cshtml"
---
> #line (12,3)-(20,1) "C:\projects\scratch\WebApplication4\WebApplication4\Pages\Index.cshtml"
50c50,51
<
---
> <h3> @ViewData["Title"] </h3>
> <hr />
56,66d56
< WriteLiteral("<h3> ");
< Write(
< #nullable restore
< #line (18,11)-(18,28) "C:\projects\scratch\WebApplication4\WebApplication4\Pages\Index.cshtml"
< ViewData["Title"]
<
< #line default
< #line hidden
< #nullable disable
< );
< WriteLiteral(" </h3>\r\n <hr />\r\n");
Any guesses what might have changed here? |
I have investigated a bit and I think it previously worked just by accident - we never parsed razor/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Legacy/CSharpCodeParser.cs Lines 1033 to 1038 in 29658c1
That was changed in #10232 - we now always parse the That this never have really worked can be seen if you construct an example without the @{
#region
ViewData["Title"] = "Title";
#endregion
<h3>title</h3>
<hr />
} |
Well, intended or not, I have been using this code for about 5 years... since .net core 2.0 at least and it has always worked hehehe |
That means you are using a different .NET SDK version in your Docker builds than in VS. If you update your local SDK, you should see the errors in VS as well. |
Hi all, we've gotten a number of hits on this bug, so I wanted to summarize the current status. Bug SummaryRazor's lexer and parser does not have a good understanding of C# preprocessor directives, and never has. When the preprocessor directive is followed by a normal C# statement, something that ends a C# statement ( FlavorsThere are a few different flavors of this bug that we've identified:
@{
#region R
<h3>@ViewData["Title"]</h3>
#endregion
}
@{
switch (true)
{
#region R
case true:
<div>@(1 + 1)</div>
break;
}
}
ActionsThis is an unfortunate bug that isn't particularly easy to solve; all possible paths have different negative outcomes.
We've decided on paths 1 and 3 here; we've already been heavily working on the new roslyn-based tokenizer, and hope to have feature branch merged by the end of the month. That will be an opt-in change as we start testing on more real-world scenarios, but it has shown promising compatibility with existing major Razor repos and will bring lots of other improvements, such as better string support and making it easier to keep Razor's understanding of C# up to date. In the meantime, we don't think adding a 3rd incorrect preprocessor parsing path serves anyone; the workarounds are valid Razor and will continue to be so for the forseeable future, and we're very concerned about the potential for the runtime split confusing people further. Going forward, we will also be creating a document on breaking changes in Razor, which will document things of this nature, and we will also be working to get it published on Microsoft Learn, like similar documents are published from the Roslyn and Runtime projects. We're sorry for the inconvenience caused here. WorkaroundsTo explicitly spell out the workarounds, here's a few different examples of how to workaround the issue: Surround the HTML in a blockThe HTML can be surrounded with braces. This works in every case that we've seen reported; if there is some case that this doesn't cover, please let us know. @{
#if DEBUG
{
<h3>@ViewData["Title"]</h3>
}
#endif
} Add a semicolon to the directiveDirectives such as @{
#region R ;
<h3>@ViewData["Title"]</h3>
#endregion
} Add a semicolon after the directiveDirectives such as @{
#if DEBUG
;
<h3>@ViewData["Title"]</h3>
#endif
} |
Dear,
Today we tried to build a aspnet 8.0 project, which used to build just fine, and noticed that it pulled a new container version, moving sdk from 8.0.303 to 8.0.400, and we started to get some strange errors like this:
error CS0019: Operator '>' cannot be applied to operands of type 'string' and 'IHtmlContent'
The strange part is that the line that is pointed by the error message does not contain such a comparison.
We could see this same error in several other cshtml files and the common thing is that the line pointed by the error message is one line after an
#endregion
instruction.Removing the
#region
/#endregion
pairs fixed the build issue.Am I missing something?
The text was updated successfully, but these errors were encountered: