palkan / anyway_config

Configuration library for Ruby gems and applications
MIT License
778 stars 52 forks source link

Rails 5.1 error: uninitialized constant Rails::Autoloaders (NameError) #127

Closed leemour closed 1 year ago

leemour commented 1 year ago

What did you do?

Start application rails s or rails c

What did you expect to happen?

Application loaded without errors

What actually happened?

Error occured

 ✘ viacheslavptsarev ⮀ ~/Work/storefront ⮀  bundle exec rails s
/Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/json-1.8.6/lib/json/common.rb:155: warning: Using the last argument as keyword parameters is deprecated
/Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/json-1.8.6/lib/json/common.rb:155: warning: Using the last argument as keyword parameters is deprecated
/Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/json-1.8.6/lib/json/common.rb:155: warning: Using the last argument as keyword parameters is deprecated
/Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/json-1.8.6/lib/json/common.rb:155: warning: Using the last argument as keyword parameters is deprecated
/Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/opentelemetry-exporter-jaeger-0.17.0/thrift/gen-rb/jaeger_types.rb:82: warning: already initialized constant OpenTelemetry::Exporter::Jaeger::Thrift::Log::FIELDS
/Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/opentelemetry-exporter-jaeger-0.17.0/thrift/gen-rb/jaeger_types.rb:80: warning: previous definition of FIELDS was here
/Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/anyway_config-2.3.1/lib/anyway/type_casting.rb:111: warning: Pattern matching is experimental, and the behavior may change in future versions of Ruby!
/Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/anyway_config-2.3.1/lib/anyway/rbs.rb:40: warning: Pattern matching is experimental, and the behavior may change in future versions of Ruby!
Traceback (most recent call last):
    24: from bin/rails:5:in `<main>'
    23: from bin/rails:5:in `require'
    22: from /Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/railties-5.1.7/lib/rails/commands.rb:16:in `<top (required)>'
    21: from /Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/railties-5.1.7/lib/rails/command.rb:44:in `invoke'
    20: from /Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/railties-5.1.7/lib/rails/command/base.rb:63:in `perform'
    19: from /Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/thor-1.2.1/lib/thor.rb:392:in `dispatch'
    18: from /Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/thor-1.2.1/lib/thor/invocation.rb:127:in `invoke_command'
    17: from /Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/thor-1.2.1/lib/thor/command.rb:27:in `run'
    16: from /Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/railties-5.1.7/lib/rails/commands/server/server_command.rb:130:in `perform'
    15: from /Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/railties-5.1.7/lib/rails/commands/server/server_command.rb:130:in `tap'
    14: from /Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/railties-5.1.7/lib/rails/commands/server/server_command.rb:133:in `block in perform'
    13: from /Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/railties-5.1.7/lib/rails/commands/server/server_command.rb:133:in `require'
    12: from /Users/viacheslavptsarev/Work/storefront/config/application.rb:14:in `<top (required)>'
    11: from /Users/viacheslavptsarev/Work/storefront/config/application.rb:15:in `<module:Instamart>'
    10: from /Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/railties-5.1.7/lib/rails/application.rb:91:in `inherited'
     9: from /Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/activesupport-5.1.7/lib/active_support/lazy_load_hooks.rb:49:in `run_load_hooks'
     8: from /Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/activesupport-5.1.7/lib/active_support/lazy_load_hooks.rb:49:in `each'
     7: from /Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/activesupport-5.1.7/lib/active_support/lazy_load_hooks.rb:50:in `block in run_load_hooks'
     6: from /Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/activesupport-5.1.7/lib/active_support/lazy_load_hooks.rb:65:in `execute_hook'
     5: from /Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/activesupport-5.1.7/lib/active_support/lazy_load_hooks.rb:60:in `with_execution_control'
     4: from /Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/activesupport-5.1.7/lib/active_support/lazy_load_hooks.rb:67:in `block in execute_hook'
     3: from /Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/anyway_config-2.3.1/lib/anyway/railtie.rb:12:in `block in <class:Railtie>'
     2: from /Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/anyway_config-2.3.1/lib/anyway/rails/settings.rb:29:in `autoload_static_config_path='
     1: from /Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/anyway_config-2.3.1/lib/anyway/rails/settings.rb:29:in `tap'
/Users/viacheslavptsarev/.rbenv/versions/2.7.7/lib/ruby/gems/2.7.0/gems/anyway_config-2.3.1/lib/anyway/rails/settings.rb:31:in `block in autoload_static_config_path=': uninitialized constant Rails::Autoloaders (NameError)

Additional context

Created a config .rb, .yml files. Seems like Rails::Autoloaders doesn't exist in railties 5.1.7

Environment

Ruby Version: 2.7.7

Framework Version (Rails, whatever): 5.1.7

Anyway Config Version: Tried version 2.3.1, 2.4.0

bibendi commented 1 year ago

@palkan hey!

I think the problem is, Zeitwerk gem can be plugged in the application like a third party dependency, and at the same time not used by the Rails app as main loader. It looks like we need to add zeitwerk gem into this file https://github.com/palkan/anyway_config/blob/master/gemfiles/rails5.gemfile to represent the bug.

Maybe we should check the Rails version too?https://github.com/palkan/anyway_config/blob/8f4e3f5fb8e0320a7c9300bbb402005781b89292/lib/anyway/rails/settings.rb#L15

palkan commented 1 year ago

@bibendi Thanks!

Yeah, adding Zeitwerk to a Gemfile reproduces the problem: https://github.com/palkan/anyway_config/actions/runs/4858358580/jobs/8659695489

Will take a closer look soon.

palkan commented 1 year ago

Pushed a fix to master.

We should be able to use Zeitwerk even if Rails app is not using it; we just should check for Rails inflectors.

Also, added an internal option Anyway::Settings.autoload_via_zeitwerk = true | false that can be used to manually control autoloaders behavior.

Please, let me know if the fix doesn't help, I'll re-open the issue.

x2es commented 1 year ago

Thanx for fix!

I am linking it here:

When would you build next gem release (2.4.1)?

palkan commented 1 year ago

2.4.1 is out!