-
Notifications
You must be signed in to change notification settings - Fork 199
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
Razor files: Generated code misses #line hidden for using directives #10375
Comments
The Presumably this only happens if the Razor file has an |
This issue impacts us on SonarCloud, and yes, it's the _Imports.razor file for us that only has 11 This razor file is used for a part of our application using Blazor, as followed from the official docs. I think this would be rather common. |
The problem is reproducible also when using dotnet 8.0.5. |
Oh yes, I didn't think of the |
I think the problem starts with the namespace declaration. The generated syntax tree for: @using System
@using System.Collections.Generic is: #pragma checksum "C:\_Imports.razor" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "395205bceb8baabf9dd9a6d172f3f922d3bf2479"
// <auto-generated/>
#pragma warning disable 1591
namespace ASP.TestCases
{
... If I call I would have expected that the namespace declaration would be treated as generated code and not be mapped. Since it's not part of the user code, any line reported is wrong. In this specific case, it happens that line 4 (0-based index) does not exist in the file. |
@costin-zaharia-sonarsource In that example with the namespace, does the mapped span come back with |
Hi @davidwengier, great question. I've missed that. The The problem is indeed the semicolon. |
The problem is that for the One problem with enhanced line directives is that they lack proper support for "range," as described, e.g., here: dotnet/roslyn#69248 (comment). You chose to separate the original and generated code by new lines. In the case of |
@martin-strecker-sonarsource Thanks, thats a very informative link. This line in particular:
This surprises me. Logically speaking I would have thought that the changes introduced in #10380 would solve this, as based on this diff for example: I would have hoped that Roslyn would know that the newline and |
I'm not sure about it and it needs to be tested. The actual mapping is quite complicated (due to support for debugger sequence points) and described here: https://github.com/dotnet/csharplang/blob/8bb3c4ca0180db2b9dc3573d91bee60da2ec530c/proposals/csharp-10.0/enhanced-line-directives.md Can you test in #10380 if I can do such a test in SonarSource/sonar-dotnet#9289 if that helps. |
I've found one more case that might be related to this. When ...
#line default
#line hidden
#nullable disable
;
[global::Microsoft.AspNetCore.Components.RouteAttribute(
// language=Route,Component
#nullable restore
#line (1,7)-(1,15) "C:\src\work\sonar-dotnet\sonar-dotnet-shared-library\src\test\resources\RazorProtobufImporter\WebProject\Cases.razor"
"/cases"
#line default
#line hidden
#nullable disable
)]
... and the If you prefer I can open a separate issue for this. |
Sounds correct - the linked PR won't fix this issue. I've added a test - #10380 (comment). In this case I guess a workaround (for analyzer authors) could be to filter out these mappings which point to lines that are greater than the following mappings since it should be impossible to have an actual overlapping code like that. For example: #nullable restore
#line (1,2)-(1,23) "Shared/Component1.razor"
using System.Net.Http // maps to line 1
#nullable disable
; // maps to line 3 👈 can filter this out since there's a mapping to line 2 below!
#nullable restore
#line (2,2)-(2,61) "Shared/Component1.razor" // maps to line 2
using static Microsoft.AspNetCore.Components.Web.RenderMode
#line default
#line hidden
#nullable disable
; // doesn't map anywhere thanks to the `#line hidden` |
Version Used:
.Net 9 preview SDK version 9.0.100-preview.3.24204.13
Steps to Reproduce:
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
in the csprojBugRepro.razor
The actual generated code looks like so:
After the
using System.Net.Http
the#line default #line hidden
directives are missing.The expected generated code should look like so:
Why is this a problem:
Because of the missing
#line default #line hidden
, the;
is mapped back to the razor file at a position two lines below the using. This line might be outside the number of lines of the razor files. This causes this error https://community.sonarsource.com/t/java-lang-illegalargumentexception-line-575-is-out-of-range-for-file-xxx-file-has-386-lines/108740/17 in our analyzer.See SonarSource/sonar-dotnet#9288 for more details and SonarSource/sonar-dotnet#9289 for two unit tests. One shows the problematic mapping and the other one shows that adding
#line default #line hidden
would fix the problem.The text was updated successfully, but these errors were encountered: