SonarSource / sonar-dotnet

Code analyzer for C# and VB.NET projects
https://redirect.sonarsource.com/plugins/csharp.html
GNU Lesser General Public License v3.0
788 stars 226 forks source link

Razor: SymbolReference locations for @typeparam are misplaced #8417

Open martin-strecker-sonarsource opened 11 months ago

martin-strecker-sonarsource commented 11 months ago

Based on this community report.

Description

The symbol reference for @typeparam symbols are miss-placed when compiled with the .Net8 SDK.

Repro steps

.Net 8 BlazorWebAssembly

<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
    <PropertyGroup>
        <ProjectGuid>{1250CC30-A343-4A10-A1DE-B1792ADB6244}</ProjectGuid>
        <TargetFramework>net8.0</TargetFramework>
        <RuntimeIdentifier>browser-wasm</RuntimeIdentifier>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.0" />
        <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.0" PrivateAssets="all" />
        <PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="8.0.0" />
    </ItemGroup>
</Project>

With TestComponent.razor

@typeparam TSomeVeryLongName where TSomeVeryLongName : class

Run the SonarAnalyzsis and generate the .sonarqube\out\0\output-cs\symrefs.pb file, like e.g. this one: symrefs.zip

{
   "filePath":"C:\\Users\\martin.strecker\\Desktop\\Bench\\Blazor\\TestComponent.razor",
   "reference":[
      {
         "declaration":{
            "startLine":1,
            "endLine":1,
            "startOffset":0,
            "endOffset":17
         },
         "reference":[
            {
               "startLine":1,
               "endLine":1,
               "startOffset":6,
               "endOffset":23
            }
         ]
      }
   ]
}

Expected behavior

The declaration range should be 12-29 and the reference range should be 36-53.

@typeparam TSomeVeryLongName where TSomeVeryLongName : class
           ^^^^^^ Decl ^^^^^       ^^^^^ Ref ^^^^^^^

Actual behavior

The declaration range is 0-17 and the reference range is 6-23 and overlaps.

@typeparam TSomeVeryLongName where TSomeVeryLongName : class
^^^^^^ Decl ^^^^^
    ^^^^^ Ref ^^^^^^^

Known workarounds

Use .Net 7 SDK (no Symbol references are created). or add spaces between where and the type parameter reference:

@typeparam TSomeVeryLongName where                          TSomeVeryLongName : class
vedion commented 10 months ago

I can see two pull requests have been merged with master, does that mean this issue is fixed now?

martin-strecker-sonarsource commented 10 months ago

We fixed the failure, so the end step is no longer failing. We did so by skipping symbols that overlapped. We keep this ticket open because the root cause still needs to be fixed.

pavel-mikula-sonarsource commented 7 months ago

This has been fixed in Roslyn 4.9.2 that was released last week

pavel-mikula-sonarsource commented 7 months ago

Ah, probably not yet. While the reference is present, it probably doesn't have the correct offset. @martin-strecker-sonarsource can you try to check?

See #8890 and changes in SymbolReferenceAnalyzerTest

martin-strecker-sonarsource commented 7 months ago

Yes. The behavior we see in SymbolReferenceAnalyzerTest is the overlapping bug. It is under test in the .Net 8 UTs and with the upgrade of Roslyn it is trickling down to the "normal" UTs as well. The comment in the UT describes the wrong behavior we expect to see with the upgrade (we see the (wrong) 6-23 reference that overlaps with it's declaration 0-17): https://github.com/SonarSource/sonar-dotnet/blob/db66eb7d1e58ec79390decd4d4a500874f9840e5/analyzers/tests/SonarAnalyzer.Test/Rules/Utilities/SymbolReferenceAnalyzerTest.cs#L241-L246

martin-strecker-sonarsource commented 7 months ago

I reported the problem in https://github.com/dotnet/razor/issues/10087