dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.96k stars 4.03k forks source link

Improve file locations in the Roslyn Sarif reports for source generator files #51773

Open costin-zaharia-sonarsource opened 3 years ago

costin-zaharia-sonarsource commented 3 years ago

Hello,

In the Roslyn Sarif reports the file locations for source generator code are not pointing to the right location on disk, even if EmitCompilerGeneratedFiles is set to true.

In SonarSource we parse these reports in order to import the raised issues and having the full path in the report (similar with the one for non generated code) will help us and the community write better tools which handle these files.

Expected Behavior:

{
  "ruleId": "S1144",
  "level": "warning",
  "message": "Remove the unused private method 'UnusedMethod'.",
  "locations": [
    {
      "resultFile": {
        "uri": "file:///c:/src/SourceGeneratorPOC\SourceGeneratorPOC\obj\Debug\netcoreapp3.1\generated\SourceGeneratorPOC.Generators\SourceGeneratorPOC.SourceGenerator\Greetings.cs",
        "region": {
          "startLine": 7,
          "startColumn": 9,
          "endLine": 7,
          "endColumn": 47
        }
      }
    }
  ],
}

Actual Behavior:

{
  "ruleId": "S1144",
  "level": "warning",
  "message": "Remove the unused private method 'UnusedMethod'.",
  "locations": [
    {
      "resultFile": {
        "uri": "SourceGeneratorPOC.Generators/SourceGeneratorPOC.SourceGenerator/Greetings.cs",
        "region": {
          "startLine": 7,
          "startColumn": 9,
          "endLine": 7,
          "endColumn": 47
        }
      }
    }
  ],
}

Thank you!

Youssef1313 commented 3 years ago

Issue could be around https://github.com/dotnet/roslyn/blob/6da1274c9d24c2f90a48290394a951b23617f2a3/src/Compilers/Core/Portable/SourceGeneration/GeneratorDriver.cs#L392

The full path used to write the file is retrieved here:

https://github.com/dotnet/roslyn/blob/f2d3bd1bbf019fdc34ba84ceca09200a89e1f1e3/src/Compilers/Core/Portable/CommandLine/CommonCompiler.cs#L994

I'm not able to find a way to access GeneratedFilesOutputDirectory from GeneratorDriver to have the full path assigned to SyntaxTree.FilePath.