From a583f771be033fe80903382ebc19fb942673ad02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Thu, 1 Oct 2020 13:02:02 +0200 Subject: [PATCH] Merge pull request #3854 from rubygems/brackets Fix some cases of running `bundler` on a path including brackets (cherry picked from commit 9860c262452e54f5a5fab54c470ea03ea0d371e8) --- bundler/lib/bundler/dsl.rb | 2 +- bundler/lib/bundler/gem_helper.rb | 4 ++-- bundler/lib/bundler/rubygems_ext.rb | 16 ++++++++++++++++ bundler/lib/bundler/source/path.rb | 2 +- bundler/spec/commands/install_spec.rb | 18 +++++++++++++++++- .../realworld/fixtures/warbler/Gemfile.lock | 4 ++-- bundler/spec/runtime/gem_tasks_spec.rb | 12 ++++++++++++ 7 files changed, 51 insertions(+), 7 deletions(-) diff --git a/bundler/lib/bundler/dsl.rb b/bundler/lib/bundler/dsl.rb index cbe7b6bb8efd..1cc7908b8a88 100644 --- a/bundler/lib/bundler/dsl.rb +++ b/bundler/lib/bundler/dsl.rb @@ -63,7 +63,7 @@ def gemspec(opts = nil) development_group = opts[:development_group] || :development expanded_path = gemfile_root.join(path) - gemspecs = Dir[File.join(expanded_path, "{,*}.gemspec")].map {|g| Bundler.load_gemspec(g) }.compact + gemspecs = Gem::Util.glob_files_in_dir("{,*}.gemspec", expanded_path).map {|g| Bundler.load_gemspec(g) }.compact gemspecs.reject! {|s| s.name != name } if name Index.sort_specs(gemspecs) specs_by_name_and_version = gemspecs.group_by {|s| [s.name, s.version] } diff --git a/bundler/lib/bundler/gem_helper.rb b/bundler/lib/bundler/gem_helper.rb index 739fae1b2951..4ada36aef487 100644 --- a/bundler/lib/bundler/gem_helper.rb +++ b/bundler/lib/bundler/gem_helper.rb @@ -32,7 +32,7 @@ def gemspec(&block) def initialize(base = nil, name = nil) @base = File.expand_path(base || SharedHelpers.pwd) - gemspecs = name ? [File.join(@base, "#{name}.gemspec")] : Dir[File.join(@base, "{,*}.gemspec")] + gemspecs = name ? [File.join(@base, "#{name}.gemspec")] : Gem::Util.glob_files_in_dir("{,*}.gemspec", @base) raise "Unable to determine name from existing gemspec. Use :name => 'gemname' in #install_tasks to manually set it." unless gemspecs.size == 1 @spec_path = gemspecs.first @gemspec = Bundler.load_gemspec(@spec_path) @@ -111,7 +111,7 @@ def rubygem_push(path) end def built_gem_path - Dir[File.join(base, "#{name}-*.gem")].sort_by {|f| File.mtime(f) }.last + Gem::Util.glob_files_in_dir("#{name}-*.gem", base).sort_by {|f| File.mtime(f) }.last end def git_push(remote = nil) diff --git a/bundler/lib/bundler/rubygems_ext.rb b/bundler/lib/bundler/rubygems_ext.rb index 82816d24fab2..0322b06d0787 100644 --- a/bundler/lib/bundler/rubygems_ext.rb +++ b/bundler/lib/bundler/rubygems_ext.rb @@ -173,6 +173,22 @@ def hash undef_method :eql? if method_defined? :eql? alias_method :eql?, :== end + + require "rubygems/util" + + Util.singleton_class.module_eval do + if Util.singleton_methods.include?(:glob_files_in_dir) # since 3.0.0.beta.2 + remove_method :glob_files_in_dir + end + + def glob_files_in_dir(glob, base_path) + if RUBY_VERSION >= "2.5" + Dir.glob(glob, :base => base_path).map! {|f| File.expand_path(f, base_path) } + else + Dir.glob(File.join(base_path.to_s.gsub(/[\[\]]/, '\\\\\\&'), glob)).map! {|f| File.expand_path(f) } + end + end + end end module Gem diff --git a/bundler/lib/bundler/source/path.rb b/bundler/lib/bundler/source/path.rb index 29e01e93ed2f..2c2e9023b3d4 100644 --- a/bundler/lib/bundler/source/path.rb +++ b/bundler/lib/bundler/source/path.rb @@ -171,7 +171,7 @@ def load_spec_files if File.directory?(expanded_path) # We sort depth-first since `<<` will override the earlier-found specs - Dir["#{expanded_path}/#{@glob}"].sort_by {|p| -p.split(File::SEPARATOR).size }.each do |file| + Gem::Util.glob_files_in_dir(@glob, expanded_path).sort_by {|p| -p.split(File::SEPARATOR).size }.each do |file| next unless spec = load_gemspec(file) spec.source = self diff --git a/bundler/spec/commands/install_spec.rb b/bundler/spec/commands/install_spec.rb index 0d17e7900eaf..d1b8585114b3 100644 --- a/bundler/spec/commands/install_spec.rb +++ b/bundler/spec/commands/install_spec.rb @@ -511,7 +511,7 @@ end describe "when Bundler root contains regex chars" do - it "doesn't blow up" do + it "doesn't blow up when using the `gem` DSL" do root_dir = tmp("foo[]bar") FileUtils.mkdir_p(root_dir) @@ -526,6 +526,22 @@ bundle :install, :dir => root_dir end + + it "doesn't blow up when using the `gemspec` DSL" do + root_dir = tmp("foo[]bar") + + FileUtils.mkdir_p(root_dir) + + build_lib "foo", :path => root_dir + gemfile = <<-G + gemspec + G + File.open("#{root_dir}/Gemfile", "w") do |file| + file.puts gemfile + end + + bundle :install, :dir => root_dir + end end describe "when requesting a quiet install via --quiet" do diff --git a/bundler/spec/realworld/fixtures/warbler/Gemfile.lock b/bundler/spec/realworld/fixtures/warbler/Gemfile.lock index a524f6ef30d3..bcf0799494ad 100644 --- a/bundler/spec/realworld/fixtures/warbler/Gemfile.lock +++ b/bundler/spec/realworld/fixtures/warbler/Gemfile.lock @@ -6,7 +6,7 @@ PATH GEM remote: https://rubygems.org/ specs: - jruby-jars (9.2.9.0) + jruby-jars (9.2.11.1) jruby-rack (1.1.21) rake (13.0.1) rubyzip (1.3.0) @@ -26,4 +26,4 @@ DEPENDENCIES warbler (~> 2.0) BUNDLED WITH - 2.2.0.rc.1 + 2.2.0.rc.2 diff --git a/bundler/spec/runtime/gem_tasks_spec.rb b/bundler/spec/runtime/gem_tasks_spec.rb index b2f9ae725e95..b0ef0cc144c3 100644 --- a/bundler/spec/runtime/gem_tasks_spec.rb +++ b/bundler/spec/runtime/gem_tasks_spec.rb @@ -67,6 +67,18 @@ end end + context "rake build when path has brackets", :ruby_repo do + before do + bracketed_bundled_app = tmp.join("bundled[app") + FileUtils.cp_r bundled_app, bracketed_bundled_app + bundle "exec rake build", :dir => bracketed_bundled_app + end + + it "still runs successfully" do + expect(err).to be_empty + end + end + context "bundle path configured locally" do before do bundle "config set path vendor/bundle"