stepful/cyperful

Rails not initialized at Bundle require-time

searls opened this issue · 1 comments

When cyperful is required by my Gemfile (e.g. I don't have require: false set), it tries to hook into Rails before Rails itself hasn't initialized yet. Looking briefly at the code, it seems like this middleware may need to be added in a railtie initializer

$ CYPERFUL=1 bin/rails test test/system/program_admin_test.rb:60
/Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/cyperful-0.1.9/lib/cyperful/framework_injections.rb:104:in `<main>': undefined method `config' for nil (NoMethodError)

  Rails.application.config.content_security_policy do |policy|
                   ^^^^^^^
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/3.3.0/bundled_gems.rb:74:in `require'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/3.3.0/bundled_gems.rb:74:in `block (2 levels) in replace_require'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/zeitwerk-2.6.13/lib/zeitwerk/kernel.rb:34:in `require'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/cyperful-0.1.9/lib/cyperful.rb:68:in `<main>'
        from <internal:/Users/justin/.rbenv/versions/3.3.0/lib/ruby/site_ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
        from <internal:/Users/justin/.rbenv/versions/3.3.0/lib/ruby/site_ruby/3.3.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/3.3.0/bundled_gems.rb:74:in `block (2 levels) in replace_require'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/zeitwerk-2.6.13/lib/zeitwerk/kernel.rb:34:in `require'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/bundler-2.5.4/lib/bundler/runtime.rb:60:in `block (2 levels) in require'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/bundler-2.5.4/lib/bundler/runtime.rb:55:in `each'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/bundler-2.5.4/lib/bundler/runtime.rb:55:in `block in require'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/bundler-2.5.4/lib/bundler/runtime.rb:44:in `each'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/bundler-2.5.4/lib/bundler/runtime.rb:44:in `require'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/bundler-2.5.4/lib/bundler.rb:187:in `require'
        from /Users/justin/code/searls/grog/config/application.rb:19:in `<main>'
        from /Users/justin/code/searls/grog/config/environment.rb:2:in `require_relative'
        from /Users/justin/code/searls/grog/config/environment.rb:2:in `<main>'
        from /Users/justin/code/searls/grog/test/test_helper.rb:2:in `require_relative'
        from /Users/justin/code/searls/grog/test/test_helper.rb:2:in `<main>'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/3.3.0/bundled_gems.rb:74:in `require'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/3.3.0/bundled_gems.rb:74:in `block (2 levels) in replace_require'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
        from /Users/justin/code/searls/grog/test/application_system_test_case.rb:1:in `<main>'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/3.3.0/bundled_gems.rb:74:in `require'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/3.3.0/bundled_gems.rb:74:in `block (2 levels) in replace_require'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
        from /Users/justin/code/searls/grog/test/system/program_admin_test.rb:1:in `<main>'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/3.3.0/bundled_gems.rb:74:in `require'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/3.3.0/bundled_gems.rb:74:in `block (2 levels) in replace_require'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/bundler/gems/rails-02f6c2913b75/railties/lib/rails/test_unit/runner.rb:61:in `block in load_tests'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/bundler/gems/rails-02f6c2913b75/railties/lib/rails/test_unit/runner.rb:60:in `each'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/bundler/gems/rails-02f6c2913b75/railties/lib/rails/test_unit/runner.rb:60:in `load_tests'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/bundler/gems/rails-02f6c2913b75/railties/lib/rails/test_unit/runner.rb:52:in `run'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/bundler/gems/rails-02f6c2913b75/railties/lib/rails/commands/test/test_command.rb:33:in `perform'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/thor-1.3.1/lib/thor/command.rb:28:in `run'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/thor-1.3.1/lib/thor/invocation.rb:127:in `invoke_command'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/bundler/gems/rails-02f6c2913b75/railties/lib/rails/command/base.rb:178:in `invoke_command'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/thor-1.3.1/lib/thor.rb:527:in `dispatch'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/bundler/gems/rails-02f6c2913b75/railties/lib/rails/command/base.rb:73:in `perform'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/bundler/gems/rails-02f6c2913b75/railties/lib/rails/command.rb:71:in `block in invoke'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/bundler/gems/rails-02f6c2913b75/railties/lib/rails/command.rb:149:in `with_argv'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/bundler/gems/rails-02f6c2913b75/railties/lib/rails/command.rb:69:in `invoke'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/bundler/gems/rails-02f6c2913b75/railties/lib/rails/commands.rb:18:in `<main>'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/3.3.0/bundled_gems.rb:74:in `require'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/3.3.0/bundled_gems.rb:74:in `block (2 levels) in replace_require'
        from /Users/justin/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
        from bin/rails:4:in `<main>'

In the meantime, it'd probably be best to suggest users add Cyperful to their Gemfile with require: false:

group :test do
  # …
  gem "cyperful", require: false
end

Thanks @searls, I'm pretty ignorant in best practices like these, but I'll look into fixing this with railties.