From c19d9568323489638ae3b6fd3884b93a0cd15933 Mon Sep 17 00:00:00 2001 From: Fred Snyder Date: Sun, 5 Nov 2023 06:27:48 -0500 Subject: [PATCH] Library#locate_ref returns nil for unresolved requires (#675) --- lib/solargraph/library.rb | 6 +++++- spec/fixtures/workspace/app.rb | 2 ++ spec/library_spec.rb | 11 +++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/solargraph/library.rb b/lib/solargraph/library.rb index fd83dfae1..dff701cb3 100644 --- a/lib/solargraph/library.rb +++ b/lib/solargraph/library.rb @@ -258,6 +258,10 @@ def locate_pins location api_map.locate_pins(location).map { |pin| pin.realize(api_map) } end + # Match a require reference to a file. + # + # @param location [Location] + # @return [Location, nil] def locate_ref location map = source_map_hash[location.filename] return if map.nil? @@ -268,7 +272,7 @@ def locate_ref location next unless source_map_hash.key?(full) return Location.new(full, Solargraph::Range.from_to(0, 0, 0, 0)) end - # api_map.yard_map.require_reference(pin.name) + nil rescue FileNotFoundError nil end diff --git a/spec/fixtures/workspace/app.rb b/spec/fixtures/workspace/app.rb index 77a627a0b..72698756b 100644 --- a/spec/fixtures/workspace/app.rb +++ b/spec/fixtures/workspace/app.rb @@ -1,2 +1,4 @@ +require 'not_a_file' + thing = Thing.new thing.do_thing diff --git a/spec/library_spec.rb b/spec/library_spec.rb index a8d1d8795..1f6ec4115 100644 --- a/spec/library_spec.rb +++ b/spec/library_spec.rb @@ -452,6 +452,17 @@ def bar; end expect(library.current).to be_nil end + describe '#locate_ref' do + it 'returns nil without a matching reference location' do + workspace = File.absolute_path(File.join('spec', 'fixtures', 'workspace')) + library = Solargraph::Library.load(workspace) + library.map! + location = Solargraph::Location.new(File.join(workspace, 'app.rb'), Solargraph::Range.from_to(0, 8, 0, 8)) + found = library.locate_ref(location) + expect(found).to be_nil + end + end + context 'unsynchronized' do let(:library) { Solargraph::Library.load File.absolute_path(File.join('spec', 'fixtures', 'workspace')) } let(:good_file) { File.join(library.workspace.directory, 'lib', 'thing.rb') }