From f631eca81183e58e0a1672516a32e5b614e379e5 Mon Sep 17 00:00:00 2001 From: Artyom Khramov Date: Sun, 12 Nov 2017 03:48:11 +0600 Subject: [PATCH] Make `bundle clean` clean extension directories `bundle clean` command doesn't remove gem extensions, because extensions reside out of gem directories. This change tweaks `Bundler::Runtime#clean` to remove extensions dirs as well. --- lib/bundler/runtime.rb | 10 ++++++++-- spec/commands/clean_spec.rb | 35 +++++++++++++++++++++++++++++++++++ spec/support/builders.rb | 1 + 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/lib/bundler/runtime.rb b/lib/bundler/runtime.rb index f27597b8542..000781ca06b 100644 --- a/lib/bundler/runtime.rb +++ b/lib/bundler/runtime.rb @@ -163,6 +163,7 @@ def clean(dry_run = false) gem_dirs = Dir["#{Gem.dir}/gems/*"] gem_files = Dir["#{Gem.dir}/cache/*.gem"] gemspec_files = Dir["#{Gem.dir}/specifications/*.gemspec"] + extension_dirs = Dir["#{Gem.dir}/extensions/*/*/*"] spec_gem_paths = [] # need to keep git sources around spec_git_paths = @definition.spec_git_paths @@ -170,6 +171,7 @@ def clean(dry_run = false) spec_gem_executables = [] spec_cache_paths = [] spec_gemspec_paths = [] + spec_extension_paths = [] specs.each do |spec| spec_gem_paths << spec.full_gem_path # need to check here in case gems are nested like for the rails git repo @@ -181,6 +183,7 @@ def clean(dry_run = false) end spec_cache_paths << spec.cache_file spec_gemspec_paths << spec.spec_file + spec_extension_paths << spec.extension_dir if spec.respond_to?(:extension_dir) spec_git_cache_dirs << spec.source.cache_path.to_s if spec.source.is_a?(Bundler::Source::Git) end spec_gem_paths.uniq! @@ -192,6 +195,7 @@ def clean(dry_run = false) stale_gem_dirs = gem_dirs - spec_gem_paths stale_gem_files = gem_files - spec_cache_paths stale_gemspec_files = gemspec_files - spec_gemspec_paths + stale_extension_dirs = extension_dirs - spec_extension_paths removed_stale_gem_dirs = stale_gem_dirs.collect {|dir| remove_dir(dir, dry_run) } removed_stale_git_dirs = stale_git_dirs.collect {|dir| remove_dir(dir, dry_run) } @@ -204,8 +208,10 @@ def clean(dry_run = false) FileUtils.rm(file) if File.exist?(file) end end - stale_git_cache_dirs.each do |cache_dir| - SharedHelpers.filesystem_access(cache_dir) do |dir| + + stale_dirs = stale_git_cache_dirs + stale_extension_dirs + stale_dirs.each do |stale_dir| + SharedHelpers.filesystem_access(stale_dir) do |dir| FileUtils.rm_rf(dir) if File.exist?(dir) end end diff --git a/spec/commands/clean_spec.rb b/spec/commands/clean_spec.rb index d0df6d30d7f..ff3317bb1d9 100644 --- a/spec/commands/clean_spec.rb +++ b/spec/commands/clean_spec.rb @@ -733,4 +733,39 @@ def should_not_have_gems(*gems) expect(vendored_gems("bundler/gems/extensions")).to exist expect(vendored_gems("bundler/gems/very_simple_git_binary-1.0-#{revision[0..11]}")).to exist end + + it "removes extension directories", :rubygems => "2.2" do + gemfile <<-G + source "file://#{gem_repo1}" + + gem "thin" + gem "very_simple_binary" + gem "simple_binary" + G + + bundle! "install", forgotten_command_line_options(:path => "vendor/bundle") + + very_simple_binary_extensions_dir = + Pathname.glob("#{vendored_gems}/extensions/*/*/very_simple_binary-1.0").first + + simple_binary_extensions_dir = + Pathname.glob("#{vendored_gems}/extensions/*/*/simple_binary-1.0").first + + expect(very_simple_binary_extensions_dir).to exist + expect(simple_binary_extensions_dir).to exist + + gemfile <<-G + source "file://#{gem_repo1}" + + gem "thin" + gem "simple_binary" + G + + bundle! "install" + bundle! :clean + expect(out).to eq("Removing very_simple_binary (1.0)") + + expect(very_simple_binary_extensions_dir).not_to exist + expect(simple_binary_extensions_dir).to exist + end end diff --git a/spec/support/builders.rb b/spec/support/builders.rb index 377ca35523c..c2ab183fdff 100644 --- a/spec/support/builders.rb +++ b/spec/support/builders.rb @@ -182,6 +182,7 @@ def build_repo1 end build_gem "very_simple_binary", &:add_c_extension + build_gem "simple_binary", &:add_c_extension build_gem "bundler", "0.9" do |s| s.executables = "bundle"