Skip to content

Commit 3d19c76

Browse files
hsbtclaude
andcommitted
Expand the git source gemspec path before the chdir
`load_gemspec` changed into the gemspec's directory and then passed the still-relative path to `Gem::StubSpecification.gemspec_stub`, which reads it while that chdir is active, so a relative path resolved one directory too deep. Expanding once up front keeps the absolute-path requirement in the code instead of relying on every caller to satisfy it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent d6b1dde commit 3d19c76

2 files changed

Lines changed: 32 additions & 4 deletions

File tree

lib/bundler/source/git.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,12 @@ def fetch
432432
def validate_spec(_spec); end
433433

434434
def load_gemspec(file)
435-
dirname = Pathname.new(file).dirname
436-
SharedHelpers.chdir(dirname.to_s) do
437-
stub = Gem::StubSpecification.gemspec_stub(file, install_path.parent, install_path.parent)
438-
stub.full_gem_path = dirname.expand_path(root).to_s
435+
# Expand the path before the chdir below, since resolving it inside the
436+
# block would base it on the gemspec directory instead of `root`.
437+
gemspec_path = Pathname.new(file).expand_path(root)
438+
SharedHelpers.chdir(gemspec_path.dirname.to_s) do
439+
stub = Gem::StubSpecification.gemspec_stub(gemspec_path.to_s, install_path.parent, install_path.parent)
440+
stub.full_gem_path = gemspec_path.dirname.to_s
439441
StubSpecification.from_stub(stub)
440442
end
441443
end

spec/bundler/source/git_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,30 @@
149149
expect(::Bundler::FileUtils).to have_received(:rm_rf).once
150150
end
151151
end
152+
153+
describe "#load_gemspec" do
154+
let(:options) do
155+
{ "uri" => uri, "revision" => "123abc" }
156+
end
157+
158+
before do
159+
allow(Bundler).to receive(:root).and_return(tmp)
160+
allow(subject).to receive(:install_path).and_return(tmp("install/bar-123abc"))
161+
162+
create_file(tmp("bar/bar.gemspec"), <<~GEMSPEC)
163+
Gem::Specification.new do |s|
164+
s.name = "bar"
165+
s.version = "1.0"
166+
end
167+
GEMSPEC
168+
end
169+
170+
it "resolves a relative path against the root, not the gemspec directory" do
171+
spec = Dir.chdir(tmp) { subject.send(:load_gemspec, "bar/bar.gemspec") }
172+
173+
expect(spec.name).to eq("bar")
174+
expect(spec.loaded_from).to eq(tmp("bar/bar.gemspec").to_s)
175+
expect(spec.full_gem_path).to eq(tmp("bar").to_s)
176+
end
177+
end
152178
end

0 commit comments

Comments
 (0)