Skip to content

Commit eb4b12b

Browse files
apiologyclaude
andcommitted
Fix Gemspecs#in_this_bundle? crash when no Gemfile is discoverable
Bundler.definition raises Bundler::GemfileNotFound (not nil) when no Gemfile is discoverable from the current process's working directory - e.g. when Solargraph is installed and invoked as a standalone gem. The safe-navigation chain in in_this_bundle? doesn't help since the exception happens while evaluating Bundler.definition itself, which crashed find_gem and, downstream, the CLI's unbundled-environment paths exercised by this PR's shell.rb wiring. Same fix as castwide#1225 (open upstream, not yet merged); included directly here so this PR's own CI is green without waiting on that PR to land first. Once castwide#1225 merges, a future rebase of this branch will see it as a no-op. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent cf5c8c8 commit eb4b12b

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

lib/solargraph/workspace/gemspecs.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,12 @@ def query_external_bundle command
220220
# @sg-ignore need boolish support for ? methods
221221
def in_this_bundle?
222222
Bundler.definition&.lockfile&.to_s&.start_with?(directory)
223+
rescue Bundler::GemfileNotFound
224+
# Solargraph itself isn't running under a discoverable Gemfile
225+
# (e.g. installed and invoked as a standalone gem), so it can't
226+
# be "this bundle" - fall back to treating the workspace as an
227+
# external bundle.
228+
false
223229
end
224230

225231
# @return [Array<Gem::Specification, Bundler::LazySpecification, Bundler::StubSpecification>]

spec/workspace/gemspecs_find_gem_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,23 @@
9797
end
9898
end
9999
end
100+
101+
context 'when Solargraph itself is not running under a discoverable Gemfile' do
102+
# Regression test: Bundler.definition raises Bundler::GemfileNotFound
103+
# (rather than returning nil) when no Gemfile is discoverable from the
104+
# current process, e.g. when Solargraph is installed and invoked as a
105+
# standalone gem. #in_this_bundle? must not let that exception escape.
106+
let(:dir_path) { File.realpath(Dir.mktmpdir) }
107+
let(:name) { 'solargraph' }
108+
let(:version) { nil }
109+
110+
before do
111+
allow(Bundler).to receive(:definition).and_raise(Bundler::GemfileNotFound, 'Could not locate Gemfile')
112+
end
113+
114+
it 'falls back to resolving gems ignoring any local bundle instead of raising' do
115+
expect { gemspec }.not_to raise_error
116+
expect(gemspec.name).to eq(name)
117+
end
118+
end
100119
end

0 commit comments

Comments
 (0)