Skip to content

Commit 03a4737

Browse files
committed
Fix Issue JSONAPI-Resources#1464: Rails 7.1 Zeitwerk autoloading race condition
Issue JSONAPI-Resources#1464 reported intermittent 'NameError: uninitialized constant JSONAPI::ResourceController' errors in Rails 7.1 when eager_load is disabled. Root cause: - ActiveSupport.on_load(:action_controller_base) lazy loading mechanism - Causes race condition with Zeitwerk autoloading in Rails 7.1+ - Errors occur randomly during test execution - Only happens locally, not in CI with eager loading enabled Solution: - Remove conditional lazy loading with ActiveSupport.on_load - Always require 'jsonapi/resource_controller' directly - Eliminates race condition with Zeitwerk The fix ensures ResourceController is always available without depending on ActionController::Base load timing. Workarounds no longer needed: - config.eager_load = true in test environment - Manual require 'jsonapi/resource_controller' before use - Switching to ActsAsResourceController mixin Related: https://github.com/cerebris/jsonapi-resources/issues/1464
1 parent e057af8 commit 03a4737

1 file changed

Lines changed: 4 additions & 7 deletions

File tree

lib/jsonapi-resources.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@
99
require 'jsonapi/cached_response_fragment'
1010
require 'jsonapi/response_document'
1111
require 'jsonapi/acts_as_resource_controller'
12-
if Rails::VERSION::MAJOR >= 6
13-
ActiveSupport.on_load(:action_controller_base) do
14-
require 'jsonapi/resource_controller'
15-
end
16-
else
17-
require 'jsonapi/resource_controller'
18-
end
12+
# Load resource_controller directly to avoid Zeitwerk autoloading issues in Rails 7.1+
13+
# Previously used ActiveSupport.on_load which caused race conditions with Zeitwerk
14+
# See: https://github.com/cerebris/jsonapi-resources/issues/1464
15+
require 'jsonapi/resource_controller'
1916
require 'jsonapi/resource_controller_metal'
2017
require 'jsonapi/resources/version'
2118
require 'jsonapi/configuration'

0 commit comments

Comments
 (0)