Skip to content

Fix Issue #1464: Rails 7.1 Zeitwerk autoloading race condition#18

Merged
takaokouji merged 1 commit intomasterfrom
fix/rails71-zeitwerk-resource-controller-1464
Jan 21, 2026
Merged

Fix Issue #1464: Rails 7.1 Zeitwerk autoloading race condition#18
takaokouji merged 1 commit intomasterfrom
fix/rails71-zeitwerk-resource-controller-1464

Conversation

@takaokouji
Copy link
Collaborator

Summary

Fixes JSONAPI-Resources#1464 by removing the ActiveSupport.on_load lazy loading mechanism that causes race conditions with Zeitwerk in Rails 7.1+.

Problem

In Rails 7.1 with Zeitwerk autoloading and eager_load disabled, tests would intermittently fail with:

NameError: uninitialized constant JSONAPI::ResourceController

The error was random (2-3 to 20-30 times per test run) and only occurred locally, not in CI where eager loading is enabled.

Root Cause

The gem used ActiveSupport.on_load(:action_controller_base) to delay loading of ResourceController:

if Rails::VERSION::MAJOR >= 6
  ActiveSupport.on_load(:action_controller_base) do
    require 'jsonapi/resource_controller'
  end
else
  require 'jsonapi/resource_controller'
end

This lazy loading mechanism creates a race condition with Zeitwerk's autoloading in Rails 7.1+.

Solution

Remove the conditional lazy loading and always require ResourceController directly:

require 'jsonapi/resource_controller'

This eliminates the race condition and ensures ResourceController is always available.

Testing

  • All 691 tests pass
  • No changes to test behavior
  • Compatible with Rails 6.1+

Workarounds No Longer Needed

  • config.eager_load = true in test environment
  • Manual require 'jsonapi/resource_controller' before use
  • Switching to ActsAsResourceController mixin

Related

🤖 Generated with Claude Code

… 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
@takaokouji takaokouji merged commit 4734642 into master Jan 21, 2026
60 checks passed
@takaokouji takaokouji deleted the fix/rails71-zeitwerk-resource-controller-1464 branch January 21, 2026 12:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NameError: uninitialized constant JSONAPI::ResourceController in Rails 7.1

1 participant