Load Error on C extension of Cached Ruby gem in Rails - ruby-on-rails

For a Rails app, I have a gitlab CI/CD set up with three stages. First stage is caching of gems and assets.
cache: &default-cache
untracked: true
key: "assets-compile:vendor_ruby:.yarn-cache:tmp_cache_assets_sprockets:v3"
paths:
- vendor/ruby/
- .yarn-cache/
- public/assets/
- node_modules/
policy: pull-push
In the next stage, I pull from the cached gems in vendor/ruby folder.
testing:
cache:
<<: *default-cache
policy: pull
stage: test
image: "ruby:3.0"
timeout: one hour
script:
- apt-get update -qq && apt-get install -y -qq nodejs
- export PATH="/usr/local/bundle/bin:$PATH"
- bundle config set --local path 'vendor/ruby'
- RAILS_ENV=test bundle install -j $(nproc)
This works well, but for a gem 'ox', throws a LoadError
Failure/Error: require 'ox'
LoadError:
cannot load such file -- ox/ox
# ./vendor/ruby/ruby/3.0.0/gems/zeitwerk-2.6.6/lib/zeitwerk/kernel.rb:38:in `require'
# ./vendor/ruby/ruby/3.0.0/gems/zeitwerk-2.6.6/lib/zeitwerk/kernel.rb:38:in `require'
# ./vendor/ruby/ruby/3.0.0/gems/ox-2.14.11/lib/ox.rb:79:in `<top (required)>'
My understanding is that it is unable to load ox.rb which would be created after the C extension is compiled, which is not happening during caching.
How do I solve this?
I tried removing require: false from the Gemfile, but didn't work.

Related

Rails Rspec errors on Github actions

