Skip to content

Commit 7bf039b

Browse files
committed
Gracefully handle gem loading errors
The relaxing of the `childprocess` gem dependency constraint in 89dc99a resulted in us seeing the following error running `overcommit --run` in the local Overcommit repository: .../lib/bundler/runtime.rb:317:in `check_for_activated_spec!': You have already activated childprocess 0.6.3, but your Gemfile requires childprocess 0.7.0. Prepending `bundle exec` to your command may solve this. (Gem::LoadError) Since we didn't prepend `bundle exec` to the command, the version of `overcommit` that is executed is whichever is the latest version installed in the local set of installed gems. In the example above, I had Overcommit 0.39.1 installed, which depends on childprocess ~> 0.6.3. Thus `childprocess` 0.6.3 is loaded, and when we attempt to invoke `Bundler.setup` we get the `Gem::LoadError` referenced above since the local `Gemfile.lock` expects `childprocess` 0.7.0. We can work around this by catching this specific error and re-execing with `bundle exec` prepended to the command.
1 parent 89dc99a commit 7bf039b

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
* Use the `core.hooksPath` Git configuration option when installing hooks
99
* Gracefully handle binary files in `LineEndings` pre-commit hook
1010
* Relax `childprocess` dependency to allow 0.x
11+
* Gracefully handle gem loading errors when invoking Overcommit in a repo where
12+
the `gemfile` specified by the local `.overcommit.yml` references a gem
13+
version incompatible with the already-loaded Overcommit
1114

1215
## 0.39.1
1316

bin/overcommit

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ if gemfile = YAML.load_file('.overcommit.yml')['gemfile'] rescue nil
2222
puts "Problem loading '#{gemfile}': #{ex.message}"
2323
puts "Try running:\nbundle install --gemfile=#{gemfile}" if ex.is_a?(Bundler::GemNotFound)
2424
exit 78 # EX_CONFIG
25+
rescue Gem::LoadError => ex
26+
# Handle case where user is executing overcommit without `bundle exec` and
27+
# whose local Gemfile has a gem requirement that does not match a gem
28+
# requirement of the installed version of Overcommit.
29+
raise unless ex.message =~ /already activated/i
30+
exec('bundle', 'exec', $0, *ARGV)
2531
end
2632
end
2733
# rubocop:enable Style/RescueModifier

0 commit comments

Comments
 (0)