fix(rails): load create_agent.rb explicitly instead of autoloading the whole lib/#314
Open
bexchauveto wants to merge 1 commit into
Open
fix(rails): load create_agent.rb explicitly instead of autoloading the whole lib/#314bexchauveto wants to merge 1 commit into
bexchauveto wants to merge 1 commit into
Conversation
…e whole lib/ The engine added the host app's entire `lib/` directory to Zeitwerk autoload_paths, only to resolve `ForestAdminRails::CreateAgent`. This forced Zeitwerk on the rest of the host's `lib/`, breaking apps that manage their own loading (explicit requires/includes). Drop the `add_autoload_paths` initializer and instead `require` the host's `lib/forest_admin_rails/create_agent.rb` explicitly during configuration, right after the existing-file check. The host's `lib/` is left untouched. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Member
Author
✅ Manual verification on a real Rails app (forest-poc /
|
Scenario (app does not opt into autoload_lib) |
lib/ in autoload_paths |
ForestAdminRails::CreateAgent loads |
|---|---|---|
main (old code) |
true → bug reproduced |
— |
| this branch | false → fixed |
true |
Conclusions:
- Bug reproduced on
main: Forest forces the host'slib/intoautoload_pathseven when the app never asked for it — exactly the customer's report. - Fix confirmed: the gem no longer touches the host's
autoload_paths. - No regression:
ForestAdminRails::CreateAgentstill loads correctly via the explicitrequire. - The nominal case is unaffected: when the app does opt into
autoload_lib, that keeps working — Forest just no longer makes that choice on the app's behalf.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A customer migrating to
forest_admin_railsreported that the gem breaks their boot: their entirelib/directory ends up managed by Zeitwerk autoloading, conflicting with their explicit loading strategy (explicitrequire/include).Root cause: the engine added the host app's whole
lib/directory to Zeitwerkautoload_paths, via this initializer:The only reason this existed was to resolve the constant
ForestAdminRails::CreateAgentfrom the host'slib/forest_admin_rails/create_agent.rb. But since Rails 6,lib/is intentionally kept out ofautoload_pathsprecisely because it often contains files that aren't Zeitwerk-compatible or are loaded explicitly. Forcing the host's entirelib/into autoload is intrusive and is what breaks these apps.Fix
add_autoload_pathsinitializer.load_configuration— right after the existingcreate_agent_file_exists?guard:Since
lib/is no longer in autoload_paths, a plainrequireis safe (no Zeitwerk conflict), and the host'slib/is left entirely under the app's own control. The path is centralized in acreate_agent_pathhelper reused bycreate_agent_file_exists?.Tests
Added to
engine_spec.rb:forest_admin_rails.add_autoload_pathsinitializer.create_agent_pathpoints to the expected location underRails.root.create_agent_file_exists?(present / absent).load_configuration: skips when no web server / file absent;requires the host file explicitly then runs setup when both conditions hold.All
engine_spec.rbexamples pass (28, 0 failures) and RuboCop is clean.