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

Commit

Permalink
Merge #6761
Browse files Browse the repository at this point in the history
6761: fix error with path objects in array r=greysteil a=alexggordon

Thanks so much for the contribution!
To make reviewing this PR a bit easier, please fill out answers to the following questions.

### What was the end-user problem that led to this PR?

The problem was that `Pathname` instances in an array can not be sorted when there are string instances in the array also. Because of this, calling `.sort` before `.to_s` resulted in the error

```
[!] There was an error parsing `Gemfile`: comparison of Pathname with String failed. Bundler cannot continue.
```

you can easily see this issue doing
```
> require 'rails'
=> true
> [Pathname.new("/tmp/bundle")]
=> [#<Pathname:/tmp/bundle>]
> [Pathname.new("/tmp/bundle"), "test"]
=> [#<Pathname:/tmp/bundle>, "test"]
> [Pathname.new("/tmp/bundle"), "test"].sort
ArgumentError: comparison of Pathname with String failed
```

### What was your diagnosis of the problem?

`sort` was called before `map` in the warn message. 

### What is your fix for the problem, implemented in this PR?

We should call `map(&:to_s)` before calling sort, and add a test case for this scenario. 

### Why did you choose this fix out of the possible options?

Because it broke our production deploys. 

Co-authored-by: Alex Gordon <agordon@sessionm.com>
  • Loading branch information
bundlerbot and Alex Gordon committed Oct 25, 2018
2 parents 9d59fa4 + 30c4426 commit fbb9c2d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/bundler.rb
Expand Up @@ -371,7 +371,7 @@ def requires_sudo?
unwritable_files = files.reject {|f| File.writable?(f) }
sudo_needed = !unwritable_files.empty?
if sudo_needed
Bundler.ui.warn "Following files may not be writable, so sudo is needed:\n #{unwritable_files.sort.map(&:to_s).join("\n ")}"
Bundler.ui.warn "Following files may not be writable, so sudo is needed:\n #{unwritable_files.map(&:to_s).sort.join("\n ")}"
end
end

Expand Down
13 changes: 13 additions & 0 deletions spec/bundler/bundler_spec.rb
Expand Up @@ -378,9 +378,11 @@ def clear_cached_requires_sudo
before do
allow(Bundler).to receive(:which).with("sudo").and_return("/usr/bin/sudo")
FileUtils.mkdir_p("tmp/vendor/bundle")
FileUtils.mkdir_p("tmp/vendor/bin_dir")
end
after do
FileUtils.rm_rf("tmp/vendor/bundle")
FileUtils.rm_rf("tmp/vendor/bin_dir")
if Bundler.respond_to?(:remove_instance_variable)
Bundler.remove_instance_variable(:@requires_sudo_ran)
Bundler.remove_instance_variable(:@requires_sudo)
Expand All @@ -401,13 +403,24 @@ def clear_cached_requires_sudo
before do
FileUtils.touch("tmp/vendor/bundle/unwritable1.txt")
FileUtils.touch("tmp/vendor/bundle/unwritable2.txt")
FileUtils.touch("tmp/vendor/bin_dir/unwritable3.txt")
FileUtils.chmod(0o400, "tmp/vendor/bundle/unwritable1.txt")
FileUtils.chmod(0o400, "tmp/vendor/bundle/unwritable2.txt")
FileUtils.chmod(0o400, "tmp/vendor/bin_dir/unwritable3.txt")
end
it "should return true and display warn message" do
allow(Bundler).to receive(:bundle_path).and_return(Pathname("tmp/vendor/bundle"))
bin_dir = Pathname("tmp/vendor/bin_dir/")

# allow File#writable? to be called with args other than the stubbed on below
allow(File).to receive(:writable?).and_call_original

# fake make the directory unwritable
allow(File).to receive(:writable?).with(bin_dir).and_return(false)
allow(Bundler).to receive(:system_bindir).and_return(Pathname("tmp/vendor/bin_dir/"))
message = <<-MESSAGE.chomp
Following files may not be writable, so sudo is needed:
tmp/vendor/bin_dir/
tmp/vendor/bundle/unwritable1.txt
tmp/vendor/bundle/unwritable2.txt
MESSAGE
Expand Down

0 comments on commit fbb9c2d

Please sign in to comment.