Skip to content

Commit f3b465f

Browse files
committed
Fix double-loading of constants in development
When developing Overcommit locally, if you ran `overcommit --run` you would encounter an issue where you would see a bunch of warnings displayed related to already-initialized constants, e.g. .../projects/overcommit/lib/overcommit/constants.rb:6: warning: already initialized constant Overcommit::HOME .../.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/overcommit-0.39.1/lib/overcommit/constants.rb:5: warning: previous definition of HOME was here This was ultimately caused by the fact that in the Gem specification (`overcommit.gemspec`) we were modifying the `$LOAD_PATH` to include the local repository's `lib` directory. This is problematic because if the user had run `gem install overcommit` previously, that version of Overcommit would already be in the `$LOAD_PATH`, e.g. > $LOAD_PATH.grep(/overcommit/) .../.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/overcommit-0.39.1/lib .../projects/overcommit/lib Notice that the local development directory under `projects` comes _after_ the version of Overcommit installed via `gem install ...`. A simple workaround is to not modify the `$LOAD_PATH` and to use `require_relative` instead, thus loading the constants directly when initializing the Gem specification.
1 parent bf6a8df commit f3b465f

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

overcommit.gemspec

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
$LOAD_PATH << File.expand_path('../lib', __FILE__)
2-
require 'overcommit/constants'
3-
require 'overcommit/version'
1+
require_relative './lib/overcommit/constants'
2+
require_relative './lib/overcommit/version'
43

54
Gem::Specification.new do |s|
65
s.name = 'overcommit'

0 commit comments

Comments
 (0)