Skip to content

Map mixins that ActiveSupport.on_load hooks add to Rails classes - #147

Closed
apiology wants to merge 3 commits into
gem-without-yardfrom
map-active-support-on-load-mixins
Closed

apiology wants to merge 3 commits into
gem-without-yardfrom
map-active-support-on-load-mixins

Conversation

@apiology

@apiology apiology commented Oct 9, 2026 •

Copy link
Copy Markdown
Owner

This PR was written by Claude Code on behalf of @apiology.

Builds on castwide#1378.

Problem: Methods that gems mix into Rails classes through load hooks are missing:

# responders
ActiveSupport.on_load(:action_controller) { include ActionController::RespondWith }
# globalid
ActiveSupport.on_load(:active_record) { send :include, GlobalID::Identification }

ActionController::Base.new.respond_with  # no pin found
ActiveRecord::Base.new.to_gid            # no pin found

Rails apps lose responders, globalid, orm_adapter and turbo-rails' native navigation helpers, and an app's own on_load initializers have no effect.

Solution: Map include, extend and prepend inside an ActiveSupport.on_load(:hook) block (bare, self., or send :include) onto the classes that conventions report for that hook through a new Convention::Base#load_hook_targets; Convention.load_hook_targets unions what every registered convention returns.

Only mixins are mapped: methods defined or macros called inside the block are not. Hooks no convention maps are ignored, so with no plugin loaded nothing is mapped. Module names resolve as written, not against the block's lexical scope.

Where this belongs: ActiveSupport.on_load is ActiveSupport's API, but only Rails integrations call it. In a random sample of 357 rubygems.org gems depending on activesupport, none of the 190 that are not Rails-only in practice call it, while 14% of them (26 of 190; 95% CI 9.5–19%) use class_attribute, mattr_*/cattr_* or config_accessor. So core keeps the generic block mechanism, and the hook-to-class table, which depends on Rails libraries, comes from plugins: solargraph-rails supplies it via load_hook_targets.

In the language server, gem pins are cached in a solargraph cache subprocess, so mixins from gems map there only with #149, which loads the workspace's plugins when caching gems.

Test plan: measured before the table moved to plugins, with it in core; needs re-measuring with solargraph-rails supplying it. solargraph-rails 061a2009 definitions (Rails 8.0), against gem-without-yard: of the 23 entries skipped for this reason, 9 now match (responders 7/9, orm_adapter 1/1, globalid 1/6) and 7 more resolve with real types where the definitions say undefined (globalid 5, responders #responder?/#mimes_for_respond_to? as Boolean). The 7 turbo-rails entries need #144, since Turbo::Native::Navigation lives under app/. No regressions across all 3133 entries.

apiology and others added 3 commits October 9, 2026 12:14
Gems add methods to Rails classes lazily, e.g. responders runs
`ActiveSupport.on_load(:action_controller) { include
ActionController::RespondWith }`. The include ran inside a block, so
its reference attached to the enclosing namespace (or nothing) and
ActionController::Base never gained the module's methods.

Map include, extend and prepend inside an on_load block (bare, with
self as receiver, or through `send :include`) onto the classes that
run the hook. The hook-to-class table comes from the run_load_hooks
call sites in Rails 7.0 through 8.1; :action_controller runs on both
ActionController::Base and ActionController::API. Hooks whose payload
is not a class (:i18n, :after_initialize, ...) are left alone.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EhUSLUimEYJyCZv8NdQGKb
The generic<X> left by Hash#fetch is the rbs >= 4.1.0 regression
tracked in castwide#1227; fetch without a default leaks it
the same way.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EhUSLUimEYJyCZv8NdQGKb
The hook-to-class table depends on which Rails libraries define each
hook, so it belongs with the Rails integration rather than in core.
Conventions now supply it through Convention::Base#load_hook_targets,
and Convention.load_hook_targets unions what every registered
convention returns. With no convention supplying targets, mixins in
on_load blocks map nothing.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EhUSLUimEYJyCZv8NdQGKb
@apiology

apiology commented Oct 9, 2026

Copy link
Copy Markdown
Owner Author

Claude: Replaced by iftheshoefritz/solargraph-rails#226, which maps on_load mixins in solargraph-rails and measures the same 15 of 23.

@apiology apiology closed this Oct 9, 2026
apiology added a commit to iftheshoefritz/solargraph-rails that referenced this pull request Oct 9, 2026
Rails integrations add modules to Rails classes inside
ActiveSupport.on_load(:hook) blocks, e.g. responders including
ActionController::RespondWith into ActionController::Base. Solargraph
attached those mixins to the enclosing namespace, so methods such as
respond_with were missing from controllers.

Register a :send node processor that maps include, extend and prepend
inside such a block onto the classes the hook runs on, taken from the
run_load_hooks call sites in Rails 7.0 through 8.1. Calls on self, with
no receiver, through send(:include, ...) or on the block parameter
(on_load(:x) { |base| base.include Foo }) all count.

This replaces doing the same in Solargraph core
(apiology/solargraph#147): no non-Rails activesupport gem calls
on_load.

Solargraph before 0.56.2 keeps one processor per node type, so
registering there would replace its own; the processor is only loaded
and registered where NodeProcessor supports chaining.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EhUSLUimEYJyCZv8NdQGKb
apiology added a commit to iftheshoefritz/solargraph-rails that referenced this pull request Oct 13, 2026
* Map mixins added in ActiveSupport.on_load blocks

Rails integrations add modules to Rails classes inside
ActiveSupport.on_load(:hook) blocks, e.g. responders including
ActionController::RespondWith into ActionController::Base. Solargraph
attached those mixins to the enclosing namespace, so methods such as
respond_with were missing from controllers.

Register a :send node processor that maps include, extend and prepend
inside such a block onto the classes the hook runs on, taken from the
run_load_hooks call sites in Rails 7.0 through 8.1. Calls on self, with
no receiver, through send(:include, ...) or on the block parameter
(on_load(:x) { |base| base.include Foo }) all count.

This replaces doing the same in Solargraph core
(apiology/solargraph#147): no non-Rails activesupport gem calls
on_load.

Solargraph before 0.56.2 keeps one processor per node type, so
registering there would replace its own; the processor is only loaded
and registered where NodeProcessor supports chaining.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EhUSLUimEYJyCZv8NdQGKb

* Cover the on_load processor's guard branches

Add specs for the cases each guard rejects: non-constant mixin
arguments, send without a method name or with a string one, on_load
without an ActiveSupport receiver or a symbol hook, and Solargraph
versions where processors cannot be chained. Registration moves into
LoadHooks.register so that last case can be exercised.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EhUSLUimEYJyCZv8NdQGKb

* Register the load hook processor from the entrypoint

Feature files under lib/solargraph/rails/ have no load-time side
effects; registration happens at the bottom of solargraph-rails.rb,
next to Convention.register.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EhUSLUimEYJyCZv8NdQGKb

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
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.

1 participant