Why is GitHub Actions unable to find rubocop and httparty gems?

382 Views Asked by At

I'm trying to develop a Ruby script that does some API calls using a couple of Classes that use the httparty gem in another rb file in a separate directory like this:

./script.rb
./lib
./lib/calls.rb

The code is actually more extensive but for the sake of simplicity here is a minimalist description:

script.rb

#!/usr/bin/env ruby
require_relative 'lib/calls'

service = Service.new
response = service.get
puts response

lib/calls.rb

require 'httparty'

class Service
  include HTTParty
  base_uri 'https://api.service.com/v1/endpoint'

  def get
    self.class.get('/')
  end
end

That works great! Now I'm trying to run it using GitHub Actions and I get this error:

<internal:/opt/hostedtoolcache/Ruby/3.2.2/x64/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:85:in `require': cannot load such file -- httparty (LoadError)
    from <internal:/opt/hostedtoolcache/Ruby/3.2.2/x64/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
    from /home/runner/work/test/test/lib/calls.rb:1:in `<top (required)>'
    from ./script.rb:2:in `require_relative'
    from ./script.rb:2:in `<main>'

I've read somewhere about this issue happening if you use require 'HTTParty with caps but I'm using it with all lowercase, so that's not it.

Another interesting thing that I believe might be related to is that if I try to run rubocop directly in the command line in GitHub Actions I get this:

Run rubocop --parallel
/home/runner/work/_temp/b235a1be-84fe-430b-91fb-4118542d46a3.sh: line 1: rubocop: command not found
Error: Process completed with exit code 127.

Both rubocop and httparty have been installed, of course, and rubocop runs if I use bundle exec rubocop, but locally I can run it using just rubocop.

EDIT: yaml file from GitHub Actions and confirmation that the gems were installed:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
      - name: Install Ruby and gems
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: 3.2.2
          bundler-cache: true
      - name: Script
        run: ./script.rb
Run bundler install
...
Using httparty 0.21.0
...
Using rubocop 1.50.2
...
```
2

There are 2 best solutions below

0
Alvaro On

Fixed it by using bundle exec before running a command in GitHub Actions.

 - name: Script
        run: bundle exec script.rb
0
shuirong On

The solution is to set bundler-cache: false.


I encountered the same issue today, and I followed Google to come here.

Unfortunately, the solution for Alvaro's problem doesn't work for me. But fortunately, I think I may have found the reason: the issue lies with the setting bundler-cache: true.

I believe the error occurs as follows: when the Action uses ruby/setup-ruby@v1, it actually executes a bundle install command inside, and the Action caches the results of this installation. Then, both Alvaro and I run bundle install again below, but this time the command only reads the cached results and doesn't actually install anything. As a result, the error about the missing gem occurs when running ruby run script.rb.

so, the solution is to set bundler-cache: false.