Skip to content

Do not autoload ActionText::ContentHelper helper on boot#54914

Merged
rafaelfranca merged 1 commit intorails:mainfrom
chaadow:fix_autoloaded_content_helper
Oct 21, 2025
Merged

Do not autoload ActionText::ContentHelper helper on boot#54914
rafaelfranca merged 1 commit intorails:mainfrom
chaadow:fix_autoloaded_content_helper

Conversation

@chaadow
Copy link
Copy Markdown
Contributor

@chaadow chaadow commented Apr 13, 2025

Motivation / Background

After watching @bensheldon 's talk An ok compromise. Faster development by designing for the Rails Autoloader I got motivated to try and run the script mentionned in the video on my production rails app (which runs on edge rails ).

Ben mentions that ActionText::ContentHelper is autoloaded at this timestamp

I've managed to replicate it on my own application.

Along with this script ( which I've edited a bit to display more messages and add it to our CI ):

# frozen_string_literal: true

require_relative "../../config/application"

autoloaded_constants = []

Rails.autoloaders.each do |loader|
  loader.on_load do |cpath, _value, _abspath|
    autoloaded_constants << [cpath, caller]
  end
end

Rails.application.initialize!

autoloaded_constants.each do |x|
  x[1] = Rails.backtrace_cleaner.clean(x[1]).first
end

# ActionText::ContentHelper
#
if autoloaded_constants.any?
  puts
  puts
  puts "ERROR: Autoloaded constants were referenced during during boot."
  puts
  puts "These files/constants were autoloaded during the boot process, which will result in" \
       "inconsistent behavior and will slow down and may break development mode. " \
       "Remove references to these constants from code loaded at boot. "
  puts
  puts
  w = autoloaded_constants.map { _1.first.length }.max
  autoloaded_constants.each do |name, location|
    puts "`#{name.ljust(w)}` referenced by #{location}"
  end

  exit 1
else
  puts "SUCCESS! No autoloaded constants were found during the boot process."
  exit 0
end

Detail

  • I've wrapped the setting of the sanitizer around the loading of ActionView::Base
  • I've removed app/helpers from the autoload once paths

Additional information

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@rails-bot rails-bot Bot added the actiontext label Apr 13, 2025
@chaadow chaadow force-pushed the fix_autoloaded_content_helper branch from 057be0e to b48e01a Compare April 13, 2025 12:08
@rafaelfranca rafaelfranca self-assigned this Oct 11, 2025
Comment thread actiontext/lib/action_text/engine.rb Outdated
config.after_initialize do |app|
if klass = app.config.action_text.sanitizer_vendor
ActionText::ContentHelper.sanitizer = klass.safe_list_sanitizer.new
ActiveSupport.on_load(:action_view_base) do
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

i've opted for this hook because the ContentHelper is defined inside app/helpers as part of the engine.

@rafaelfranca
Copy link
Copy Markdown
Member

This PR broke the CI

@chaadow chaadow force-pushed the fix_autoloaded_content_helper branch from b48e01a to 911ed08 Compare October 20, 2025 20:50
Comment thread actiontext/lib/action_text/engine.rb Outdated
@chaadow chaadow force-pushed the fix_autoloaded_content_helper branch from 911ed08 to 708eba4 Compare October 20, 2025 23:26
@rails-bot rails-bot Bot added the railties label Oct 20, 2025
@chaadow chaadow force-pushed the fix_autoloaded_content_helper branch from 708eba4 to fcc32ce Compare October 20, 2025 23:28
@chaadow chaadow force-pushed the fix_autoloaded_content_helper branch from fcc32ce to 43c4495 Compare October 20, 2025 23:29
@chaadow chaadow requested a review from rafaelfranca October 21, 2025 15:53
@rafaelfranca rafaelfranca merged commit 1b589c4 into rails:main Oct 21, 2025
3 checks passed
rafaelfranca added a commit that referenced this pull request Oct 21, 2025
Do not autoload ActionText::ContentHelper helper on boot
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.

2 participants