I keep getting errors with RSpec on Github Actions. Not sure what's going on.
I'm on Rails 7 and Ruby 3.
Locally Rspec runs fine. I'm new to Github actions and have been fumbling around with it for a few hours now. It seems like an environment issue where Rails isn't loaded correctly but I'm not sure where the problem could be. Does anyone have a working Github actions example with Rails 7, Ruby 3 and RSpec?
Here's my github actions config:
# This workflow uses actions that are not certified by GitHub. They are
# provided by a third-party and are governed by separate terms of service,
# privacy policy, and support documentation.
#
# This workflow will install a prebuilt Ruby version, install dependencies, and
# run tests and linters.
name: "Ruby on Rails CI"
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:14-alpine
ports:
- "5432:5432"
env:
POSTGRES_DB: starter_rails_api_test
POSTGRES_USER: rails
POSTGRES_PASSWORD: password
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
env:
RAILS_ENV: test
DATABASE_URL: "postgres://rails:password#localhost:5432/starter_rails_api_test"
steps:
- name: Checkout code
uses: actions/checkout#v3
# Add or replace dependency steps here
- name: Install Ruby and gems
uses: ruby/setup-ruby#v1
with:
bundler: default
# bundler-cache: true
- name: Install gems
run: bin/bundle install
# Add or replace database setup steps here
- name: Create database tables
run: bin/rails db:create
- name: Set up database schema
run: bin/rails db:schema:load
# Add or replace test runners here
- name: Run tests
run: bin/bundle exec rspec spec
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout#v3
- name: Install Ruby and gems
uses: ruby/setup-ruby#v1
with:
bundler-cache: true
# Add or replace any other lints here
# - name: Security audit dependencies
# run: bin/bundler-audit --update
# - name: Security audit application code
# run: bin/brakeman -q -w2
- name: Lint Ruby files
run: bin/bundle exec rubocop --parallel
Output:
Run bin/bundle exec rspec spec
An error occurred while loading ./spec/models/widget_spec.rb.
Failure/Error: rescue_from ActiveModel::ValidationError, with: ->(e) { handle_validation_error(e) }
NameError:
uninitialized constant ActiveModel::ValidationError
rescue_from ActiveModel::ValidationError, with: ->(e) { handle_validation_error(e) }
^^^^^^^^^^^^^^^^^
Did you mean? ActiveModel::Validator
# ./app/controllers/application_controller.rb:21:in `<class:ApplicationController>'
# ./app/controllers/application_controller.rb:3:in `<top (required)>'
# ./config/environment.rb:7:in `<top (required)>'
# ./spec/rails_helper.rb:5:in `require'
# ./spec/rails_helper.rb:5:in `<top (required)>'
# ./spec/models/widget_spec.rb:3:in `require'
# ./spec/models/widget_spec.rb:3:in `<top (required)>'
An error occurred while loading ./spec/requests/api/v1/widgets_spec.rb.
Failure/Error: require File.expand_path('../config/environment', __dir__)
FrozenError:
can't modify frozen Array: ["/home/runner/work/starter-rails-api/starter-rails-api/app/controllers", "/home/runner/work/starter-rails-api/starter-rails-api/app/controllers/concerns", "/home/runner/work/starter-rails-api/starter-rails-api/app/lib", "/home/runner/work/starter-rails-api/starter-rails-api/app/models", "/home/runner/work/starter-rails-api/starter-rails-api/app/models/concerns", "/home/runner/work/starter-rails-api/starter-rails-api/app/serializers"]
# ./config/environment.rb:7:in `<top (required)>'
# ./spec/rails_helper.rb:5:in `<top (required)>'
# ./spec/requests/api/v1/widgets_spec.rb:3:in `<top (required)>'
An error occurred while loading ./spec/routing/api/v1/widgets_routing_spec.rb.
Failure/Error: require File.expand_path('../config/environment', __dir__)
FrozenError:
can't modify frozen Array: ["/home/runner/work/starter-rails-api/starter-rails-api/app/controllers", "/home/runner/work/starter-rails-api/starter-rails-api/app/controllers/concerns", "/home/runner/work/starter-rails-api/starter-rails-api/app/lib", "/home/runner/work/starter-rails-api/starter-rails-api/app/models", "/home/runner/work/starter-rails-api/starter-rails-api/app/models/concerns", "/home/runner/work/starter-rails-api/starter-rails-api/app/serializers"]
# ./config/environment.rb:7:in `<top (required)>'
# ./spec/rails_helper.rb:5:in `<top (required)>'
# ./spec/routing/api/v1/widgets_routing_spec.rb:3:in `<top (required)>'
Finished in 0.00006 seconds (files took 1.3 seconds to load)
0 examples, 0 failures, 3 errors occurred outside of examples
Error: Process completed with exit code 1.
Fiddled around a lot with the github actions config. No luck. Always getting errors.
Turned out to be a Rails issue not Github actions. Weird that it only happens on linux and not on osx.
Fixed it by adding require 'active_model/validations' to my application_controller.rb file. Not sure why it's not already required automatically.
If anyone can shed some light on this I'd appreciate it.
For reference this is where the class is defined:
https://github.com/rails/rails/blob/8015c2c2cf5c8718449677570f372ceb01318a32/activemodel/lib/active_model/validations.rb#L425

Travis CI Ruby exited with 1

I built a new rails app and when I have configured the .travis.yml file it exits with one all the time.
Here is my travis configuration:
language: ruby
node_js:
- 12
cache:
bundler: true
yarn: true
services:
- redis-server
sudo: required
before_install:
- sudo apt-get update
- sudo apt-get install google-chrome-stable
addons:
postgresql: '9.6'
before_script:
- psql -c 'create database product_hunt_clone_test;' -U postgres
script:
- bundle install
- bundle exec rake db:schema:load
- bundle exec rake db:test:prepare
- SECRET_KEY_BASE=a RAILS_ENV=production bundle exec rake assets:precompile
- bundle exec rake test
- bundle exec rake test:system
and here are the errors travis is giving me:
screenshot from travis console
You have apparently create the initial application using the --skip-sprockets parameter. As such, you have disable the use of the assets pipeline in your application and with that have disable the need (and possibility) to run rake assets:precompile.
If you do need assets pipeline (and thus the rails-sprockets gem), you can re-enable it by adding
gem "sprockets-rails"
to your Gemfile and uncommenting the line
# require "sprockets/railtie"
in your config/application.rb file. You would also need to create the asset files as necessary. Please refer to the documentation for the assets pipeline for details.
If you actually want to skip the assets pipeline, you can just remove the call to the assets:precompile task from your .travis.yml.
Are you correctly installing your gems (with bundle install) before executing your rake commands? Seems like you would need sprockets before compiling the assets.

