razzmatazz / csharp-language-server

Roslyn-based LSP language server for C#
MIT License
574 stars 37 forks source link

Go to definition of partial class involved with source generators errors #178

Open lem102 opened 1 month ago

lem102 commented 1 month ago

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>.$ServerRequestContext.ResolveSymbolLocation@53.Invoke(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 🙂

razzmatazz commented 1 month ago

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:

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

drzbida commented 2 weeks ago

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

repelliuss commented 1 week ago

I also suffer from this