Only enable routing source locations when using routes command

The overhead isn't necessary for development when not using the routes
command. We can omit it entirely by checking for existence of the routes
command constant.
This commit is contained in:
Gannon McGibbon 2024-07-10 18:40:57 -05:00
parent 99fd4c0c90
commit e3cc8fe707
5 changed files with 14 additions and 2 deletions

@ -68,8 +68,6 @@ class Railtie < Rails::Railtie # :nodoc:
config.action_dispatch.always_write_cookie = Rails.env.development? if config.action_dispatch.always_write_cookie.nil?
ActionDispatch::Cookies::CookieJar.always_write_cookie = config.action_dispatch.always_write_cookie
ActionDispatch::Routing::Mapper.route_source_locations = Rails.env.development?
ActionDispatch::Http::Cache::Request.strict_freshness = app.config.action_dispatch.strict_freshness
ActionDispatch.test_app = app
end

@ -6,6 +6,7 @@
require "active_support/core_ext/enumerable"
require "active_support/core_ext/array/extract_options"
require "active_support/core_ext/regexp"
require "action_dispatch/routing"
require "action_dispatch/routing/redirection"
require "action_dispatch/routing/endpoint"

@ -1,3 +1,8 @@
* Enable tracking route source locations only when using routes command. Previously,
it was enabled in development mode, but is only needed for `bin/rails routes -E`.
*Gannon McGibbon*
* Deprecate `bin/rake stats` in favor of `bin/rails stats`.
*Juan Vásquez*

@ -22,6 +22,8 @@ def invoke_command(*)
desc "routes", "List all the defined routes"
def perform(*)
require "action_dispatch/routing/mapper"
ActionDispatch::Routing::Mapper.route_source_locations = true
boot_application!
require "action_dispatch/routing/inspector"

@ -403,6 +403,12 @@ class Rails::Command::RoutesTest < ActiveSupport::TestCase
assert_includes(output, "No unused routes found.")
end
test "route source locations aren't included when booted normally" do
output = rails "runner", "print !!ActionDispatch::Routing::Mapper.route_source_locations"
assert_equal("false", output)
end
private
def run_routes_command(args = [])
rails "routes", args