Skip to content

Fix FormHelper loading with Rails 7+ add_autoload_paths_to_load_path = false#3877

Merged
jonrohan merged 3 commits intomainfrom
copilot/fix-helper-primer-form-helper
Jan 30, 2026
Merged

Fix FormHelper loading with Rails 7+ add_autoload_paths_to_load_path = false#3877
jonrohan merged 3 commits intomainfrom
copilot/fix-helper-primer-form-helper

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 23, 2026

What are you trying to accomplish?

Rails 7+ applications using add_autoload_paths_to_load_path = false encounter NameError: uninitialized constant Primer::FormHelper when loading the engine.

Root cause: require "primer/form_helper" depends on $LOAD_PATH containing app/helpers, which Rails 7+ no longer guarantees. The begin/rescue block was silently swallowing the LoadError, causing the subsequent helper Primer::FormHelper to fail.

Changes:

  • Replace require "primer/form_helper" with require_relative "../../../app/helpers/primer/form_helper"
  • Remove begin/rescue block that masked the failure

This approach matches the existing pattern in lib/rubocop/cop/primer/base_cop.rb for loading app-level files.

Screenshots

N/A - Internal engine initialization change

Integration

Risk Assessment

  • Low risk the change is small, highly observable, and easily rolled back.

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_relative loads files relative to the current file location, independent of $LOAD_PATH configuration. This makes the code resilient to Rails load path changes across versions.

Alternative considered: Manually manipulating $LOAD_PATH in 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_helper resolves correctly from lib/primer/view_components/engine.rb. Verified path resolution and that the helper module loads successfully.

Accessibility

N/A - No UI changes

Merge checklist

  • Added/updated tests
  • Added/updated documentation
  • Added/updated previews (Lookbook)
  • Tested in Chrome
  • Tested in Firefox
  • Tested in Safari
  • Tested in Edge
Original prompt

This section details on the original issue you should resolve

<issue_title>helper Primer::FormHelper is incompatible with Rails 7+ config</issue_title>
<issue_description>We're trying to move to the Rails 7+ default config where add_autoload_paths_to_load_path = false. However when we do that we get a NameError

NameError: uninitialized constant Primer::FormHelper (NameError)

           helper Primer::FormHelper
                        ^^^^^^^^^^^^
/app/bundle/ruby/3.4.0/gems/primer_view_components-0.48.0/lib/primer/view_components/engine.rb:56:in 'block (2 levels) in <class:Engine>'

which seems to be coming from here
https://github.com/primer/view_components/blob/main/lib/primer/view_components/engine.rb#L49-L61

removing the begin/rescue block and adding require_relative "../../../app/helpers/primer/form_helper" to the top of the file seems to fix it. but it's still confusing considering app/helpers is on the eager load config here https://github.com/primer/view_components/blob/main/lib/primer/view_components/engine.rb#L19</issue_description>

Comments on the Issue (you are @copilot in this section)

@siddharthkp Hi @mariaaxime, thanks for taking the time to create the issue.

Things are a bit slow at work right now because of the holidays, I'll try to get you a response next week once folks are back at work :)</comment_new>


💡 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.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Jan 23, 2026

🦋 Changeset detected

Latest commit: 14e31e6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@primer/view-components Patch

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>
Copilot AI changed the title [WIP] Fix Primer::FormHelper compatibility with Rails 7+ config Fix FormHelper loading with Rails 7+ add_autoload_paths_to_load_path = false Jan 23, 2026
Copilot AI requested a review from jonrohan January 23, 2026 18:29
@jonrohan jonrohan marked this pull request as ready for review January 23, 2026 22:30
@jonrohan jonrohan requested a review from a team as a code owner January 23, 2026 22:30
Copilot AI review requested due to automatic review settings January 23, 2026 22:30
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-dependent require with require_relative for FormHelper loading
  • Remove the begin/rescue block 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.

@jonrohan jonrohan enabled auto-merge January 29, 2026 05:43
@jonrohan jonrohan requested a review from TylerJDev January 29, 2026 05:43
@jonrohan jonrohan added this pull request to the merge queue Jan 30, 2026
Merged via the queue into main with commit f51244e Jan 30, 2026
38 checks passed
@jonrohan jonrohan deleted the copilot/fix-helper-primer-form-helper branch January 30, 2026 18:39
@primer primer Bot mentioned this pull request Jan 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

helper Primer::FormHelper is incompatible with Rails 7+ config

4 participants