Skip to content
This repository was archived by the owner on Apr 14, 2021. It is now read-only.

Commit

Permalink
Auto merge of #4244 - dtognazzini:fix-path-to-gemfile, r=indirect
Browse files Browse the repository at this point in the history
Fix the path to the Gemfile during evaluation.

This is a continuation of #3349 Following up per: #3349 (comment)

The issue here is that paths used with `Bundler::Dsl#gemspec` will only work when the Gemfile being evaluated is `Bundler.default_gemfile`.

Passing a Gemfile other than `Bundler.default_gemfile` to `Bundler::Dsl.evaluate` will break uses of `path:` options in the Gemfile.

These changes update `Bundler::Dsl` to remember the Gemfile passed into `eval_gemfile` and use it to resolve relative paths.
  • Loading branch information
homu committed Jan 31, 2016
2 parents bf42ae1 + 8f8c1c4 commit 2f87aaf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
13 changes: 11 additions & 2 deletions lib/bundler/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ def initialize
@env = nil
@ruby_version = nil
@gemspecs = []
@gemfile = nil
add_git_sources
end

def eval_gemfile(gemfile, contents = nil)
@gemfile = Pathname.new(gemfile)
contents ||= Bundler.read_file(gemfile.to_s)
instance_eval(contents, gemfile.to_s, 1)
rescue Exception => e
Expand All @@ -47,7 +49,7 @@ def gemspec(opts = nil)
glob = opts && opts[:glob]
name = opts && opts[:name] || "{,*}"
development_group = opts && opts[:development_group] || :development
expanded_path = File.expand_path(path, Bundler.default_gemfile.dirname)
expanded_path = gemfile_root.join(path)

gemspecs = Dir[File.join(expanded_path, "#{name}.gemspec")]

Expand Down Expand Up @@ -144,7 +146,9 @@ def git_source(name, &block)
end

def path(path, options = {}, &blk)
with_source(@sources.add_path_source(normalize_hash(options).merge("path" => Pathname.new(path))), &blk)
source_options = normalize_hash(options).merge("path" => Pathname.new(path), "root_path" => gemfile_root)
source = @sources.add_path_source(source_options)
with_source(source, &blk)
end

def git(uri, options = {}, &blk)
Expand Down Expand Up @@ -487,5 +491,10 @@ def parse_line_number_from_description
[trace_line, description]
end
end

def gemfile_root
@gemfile ||= Bundler.default_gemfile
@gemfile.dirname
end
end
end
12 changes: 7 additions & 5 deletions lib/bundler/source/path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Source
class Path < Source
autoload :Installer, "bundler/source/path/installer"

attr_reader :path, :options
attr_reader :path, :options, :root_path
attr_writer :name
attr_accessor :version

Expand All @@ -16,6 +16,8 @@ def initialize(options)
@allow_cached = false
@allow_remote = false

@root_path = options["root_path"] || Bundler.root

if options["path"]
@path = Pathname.new(options["path"])
@path = expand(@path) unless @path.relative?
Expand Down Expand Up @@ -77,7 +79,7 @@ def install(spec, force = false)
def cache(spec, custom_path = nil)
app_cache_path = app_cache_path(custom_path)
return unless Bundler.settings[:cache_all]
return if expand(@original_path).to_s.index(Bundler.root.to_s + "/") == 0
return if expand(@original_path).to_s.index(root_path.to_s + "/") == 0

unless @original_path.exist?
raise GemNotFound, "Can't cache gem #{version_message(spec)} because #{self} is missing!"
Expand Down Expand Up @@ -111,7 +113,7 @@ def expanded_path
end

def expand(somepath)
somepath.expand_path(Bundler.root)
somepath.expand_path(root_path)
rescue ArgumentError => e
Bundler.ui.debug(e)
raise PathError, "There was an error while trying to use the path " \
Expand Down Expand Up @@ -164,8 +166,8 @@ def load_spec_files
end

def relative_path
if path.to_s.start_with?(Bundler.root.to_s)
return path.relative_path_from(Bundler.root)
if path.to_s.start_with?(root_path.to_s)
return path.relative_path_from(root_path)
end
path
end
Expand Down
1 change: 1 addition & 0 deletions spec/bundler/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@
it "restores it after it's done" do
other_source = double("other-source")
allow(Bundler::Source::Rubygems).to receive(:new).and_return(other_source)
allow(Bundler).to receive(:default_gemfile).and_return(Pathname.new("./Gemfile"))

subject.source("https://other-source.org") do
subject.gem("dobry-pies", :path => "foo")
Expand Down

0 comments on commit 2f87aaf

Please sign in to comment.