capistrano / rails

Official Ruby on Rails specific tasks for Capistrano
http://www.capistranorb.com/
MIT License
867 stars 270 forks source link

The deploy doesn't create node_modules and raises Sprockets::FileNotFound (after upgrading to rails 7) #259

Open collimarco opened 1 year ago

collimarco commented 1 year ago

Steps to reproduce

After upgrading an application from Rails 6.1 to Rails 7.0 the Capistrano deployment fails. The deployment has always worked (for years) and the application still works fine in development / test environments.

This is the error when running cap production deploy:

cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as user@server1.myapp.com: rake exit status: 1
rake stdout: Nothing written
rake stderr: rake aborted!
Sprockets::FileNotFound: couldn't find file 'emojionearea/dist/emojionearea' with type 'text/css'
Checked in these paths: 
  /home/myapp/www/myapp/releases/20220928123230/app/assets/config
  /home/myapp/www/myapp/releases/20220928123230/app/assets/images
  /home/myapp/www/myapp/releases/20220928123230/app/assets/javascripts
  /home/myapp/www/myapp/releases/20220928123230/app/assets/stylesheets
  /home/myapp/www/myapp/releases/20220928123230/vendor/assets/javascripts
  /home/myapp/www/myapp/releases/20220928123230/vendor/assets/stylesheets
  /home/myapp/www/myapp/shared/bundle/ruby/3.0.0/gems/jquery-rails-4.5.0/vendor/assets/javascripts
  /home/myapp/www/myapp/shared/bundle/ruby/3.0.0/gems/actiontext-7.0.4/app/assets/javascripts
  /home/myapp/www/myapp/shared/bundle/ruby/3.0.0/gems/actiontext-7.0.4/app/assets/stylesheets
  /home/myapp/www/myapp/shared/bundle/ruby/3.0.0/gems/actioncable-7.0.4/app/assets/javascripts
  /home/myapp/www/myapp/shared/bundle/ruby/3.0.0/gems/activestorage-7.0.4/app/assets/javascripts
  /home/myapp/www/myapp/shared/bundle/ruby/3.0.0/gems/actionview-7.0.4/lib/assets/compiled
  /home/myapp/www/myapp/releases/20220928123230/node_modules
/home/myapp/www/myapp/releases/20220928123230/app/assets/stylesheets/application.scss:13
/home/myapp/www/myapp/shared/bundle/ruby/3.0.0/gems/sprockets-4.1.1/lib/sprockets/resolve.rb:62:in `resolve!'
...

After some investigation on the server I see that:

This probably means that the problem is not Sprockets (which is looking in the right path) but the fact that node_modules is not generated during the deploy. So, why the command yarn install is not executed during the deploy? Everything used to work before the upgrade to Rails 7.

Expected behavior

The task deploy:assets:precompile should first download the Yarn modules to the node_modules folder on the server, then run rake assets:precompile.

Actual behavior

When you deploy, assets:precompile raises an exception Sprockets::FileNotFound.

System configuration

Rails 7.0.4 capistrano 3.17.1 capistrano-rails 1.6.2

collimarco commented 1 year ago

I found a solution! I share it here in case anyone has the same issue.

Here's the code that I use:

namespace :yarn do
  task :install do
    on release_roles(fetch(:assets_roles)) do
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute :rake, "yarn:install"
        end
      end
    end
  end
end
after 'bundler:install', 'yarn:install'

Probably this task should be added directly to this gem.

The issue was introduced by this change in the Rails repository: https://github.com/rails/rails/pull/43641

haseebeqx commented 1 year ago

The code in https://github.com/rails/webpacker/blob/master/docs/deployment.md worked for me.

before "deploy:assets:precompile", "deploy:yarn_install"
namespace :deploy do
  desc "Run rake yarn install"
  task :yarn_install do
    on roles(:web) do
      within release_path do
        execute("cd #{release_path} && yarn install --silent --no-progress --no-audit --no-optional")
      end
    end
  end
end
giedriusr commented 1 year ago

Is this a temporary solution?

caosborne89 commented 2 weeks ago

Also ran into this issue. Solved it using this solution.