rails6 Bootsnap::CompileCache::PermissionErrorが発生した場合

rails6 Bootsnap::CompileCache::PermissionErrorが発生した場合

rails6でrailsコマンド実行時にエラー「Bootsnap::CompileCache::PermissionError」が発生した場合の対処法を記述してます。

環境

  • OS CentOS Linux release 7.9.2009 (Core)
  • Ruby 2.7.2
  • Rails 6.1.1
  • rbenv 1.1.2-40-g62d7798

エラー全文

「rails g controller」実行時に発生。

/home/username/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bootsnap-1.4.8/lib/bootsnap/compile_cache.rb:29:in `permission_error': bootsnap doesn't have permission to write cache entries in '/home/username/railsApp/tmp/cache/bootsnap-compile-cache' (or, less likely, doesn't have permission to read '/home/username/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bootsnap-1.4.8/lib/bootsnap/compile_cache/yaml.rb') (Bootsnap::CompileCache::PermissionError)
        from /home/username/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bootsnap-1.4.8/lib/bootsnap/compile_cache/iseq.rb:44:in `rescue in load_iseq'
        from /home/username/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bootsnap-1.4.8/lib/bootsnap/compile_cache/iseq.rb:34:in `load_iseq'
        from /home/username/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
        from /home/username/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
        from /home/username/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
        from /home/username/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
        from /home/username/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
        from /home/username/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/activesupport-6.1.1/lib/active_support/dependencies.rb:332:in `block in require'
        from /home/username/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/activesupport-6.1.1/lib/active_support/dependencies.rb:299:in `load_dependency'
        from /home/username/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/activesupport-6.1.1/lib/active_support/dependencies.rb:332:in `require'
        from /home/username/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bootsnap-1.4.8/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:53:in `require_relative'
        from /home/username/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bootsnap-1.4.8/lib/bootsnap/compile_cache.rb:19:in `setup'
        from /home/username/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bootsnap-1.4.8/lib/bootsnap.rb:32:in `setup'
        from /home/username/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/bootsnap-1.4.8/lib/bootsnap/setup.rb:31:in `<top (required)>'
        from /home/username/railsApp/config/boot.rb:4:in `require'
        from /home/username/railsApp/config/boot.rb:4:in `<top (required)>'
        from /home/username/railsApp/config/application.rb:1:in `require_relative'
        from /home/username/railsApp/config/application.rb:1:in `<top (required)>'
        from /home/username/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/spring-2.1.1/lib/spring/application.rb:92:in `require'
        from /home/username/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/spring-2.1.1/lib/spring/application.rb:92:in `preload'
        from /home/username/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/spring-2.1.1/lib/spring/application.rb:157:in `serve'
        from /home/username/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/spring-2.1.1/lib/spring/application.rb:145:in `block in run'
        from /home/username/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/spring-2.1.1/lib/spring/application.rb:139:in `loop'
        from /home/username/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/spring-2.1.1/lib/spring/application.rb:139:in `run'
        from /home/username/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/spring-2.1.1/lib/spring/application/boot.rb:19:in `<top (required)>'
        from /home/username/.rbenv/versions/2.7.2/lib/ruby/site_ruby/2.7.0/rubygems/core_ext/kernel_require.rb:85:in `require'
        from /home/username/.rbenv/versions/2.7.2/lib/ruby/site_ruby/2.7.0/rubygems/core_ext/kernel_require.rb:85:in `require'
        from -e:1:in `<main>'

原因

キャッシュクリア後に作成された「tmp/cache」配下の「bootsnap-compile-cache」の所有者がrootになっていたため

rails tmp:cache:clear

対処法

「/tmp/cache」ディレクトリの権限を、対象のユーザーに戻してあげる

sudo chown -R 対象ユーザー /tmp/cache