Fix FormHelper loading with Rails 7+ add_autoload_paths_to_load_path = false#3877
Fix FormHelper loading with Rails 7+ add_autoload_paths_to_load_path = false#3877
Conversation
🦋 Changeset detectedLatest commit: 14e31e6 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Replace require with require_relative to fix incompatibility with Rails 7+ add_autoload_paths_to_load_path = false config. The app/helpers directory is no longer in $LOAD_PATH with this config, so the previous require failed. Remove the begin/rescue block that was silently swallowing errors. Co-authored-by: jonrohan <54012+jonrohan@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes a critical bug where Rails 7+ applications using add_autoload_paths_to_load_path = false encounter a NameError: uninitialized constant Primer::FormHelper when loading the engine. The fix replaces require "primer/form_helper" with require_relative to make the file loading independent of $LOAD_PATH configuration, aligning with Rails 7+ design principles.
Changes:
- Replace
$LOAD_PATH-dependentrequirewithrequire_relativefor FormHelper loading - Remove the
begin/rescueblock that was masking the LoadError
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
lib/primer/view_components/engine.rb |
Changed FormHelper loading from require to require_relative to support Rails 7+ add_autoload_paths_to_load_path = false configuration |
.changeset/rich-glasses-tease.md |
Added changeset documenting the bug fix as a patch release |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
What are you trying to accomplish?
Rails 7+ applications using
add_autoload_paths_to_load_path = falseencounterNameError: uninitialized constant Primer::FormHelperwhen loading the engine.Root cause:
require "primer/form_helper"depends on$LOAD_PATHcontainingapp/helpers, which Rails 7+ no longer guarantees. Thebegin/rescueblock was silently swallowing theLoadError, causing the subsequenthelper Primer::FormHelperto fail.Changes:
require "primer/form_helper"withrequire_relative "../../../app/helpers/primer/form_helper"begin/rescueblock that masked the failureThis approach matches the existing pattern in
lib/rubocop/cop/primer/base_cop.rbfor loading app-level files.Screenshots
N/A - Internal engine initialization change
Integration
Risk Assessment
Single line change to file loading mechanism. Maintains backward compatibility with existing Rails versions while enabling Rails 7+ compatibility. No behavior changes to the FormHelper itself.
What approach did you choose and why?
require_relativeloads files relative to the current file location, independent of$LOAD_PATHconfiguration. This makes the code resilient to Rails load path changes across versions.Alternative considered: Manually manipulating
$LOAD_PATHin the initializer. Rejected as it's more fragile and conflicts with Rails 7+ design goals.Anything you want to highlight for special attention from reviewers?
The relative path
../../../app/helpers/primer/form_helperresolves correctly fromlib/primer/view_components/engine.rb. Verified path resolution and that the helper module loads successfully.Accessibility
N/A - No UI changes
Merge checklist
Original prompt
helper Primer::FormHelperis incompatible with Rails 7+ config #3814💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.