Razor files: Generated code misses #line hidden for using directives
- Dominant language
- C#
- Stars
- 20.7k
- Forks
- 4.3k
- PR merge metrics
- PR metrics pending
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 `true` 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:
```cs
// ...
#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:
```cs
#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 https://github.com/SonarSource/sonar-dotnet/issues/9288 for more details and https://github.com/SonarSource/sonar-dotnet/pull/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.
Contributor guide
Assessment
This issue has not been assessed yet.