csharp-language-server icon indicating copy to clipboard operation
csharp-language-server copied to clipboard

Go to definition of partial class involved with source generators errors

Open lem102 opened this issue 1 year ago • 4 comments

OS: Windows 10 Editor: emacs 29.3 LSP Client: eglot 1.17

When I try go to definition of one of the partial classes in my godot project, csharp-ls throws an exception:

jsonrpc-request: jsonrpc-error: "request id=4 failed:", (jsonrpc-error-code . -32603), (jsonrpc-error-message . "System.UriFormatException: Invalid URI: The format of the URI could not be determined.
   at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind, UriCreationOptions& creationOptions)
   at System.Uri..ctor(String uriString)
   at CSharpLanguageServer.Conversions.Uri.fromPath(String path) in /_//src/CSharpLanguageServer/Conversions.fs:line 25
   at CSharpLanguageServer.Conversions.Location.fromRoslynLocation(Location loc) in /_//src/CSharpLanguageServer/Conversions.fs:line 71
   at <StartupCode$CSharpLanguageServer>[email protected](Unit unitVar) in /_//src/CSharpLanguageServer/State/ServerRequestContext.fs:line 94
   at Microsoft.FSharp.Control.AsyncPrimitives.CallThenInvoke[T,TResult](AsyncActivation`1 ctxt, TResult result1, FSharpFunc`2 part2) in D:\\a\\_work\\1\\s\\src\\FSharp.Core\\async.fs:line 508
   at Microsoft.FSharp.Control.Trampoline.Execute(FSharpFunc`2 firstAction) in D:\\a\\_work\\1\\s\\src\\FSharp.Core\\async.fs:line 112"), (jsonrpc-error-data)

After adding some logging, I can see that this is because the locations from the Roslyn symbol refer to the class I have created, and the partial filepaths of Godot's source generated classes. The latter aren't valid URIs, so the above exception is thrown.

d:\programming\non-projects\Godot\2024NewGame\FollowMob.cs
Godot.SourceGenerators\Godot.SourceGenerators.ScriptPathAttributeGenerator\FollowMob_ScriptPath.generated.cs
Godot.SourceGenerators\Godot.SourceGenerators.ScriptSerializationGenerator\FollowMob_ScriptSerialization.generated.cs
Godot.SourceGenerators\Godot.SourceGenerators.ScriptMethodsGenerator\FollowMob_ScriptMethods.generated.cs
Godot.SourceGenerators\Godot.SourceGenerators.ScriptPropertyDefValGenerator\FollowMob_ScriptPropertyDefVal.generated.cs
Godot.SourceGenerators\Godot.SourceGenerators.ScriptPropertiesGenerator\FollowMob_ScriptProperties.generated.cs
Godot.SourceGenerators\Godot.SourceGenerators.ScriptSignalsGenerator\FollowMob_ScriptSignals.generated.cs

A similar error occurs on find references.

I'd like to submit a PR to sort this, but am currently at a loss on how to:

  1. Know that a location is for a source generated file (I could look for ".generated.cs" but not sure if this is a good idea).
  2. Get the correct filepath for the source generated classes.

Any pointers would be helpful 🙂

lem102 avatar Jul 21 '24 08:07 lem102

Hey @lem102

(1) I think this is what you are looking after: https://learn.microsoft.com/en-us/dotnet/api/microsoft.codeanalysis.documentinfo.isgenerated?view=roslyn-dotnet-4.7.0

(2) Most likely you need to figure the schema yourself and implement this. Note that there is a similar code where URIs are generated for source-code-from-metadata, so that may be helpful to you:

  • https://github.com/razzmatazz/csharp-language-server/blob/master/src/CSharpLanguageServer/State/ServerRequestContext.fs#L59

Please do not hesitate to ask! Also, -- this server is a bit of a hack -- feel free to improvise :)

razzmatazz avatar Jul 25 '24 06:07 razzmatazz

can confirm this also happens with windows 11, neovim v0.10.0 in a godot project for goto definition, implementations, references, etc

i've tried to fix this for a few days, but with no F# experience and poor lsp knowledge I didn't have any results

for anyone else trying this, it seems vscode-csharp faced the same issue (and possibly partially fixed?)

https://github.com/dotnet/vscode-csharp/issues/6445 https://github.com/dotnet/vscode-csharp/pull/5858

byddha avatar Aug 20 '24 16:08 byddha

I also suffer from this

repelliuss avatar Aug 29 '24 14:08 repelliuss

https://github.com/razzmatazz/csharp-language-server/pull/189 looks very much related to this issue, has been merged and should fix part of the problem–it does not show refs to generated code but does not break find-references at very least.

razzmatazz avatar Sep 27 '24 06:09 razzmatazz

The issue should be fixed with https://github.com/razzmatazz/csharp-language-server/pull/190 -- find references will actually find references in .cshtml files and the like (provided code generators use #file pragmas properly)

razzmatazz avatar Nov 29 '24 13:11 razzmatazz

This should have been fixed with 0.16.0:

  • https://github.com/razzmatazz/csharp-language-server/releases/tag/0.16.0

Please reopen if not

razzmatazz avatar Dec 24 '24 18:12 razzmatazz