Skip to content

Conversation

@ivarne
Copy link

@ivarne ivarne commented May 6, 2025

If the Location is created without referencing the SourceTree, the previous implementation of GetLocationTrimmed lost the file path resulting in diagnostic messages without clickable file paths.

This might not be relevant for this project (yet), but I'm copying the code for my own SourceGenerator and ran into this issue when adding diagnostics for AdditionalFiles where created the location using Location.Create instead of getting it from syntax.

PS: I'm pretty new to Roslyn, so I don't know whether the lineSpan always have the path, but the implementation of Location.GetDebuggerDisplay seems to indicate that it usually does.

If the Location is created without referencing the SourceTree, the previous implementation of GetLocationTrimmed lost the file path resulting in diagnostic messages without clickable files.
return Location.Create(location.SourceTree?.FilePath ?? string.Empty, location.SourceSpan, location.GetLineSpan().Span);
var lineSpan = location.GetLineSpan();

return Location.Create(lineSpan.Path ?? string.Empty, location.SourceSpan, lineSpan.Span);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that diagnostics are not clickable is due to a known issue with Roslyn not supporting incremental Location values. I'm curious about your fix though, does LineSpan.Path return something different compared to SourceTree.FilePath and does that address the issue in question?

Copy link
Author

@ivarne ivarne May 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem arrises when I use Location.Create before instantiating the EquatableDiagnostic, because Location.Create leaves SourceTree set to null and thus the filePath is not propagated through GetLocationTrimmed.

Eg:

new EquatableDiagnostic(
    diagnosticDescriptor,
    Location.Create(
        filePath,
        new TextSpan(startIndex, endIndex.HasValue ? endIndex.Value - startIndex : 0),
        new LinePositionSpan(
            GetLinePosition(startIndex, fileContent),
            GetLinePosition(endIndex ?? startIndex, fileContent)
        )
    )
)

the filePath i provide gets wiped out because it is stored in the span and not in the SourceTree (which is null)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, does this address the issue of diagnostics not being clickable though? I'm not sure that it does, because fundamentally the issue is with Location.Create producing external locations that are not clickable.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what you mean by "clickable". The important part for me is that the path I provide is visible in the terminal output. It's probably the VSCode terminal that makes the location clickable for me.

Are you talking about the diagnostic issues not showing up inline in the IDE editor?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was referring to this issue: dotnet/runtime#92509. Generally speaking, diagnostics pointing to locations created via Location.Create suffer from issues such as them not being suppressible.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants