Skip to content

Commit

Permalink
Merge pull request rubygems#8412 from rubygems/deivid-rodriguez/prefe…
Browse files Browse the repository at this point in the history
…r-local-fixes

Fix `--prefer-local` not respecting default gems
  • Loading branch information
deivid-rodriguez authored Jan 17, 2025
2 parents d1ccc9f + 3df86cd commit 209b93a
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 6 deletions.
2 changes: 2 additions & 0 deletions bundler/lib/bundler/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ def remotely!

def prefer_local!
@prefer_local = true

sources.prefer_local!
end

# For given dependency list returns a SpecSet with Gemspec of all the required
Expand Down
2 changes: 1 addition & 1 deletion bundler/lib/bundler/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def generate_standalone_bundler_executable_stubs(spec, options = {})
def install(options)
standalone = options[:standalone]
force = options[:force]
local = options[:local]
local = options[:local] || options[:"prefer-local"]
jobs = installation_parallelization
spec_installations = ParallelInstaller.call(self, @definition.specs, jobs, standalone, force, local: local)
spec_installations.each do |installation|
Expand Down
2 changes: 2 additions & 0 deletions bundler/lib/bundler/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def can_lock?(spec)
spec.source == self
end

def prefer_local!; end

def local!; end

def local_only!; end
Expand Down
21 changes: 18 additions & 3 deletions bundler/lib/bundler/source/rubygems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def initialize(options = {})
@allow_remote = false
@allow_cached = false
@allow_local = options["allow_local"] || false
@prefer_local = false
@checksum_store = Checksum::Store.new

Array(options["remotes"]).reverse_each {|r| add_remote(r) }
Expand All @@ -30,13 +31,21 @@ def caches
@caches ||= [cache_path, *Bundler.rubygems.gem_cache]
end

def prefer_local!
@prefer_local = true
end

def local_only!
@specs = nil
@allow_local = true
@allow_cached = false
@allow_remote = false
end

def local_only?
@allow_local && !@allow_remote
end

def local!
return if @allow_local

Expand Down Expand Up @@ -139,9 +148,15 @@ def specs
index.merge!(cached_specs) if @allow_cached
index.merge!(installed_specs) if @allow_local

# complete with default specs, only if not already available in the
# index through remote, cached, or installed specs
index.use(default_specs) if @allow_local
if @allow_local
if @prefer_local
index.merge!(default_specs)
else
# complete with default specs, only if not already available in the
# index through remote, cached, or installed specs
index.use(default_specs)
end
end

index
end
Expand Down
4 changes: 4 additions & 0 deletions bundler/lib/bundler/source_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ def expired_sources?(replacement_sources)
different_sources?(lock_sources, replacement_sources)
end

def prefer_local!
all_sources.each(&:prefer_local!)
end

def local_only!
all_sources.each(&:local_only!)
end
Expand Down
7 changes: 6 additions & 1 deletion bundler/spec/cache/gems_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@
expect(out).to include("Using json #{default_json_version}")
end

it "does not use remote gems when installing with --prefer-local flag" do
install_gemfile %(source "https://gem.repo2"; gem 'json', '#{default_json_version}'), verbose: true, "prefer-local": true
expect(out).to include("Using json #{default_json_version}")
end

it "caches remote and builtin gems" do
install_gemfile <<-G
source "https://gem.repo2"
Expand Down Expand Up @@ -167,7 +172,7 @@
G

bundle :cache, raise_on_error: false
expect(exitstatus).to_not eq(0)
expect(last_command).to be_failure
expect(err).to include("json-#{default_json_version} is built in to Ruby, and can't be cached")
end
end
Expand Down
2 changes: 1 addition & 1 deletion bundler/spec/install/gemfile/git_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@
gem "foo", :git => "#{lib_path("foo-1.0")}"
G

expect(exitstatus).to_not eq(0)
expect(last_command).to be_failure
expect(err).to include("Bundler could not install a gem because it " \
"needs to create a directory, but a file exists " \
"- #{default_bundle_path("bundler")}")
Expand Down

0 comments on commit 209b93a

Please sign in to comment.