Skip to content

Razor files: Generated code misses #line hidden for using directives #10375

Open
@martin-strecker-sonarsource

Description

Version Used:

.Net 9 preview SDK version 9.0.100-preview.3.24204.13

Steps to Reproduce:

  1. Create a Blazor App
  2. Enable <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> in the csproj
  3. Add BugRepro.razor
@using System.Net.Http
@using static Microsoft.AspNetCore.Components.Web.RenderMode

The actual generated code looks like so:

// ...
#nullable restore
#line (1,2)-(2,1) "C:\Projects\sonar-scanner-vsts-test\src\BlazorApp1\Components\BugRepro.razor"
using System.Net.Http

#nullable disable
    ;
#nullable restore
#line (2,2)-(2,61) "C:\Projects\sonar-scanner-vsts-test\src\BlazorApp1\Components\BugRepro.razor"
using static Microsoft.AspNetCore.Components.Web.RenderMode

#line default
#line hidden
#nullable disable
    ;
    #nullable restore
    public partial class BugRepro : global::Microsoft.AspNetCore.Components.ComponentBase
    #nullable disable
// ...

After the using System.Net.Http the #line default #line hidden directives are missing.
The expected generated code should look like so:

#nullable restore
#line (1,2)-(2,1) "C:\Projects\sonar-scanner-vsts-test\src\BlazorApp1\Components\BugRepro.razor"
using System.Net.Http

#line default
#line hidden
#nullable disable
    ;
#nullable restore
#line (2,2)-(2,61) "C:\Projects\sonar-scanner-vsts-test\src\BlazorApp1\Components\BugRepro.razor"
using static Microsoft.AspNetCore.Components.Web.RenderMode

#line default
#line hidden
#nullable disable
    ;
    #nullable restore
    public partial class BugRepro : global::Microsoft.AspNetCore.Components.ComponentBase
    #nullable disable
// ...

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions