vscode-csharp: Go to definition for a source generated file fails to open the generated file

Repro:

  1. Open Roslyn solution and wait for load
  2. Find any type with a definition in Syntax.xml.Syntax.Generated.cs - for example BaseTypeDeclarationSyntax here
  3. Expect navigate to definition location, instead throws an error with the following stack:
[Error - 3:46:20 PM] [LanguageServerHost] System.UriFormatException: Failed create URI from 'CSharpSyntaxGenerator\CSharpSyntaxGenerator.SourceGenerator\Syntax.xml.Syntax.Generated.cs'; original string: 'CSharpSyntaxGenerator\CSharpSyntaxGenerator.SourceGenerator\Syntax.xml.Syntax.Generated.cs'
 ---> 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 Microsoft.CodeAnalysis.LanguageServer.ProtocolConversions.CreateAbsoluteUri(String absolutePath) in /_/src/Features/LanguageServer/Protocol/Extensions/ProtocolConversions.cs:line 186
   --- End of inner exception stack trace ---
   at Microsoft.CodeAnalysis.LanguageServer.ProtocolConversions.CreateAbsoluteUri(String absolutePath) in /_/src/Features/LanguageServer/Protocol/Extensions/ProtocolConversions.cs:line 186
   at Microsoft.CodeAnalysis.LanguageServer.ProtocolConversions.<TextSpanToLocationAsync>g__ConvertTextSpanToLocation|30_0(Document document, TextSpan span, Boolean isStale, CancellationToken cancellationToken) in /_/src/Features/LanguageServer/Protocol/Extensions/ProtocolConversions.cs:line 491
   at Microsoft.CodeAnalysis.LanguageServer.ProtocolConversions.TextSpanToLocationAsync(Document document, TextSpan textSpan, Boolean isStale, Nullable`1 context, CancellationToken cancellationToken) in /_/src/Features/LanguageServer/Protocol/Extensions/ProtocolConversions.cs:line 503
   at Microsoft.CodeAnalysis.LanguageServer.Handler.AbstractGoToDefinitionHandler.GetDefinitionAsync(TextDocumentPositionParams request, Boolean typeOnly, RequestContext context, CancellationToken cancellationToken) in /_/src/Features/LanguageServer/Protocol/Handler/Definitions/AbstractGoToDefinitionHandler.cs:line 141
   at Microsoft.CommonLanguageServerProtocol.Framework.QueueItem`3.StartRequestAsync(TRequestContext context, CancellationToken cancellationToken) in /_/src/Features/LanguageServer/Microsoft.CommonLanguageServerProtocol.Framework/QueueItem.cs:line 176
[Error - 3:46:20 PM] Request textDocument/definition failed.
  Message: Failed create URI from 'CSharpSyntaxGenerator\CSharpSyntaxGenerator.SourceGenerator\Syntax.xml.Syntax.Generated.cs'; original string: 'CSharpSyntaxGenerator\CSharpSyntaxGenerator.SourceGenerator\Syntax.xml.Syntax.Generated.cs'
  Code: -32000 

About this issue

  • Original URL
  • State: open
  • Created 9 months ago
  • Reactions: 31
  • Comments: 24 (6 by maintainers)

Most upvoted comments

I get this bug on a Godot project too. Godot uses source generators. It happens when I try to use “jump to definition” or “find all references”, only where it involves generated code, it seems. In non-partial classes, it works fine.

@arsinclair This demonstrates why I should never give a promise of a ship date – I’m now on leave with the arrival of our new baby. 👶 We’ll have somebody else wrap up the work but we’re still handing that off.

FYI: this is also blocking any ‘Find all references’ functionality for me.

@jasonmalinowski two weeks have passed, any update here?

@hutterm It’s in code review now, so should land if not this week then next week.

To clarify a bit: there’s still more work to do to make Go to Definition to source generated files actually work. To give a bit of a sense how this works:

  1. The Roslyn server has to send down a custom URI to VS Code for source generated files, since they’re potentially virtual and not on disk.
  2. The Roslyn VS Code extension then needs to recognize this URI and go display the dynamic content and such.

The UriFormatException was because we had a place that wasn’t correctly doing the first step. So if you had find references that found 10 references, and just one happened to be in a source generated file, then we’d fail to create the URI properly and thus the entire request would fail. Which was icky. So we fixed that; the work for step 2 isn’t in, which is why @JeremyTCD is getting the error he’s seeing. But that’ll only happen if you actually need to open a generated file. In my example of Find References w/ 10 references, at least now you can view the other 9. 😄

I’m leaving this bug open until I actually fix the end-to-end though. I’ll retitle it.

This affects us in a project that uses RefitGenerator. It’s very problematic as it completely breaks CTRL+T (show all symbols).

[Error - 1:58:26 PM] [LanguageServerHost] System.UriFormatException: Failed create URI from 'InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IAccountService.g.cs'; original string: 'InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IAccountService.g.cs'
 ---> 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 Microsoft.CodeAnalysis.LanguageServer.ProtocolConversions.CreateAbsoluteUri(String absolutePath) in /_/src/Features/LanguageServer/Protocol/Extensions/ProtocolConversions.cs:line 186
   --- End of inner exception stack trace ---
   at Microsoft.CodeAnalysis.LanguageServer.ProtocolConversions.CreateAbsoluteUri(String absolutePath) in /_/src/Features/LanguageServer/Protocol/Extensions/ProtocolConversions.cs:line 186
   at Microsoft.CodeAnalysis.LanguageServer.ProtocolConversions.<TextSpanToLocationAsync>g__ConvertTextSpanToLocation|30_0(Document document, TextSpan span, Boolean isStale, CancellationToken cancellationToken) in /_/src/Features/LanguageServer/Protocol/Extensions/ProtocolConversions.cs:line 477
   at Microsoft.CodeAnalysis.LanguageServer.ProtocolConversions.TextSpanToLocationAsync(Document document, TextSpan textSpan, Boolean isStale, Nullable`1 context, CancellationToken cancellationToken) in /_/src/Features/LanguageServer/Protocol/Extensions/ProtocolConversions.cs:line 442
   at Microsoft.CodeAnalysis.LanguageServer.Handler.WorkspaceSymbolsHandler.LSPNavigateToCallback.AddItemAsync(Project project, INavigateToSearchResult result, CancellationToken cancellationToken) in /_/src/Features/LanguageServer/Protocol/Handler/Symbols/WorkspaceSymbolsHandler.cs:line 90
   at Microsoft.CodeAnalysis.NavigateTo.AbstractNavigateToSearchService.<>c__DisplayClass13_0.<<GetOnItemFoundCallback>b__0>d.MoveNext() in /_/src/Features/Core/Portable/NavigateTo/AbstractNavigateToSearchService.cs:line 37
--- End of stack trace from previous location ---
   at Microsoft.CodeAnalysis.NavigateTo.AbstractNavigateToSearchService.AddResultIfMatchAsync(DocumentId documentId, Document document, DeclaredSymbolInfo declaredSymbolInfo, PatternMatcher nameMatcher, PatternMatcher containerMatcher, DeclaredSymbolInfoKindSet kinds, Func`2 onResultFound, CancellationToken cancellationToken) in /_/src/Features/Core/Portable/NavigateTo/AbstractNavigateToSearchService.InProcess.cs:line 162
   at Microsoft.CodeAnalysis.NavigateTo.AbstractNavigateToSearchService.ProcessIndexAsync(DocumentId documentId, Document document, String patternName, String patternContainer, DeclaredSymbolInfoKindSet kinds, Func`2 onResultFound, TopLevelSyntaxTreeIndex index, CancellationToken cancellationToken) in /_/src/Features/Core/Portable/NavigateTo/AbstractNavigateToSearchService.InProcess.cs:line 123
   at Microsoft.CodeAnalysis.NavigateTo.AbstractNavigateToSearchService.ProcessDocumentAsync(Document document, String patternName, String patternContainer, DeclaredSymbolInfoKindSet kinds, Func`2 onResultFound, CancellationToken cancellationToken) in /_/src/Features/Core/Portable/NavigateTo/AbstractNavigateToSearchService.InProcess.cs:line 96
   at Microsoft.CodeAnalysis.NavigateTo.AbstractNavigateToSearchService.ProcessDocumentsAsync(Document searchDocument, String patternName, String patternContainer, DeclaredSymbolInfoKindSet kinds, Func`2 onResultFound, ISet`1 documents, CancellationToken cancellationToken) in /_/src/Features/Core/Portable/NavigateTo/AbstractNavigateToSearchService.InProcess.cs:line 83
   at Microsoft.CodeAnalysis.NavigateTo.AbstractNavigateToSearchService.SearchGeneratedDocumentsInCurrentProcessAsync(Project project, String pattern, IImmutableSet`1 kinds, Func`2 onResultFound, CancellationToken cancellationToken) in /_/src/Features/Core/Portable/NavigateTo/AbstractNavigateToSearchService.GeneratedDocumentSearch.cs:line 60
   at Microsoft.CodeAnalysis.NavigateTo.AbstractNavigateToSearchService.SearchGeneratedDocumentsAsync(Project project, String searchPattern, IImmutableSet`1 kinds, Document activeDocument, Func`2 onResultFound, CancellationToken cancellationToken) in /_/src/Features/Core/Portable/NavigateTo/AbstractNavigateToSearchService.GeneratedDocumentSearch.cs:line 42
   at Microsoft.CodeAnalysis.NavigateTo.NavigateToSearcher.<>c__DisplayClass19_0.<<ProcessOrderedProjectsAsync>g__SearchCoreAsync|0>d.MoveNext() in /_/src/Features/Core/Portable/NavigateTo/NavigateToSearcher.cs:line 322
--- End of stack trace from previous location ---
   at Microsoft.CodeAnalysis.NavigateTo.NavigateToSearcher.<>c__DisplayClass19_0.<<ProcessOrderedProjectsAsync>g__SearchCoreAsync|0>d.MoveNext() in /_/src/Features/Core/Portable/NavigateTo/NavigateToSearcher.cs:line 339
--- End of stack trace from previous location ---
   at Microsoft.CodeAnalysis.NavigateTo.NavigateToSearcher.ProcessOrderedProjectsAsync(Boolean parallel, ImmutableArray`1 orderedProjects, HashSet`1 seenItems, Func`4 processProjectAsync, CancellationToken cancellationToken) in /_/src/Features/Core/Portable/NavigateTo/NavigateToSearcher.cs:line 301
   at Microsoft.CodeAnalysis.NavigateTo.NavigateToSearcher.SearchAllProjectsAsync(Boolean isFullyLoaded, NavigateToSearchScope scope, CancellationToken cancellationToken) in /_/src/Features/Core/Portable/NavigateTo/NavigateToSearcher.cs:line 186
   at Microsoft.CodeAnalysis.NavigateTo.NavigateToSearcher.SearchAsync(Boolean searchCurrentDocument, NavigateToSearchScope scope, CancellationToken cancellationToken) in /_/src/Features/Core/Portable/NavigateTo/NavigateToSearcher.cs:line 130
   at Microsoft.CodeAnalysis.NavigateTo.NavigateToSearcher.SearchAsync(Boolean searchCurrentDocument, NavigateToSearchScope scope, CancellationToken cancellationToken) in /_/src/Features/Core/Portable/NavigateTo/NavigateToSearcher.cs:line 140
   at Microsoft.CodeAnalysis.LanguageServer.Handler.WorkspaceSymbolsHandler.HandleRequestAsync(WorkspaceSymbolParams request, RequestContext context, CancellationToken cancellationToken) in /_/src/Features/LanguageServer/Protocol/Handler/Symbols/WorkspaceSymbolsHandler.cs:line 69
   at Microsoft.CommonLanguageServerProtocol.Framework.QueueItem`3.StartRequestAsync(TRequestContext context, CancellationToken cancellationToken) in /_/src/Features/LanguageServer/Microsoft.CommonLanguageServerProtocol.Framework/QueueItem.cs:line 176
[Error - 1:58:26 PM] Request workspace/symbol failed.
  Message: Failed create URI from 'InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IAccountService.g.cs'; original string: 'InterfaceStubGeneratorV2\Refit.Generator.InterfaceStubGeneratorV2\IAccountService.g.cs'
  Code: -32000 

The latest update of v2.15.30 seems to have fixed the issue. Was able to find references without errors for a Godot (v4.2.1) project. Would like a second opinion from anyone else who was previously facing this issue.