gitlab-ci can't find config/database.yml, although it's not supposed to exist in my repo

I'm trying to create gitlab-ci file for my Rails project.
Here's a part of my .gitlab-ci.yml:
before_script:
- apt-get update -qq && apt-get install -yqq nodejs libmysqlclient-dev
- ruby -v
- which ruby
- gem install bundler --no-ri --no-rdoc
- bundle install --jobs $(nproc) "${FLAGS[#]}"
- bundle exec rake db:create db:schema:load
- bundle exec rake db:migrate --quiet
And the error is:
Could not load database configuration. No such file - ["config/database.yml"]
On my local machine and server the files config/database.yml are present.
"config/database.yml" doesn't exist in the repo and isn't supposed too. It's added in gitignore
Only the default config/database.example.yml exists in my repo. And it doesn't contain the real credentials of my db.
How can I fix it?
You have 2 alternatives:
Put on that script to copy the .example.yml to database.yml and use the environment variables to set the config:
e.g:
production:
username: <%= ENV['DB_USERNAME'] %>
host: <%= ENV['DB_HOST'] %>
etc...
create a script to generate this file for you with data hardcoded or from the environment and call on gitlab-ci.yml.

Wercker CI config for rails 5.1 + reactjs with webpacker gem

I struggle setting up Wercker on a rails / Webpacker project.
I first had to add steps to install node, then another to pre-compile react packs, then to install yarn. First it was good but then I added dependencies to the project and know I'm stuck on 3 dependencies with the same error message :
ERROR in ./app/javascript/app/index.js
Module not found: Error: Can't resolve 'redux-thunk' in '/pipeline/source/app/javascript/app'
# ./app/javascript/app/index.js 5:0-32
# ./app/javascript/packs/app.js
That's happening only with redux-thunk, react-redux-i18n and react-spinkit
I've tried to add a step yarn install which passes through the process but doesn't solve the problem.
Here is my wercker.yml:
# wercker.yml
box: ruby:2.4.1
services:
- redis
- postgres
build:
steps:
- louischavane/install-phantomjs#0.0.5
- rails-database-yml
- script:
name: nokogiri tricks
code: bundle config build.nokogiri --use-system-libraries
- bundle-install
- bigtruedata/install-node#2.1.1
- akupila/yarn#0.2.0
- script:
name: yarn-install
code: yarn install
- script:
name: pre-compile react packs
code: NODE_ENV=test bundle exec rails webpacker:compile
- script:
name: run migration
code: rake db:migrate RAILS_ENV=test
- script:
name: load fixture
code: rake db:fixtures:load RAILS_ENV=test
- script:
name: run rubocop
code: bundle exec rubocop
- script:
name: test
code: bundle exec rake test RAILS_ENV=test
Found a way around adding a step to remove node modules before pre-compiling :
- script:
name: delete node modules
code: rm -rf node_modules
UPDATE
I now have a clean Wercker setup handling tests, system tests with chrome headless, rubocop, eslint and stylelint. You can find it here: https://gist.github.com/adesurirey/7c013bfa1a9bed9d56ffea54db5d6288

Dependencies cache in Wercker

I'm using Wercker on my Ruby on Rails project. My wercker.yml looks like this:
box: ruby
services:
- postgres
build:
steps:
- script:
name: Nokogiri fix
code: bundle config build.nokogiri --use-system-libraries
- bundle-install
- rails-database-yml
- script:
name: Set up db
code: bundle exec rake db:schema:load RAILS_ENV=test
- script:
name: rspec
code: bundle exec rspec
after-steps:
- slack-notifier:
url: $SLACK_URL
channel: notifications
username: werckerbot
The problem is that wercker every time download gems with bundle install instead of get them from cache. With that my build takes almost 2 minutes instead of a few seconds... How can I fix that?

Resources