Skip to content

Commit 97079f0

Browse files
authored
Fix __FILE__ and __dir__ in primary Gemfiles (#193)
Gemfile classes call instance_eval without telling Ruby what file the string came from, and so Ruby thinks that FILE should point to `appraisal/lib/appraisal/gemfile.rb` instead of the Gemfile itself, and that breaks anything inside the Gemfile that references either __FILE__ or __dir__. This fixes the case where `ruby Pathname.new(__dir__).join(".ruby-version").read` is used in the original `Gemfile` across Appraisals.
1 parent 2f5be65 commit 97079f0

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

lib/appraisal/gemfile.rb

+4-6
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,17 @@ module Appraisal
1212
# Load bundler Gemfiles and merge dependencies
1313
class Gemfile < BundlerDSL
1414
def load(path)
15-
if File.exist?(path)
16-
run(IO.read(path))
17-
end
15+
run(IO.read(path), path) if File.exist?(path)
1816
end
1917

20-
def run(definitions)
21-
instance_eval(definitions, __FILE__, __LINE__) if definitions
18+
def run(definitions, path, line = 1)
19+
instance_eval(definitions, path, line) if definitions
2220
end
2321

2422
def dup
2523
Gemfile.new.tap do |gemfile|
2624
gemfile.git_sources = @git_sources
27-
gemfile.run(for_dup)
25+
gemfile.run(for_dup, __FILE__, __LINE__)
2826
end
2927
end
3028
end

spec/appraisal/gemfile_spec.rb

+9
Original file line numberDiff line numberDiff line change
@@ -430,4 +430,13 @@
430430
expect(gemfile.to_s).to eq %(gem "bacon", git: "../path/bacon_pancake")
431431
end
432432
end
433+
434+
it "preserves the Gemfile's __FILE__" do
435+
gemfile = Appraisal::Gemfile.new
436+
Tempfile.open do |tmpfile|
437+
tmpfile.write "__FILE__"
438+
tmpfile.rewind
439+
expect(gemfile.load(tmpfile.path)).to include(File.dirname(tmpfile.path))
440+
end
441+
end
433442
end

0 commit comments

Comments
 (0)