dredd icon indicating copy to clipboard operation
dredd copied to clipboard

Ruby hooks aren't run in bundle context causing gem load error

Open dpashkevich opened this issue 4 years ago • 0 comments

Describe the bug

I'm trying to use Dredd with Ruby hooks with Ruby 2.6.2. dredd-hooks-ruby fails to run because of the json gem mismatch:

$ dredd-hooks-ruby $HOME/projects/my-project/dredd/dredd_hooks.rb
$HOME/projects/calendly/api-docs/dredd/dredd_hooks.rb
Traceback (most recent call last):
    26: from $HOME/.rbenv/versions/2.6.2/bin/dredd-hooks-ruby:23:in `<main>'
    25: from $HOME/.rbenv/versions/2.6.2/bin/dredd-hooks-ruby:23:in `load'
    24: from $HOME/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/dredd_hooks-0.1.2/bin/dredd-hooks-ruby:6:in `<top (required)>'
    23: from $HOME/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/dredd_hooks-0.1.2/lib/dredd_hooks/cli.rb:9:in `start'
    22: from $HOME/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/dredd_hooks-0.1.2/lib/dredd_hooks/file_loader.rb:5:in `load'
    21: from $HOME/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/dredd_hooks-0.1.2/lib/dredd_hooks/file_loader.rb:5:in `each'
    20: from $HOME/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/dredd_hooks-0.1.2/lib/dredd_hooks/file_loader.rb:7:in `block in load'
    19: from $HOME/.rbenv/versions/2.6.2/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    18: from $HOME/.rbenv/versions/2.6.2/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    17: from $HOME/projects/calendly/api-docs/dredd/dredd_hooks.rb:3:in `<top (required)>'
    16: from $HOME/.rbenv/versions/2.6.2/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    15: from $HOME/.rbenv/versions/2.6.2/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    14: from $HOME/projects/calendly/config/environment.rb:2:in `<top (required)>'
    13: from $HOME/projects/calendly/config/environment.rb:2:in `require_relative'
    12: from $HOME/projects/calendly/config/application.rb:1:in `<top (required)>'
    11: from $HOME/projects/calendly/config/application.rb:1:in `require_relative'
    10: from $HOME/projects/calendly/config/boot.rb:3:in `<top (required)>'
     9: from $HOME/.rbenv/versions/2.6.2/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
     8: from $HOME/.rbenv/versions/2.6.2/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
     7: from $HOME/.rbenv/versions/2.6.2/lib/ruby/2.6.0/bundler/setup.rb:10:in `<top (required)>'
     6: from $HOME/.rbenv/versions/2.6.2/lib/ruby/2.6.0/bundler.rb:107:in `setup'
     5: from $HOME/.rbenv/versions/2.6.2/lib/ruby/2.6.0/bundler/runtime.rb:26:in `setup'
     4: from $HOME/.rbenv/versions/2.6.2/lib/ruby/2.6.0/bundler/runtime.rb:26:in `map'
     3: from $HOME/.rbenv/versions/2.6.2/lib/ruby/2.6.0/forwardable.rb:230:in `each'
     2: from $HOME/.rbenv/versions/2.6.2/lib/ruby/2.6.0/forwardable.rb:230:in `each'
     1: from $HOME/.rbenv/versions/2.6.2/lib/ruby/2.6.0/bundler/runtime.rb:31:in `block in setup'
$HOME/.rbenv/versions/2.6.2/lib/ruby/2.6.0/bundler/runtime.rb:319:in `check_for_activated_spec!': You have already activated json 2.1.0, but your Gemfile requires json 1.8.6. Since json is a default gem, you can either remove your dependency on it or try updating to a newer version of bundler that supports json as a default gem. (Gem::LoadError)

json is a standard gem in recent versions of Ruby, so I can't uninstall json 2.1.0. I googled around, and the common advice is to run the command in the current project's bundle context. This runs successfully, however:

bundle exec dredd-hooks-ruby $HOME/projects/my-project/dredd/dredd_hooks.rb

To Reproduce

  • Install Ruby 2.6.2
  • Create a basic Gemfile in the project's root directory:
source 'https://rubygems.org'

ruby '2.6.2'

gem 'dredd_hooks', '0.1.2'
  • Create a dummy dredd_hooks.rb in project root:
require 'dredd_hooks/methods'

include DreddHooks::Methods

p 'Hello World'
  • Use the following Dredd config (most options unrelated to hooks shouldn't matter):
color: true
dry-run: null
hookfiles: dredd_hooks.rb
language: ruby
require: null
init: false
custom: {}
names: false
only: []
reporter: []
output: []
header: []
sorted: false
user: null
inline-errors: false
details: false
method: []
loglevel: warning
path: []
config: ./dredd.yml
blueprint: path/to/blueprint.yml # doesn't matter what's in the file, as long as it's valid
endpoint: 'http://localhost:3000/'

Expected behavior

Dredd successfully starts the ruby hook handler and proceeds with the tests.

What is in your dredd.yml?

See above.

What's your dredd --version output?

$ dredd --version
dredd v13.1.1 (Darwin 18.7.0; x64)

Does dredd --loglevel=debug uncover something?

No, but I've located the problem.

Can you send us failing test in a Pull Request?

Unrelated

Proposed solution

I think Dredd should always run dredd-hooks-ruby within bundle exec. This does mean a dependency on bundler, but it seems to make sense for real world Ruby projects.

The README for dredd-hooks-ruby even talls you to add the gem as a dependency to your Gemfile, instead of installing it globally as suggested in the main Dredd docs.

Workaround

The workaround appears simple, just update the language in dredd.yml to a custom command line:

language: bundle exec dredd-hooks-ruby

dpashkevich avatar Jun 05 '20 16:06 dpashkevich