Skip to content

Commit

Permalink
Return early if magic source link URI has no path (#3056)
Browse files Browse the repository at this point in the history
### Motivation

This error was caught by our telemetry. If someone is manually typing a magic source link or if there's some error in Tapioca's generation, then the URI's path might be `nil`, which then fails when we pass it to `CGI.unescape` with a `no implicit conversion of nil to String`.

### Implementation

Started checking if there's a path before trying to unescape it.

### Automated Tests

Added a test that fails in main.
  • Loading branch information
vinistock authored Jan 13, 2025
1 parent c51471f commit cf93a83
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/ruby_lsp/listeners/document_link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ def extract_document_link(node)
gem_version = resolve_version(uri)
return if gem_version.nil?

file_path = self.class.gem_paths.dig(uri.gem_name, gem_version, CGI.unescape(uri.path))
path = uri.path
return unless path

file_path = self.class.gem_paths.dig(uri.gem_name, gem_version, CGI.unescape(path))
return if file_path.nil?

@response_builder << Interface::DocumentLink.new(
Expand Down
19 changes: 19 additions & 0 deletions test/requests/document_link_expectations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@ def run_expectations(source)
listener.perform
end

def test_magic_source_links_on_unsaved_files
source = <<~RUBY
# source://erb/#1
def bar
end
RUBY

with_server(source) do |server, uri|
server.process_message(
id: 1,
method: "textDocument/documentLink",
params: { textDocument: { uri: uri } },
)

server.pop_response
assert_empty(server.pop_response.response)
end
end

private

def substitute(original)
Expand Down

0 comments on commit cf93a83

Please sign in to comment.