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

Commit

Permalink
Auto merge of #6266 - nilsding:init-check-dir-writable, r=colby-swandale
Browse files Browse the repository at this point in the history
[Init] Check if the current directory is writeable

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

The problem was that when running `bundle init` inside a directory which is not writable by the current user (e.g. `/` as demonstrated in #6219) Bundler prints out an `EACCES` error with a huge backtrace.  In the mentioned PR @segiddins suggested to print out a better error message.  This PR addresses that.

### What was your diagnosis of the problem?

See [this comment on said PR](#6219 (comment)).

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

My fix is simple: adding a check whether the current directory is writeable before trying to create `gems.rb`/`Gemfile`.  If that's not the case, print out an error and exit.

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

I chose this fix because... it was really simple to implement.

(cherry picked from commit c4b022c)
  • Loading branch information
bundlerbot authored and colby-swandale committed Apr 11, 2018
1 parent cd18777 commit 84cb6c1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/bundler/cli/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ def run
exit 1
end

unless File.writable?(Dir.pwd)
Bundler.ui.error "Can not create #{gemfile} as the current directory is not writable."
exit 1
end

if options[:gemspec]
gemspec = File.expand_path(options[:gemspec])
unless File.exist?(gemspec)
Expand Down
18 changes: 18 additions & 0 deletions spec/commands/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@
end
end

context "when the dir is not writable by the current user" do
let(:subdir) { "child_dir" }

it "notifies the user that it can not write to it" do
FileUtils.mkdir bundled_app(subdir)
# chmod a-w it
mode = File.stat(bundled_app(subdir)).mode ^ 0o222
FileUtils.chmod mode, bundled_app(subdir)

Dir.chdir bundled_app(subdir) do
bundle :init
end

expect(out).to include("directory is not writable")
expect(Dir[bundled_app("#{subdir}/*")]).to be_empty
end
end

context "when a gems.rb file exists in a parent directory", :bundler => ">= 2" do
let(:subdir) { "child_dir" }

Expand Down

0 comments on commit 84cb6c1

Please sign in to comment.