From e3cc8fe707f194fe24dcc9d8ea0cb2db3bed6861 Mon Sep 17 00:00:00 2001 From: Gannon McGibbon Date: Wed, 10 Jul 2024 18:40:57 -0500 Subject: [PATCH] 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. --- actionpack/lib/action_dispatch/railtie.rb | 2 -- actionpack/lib/action_dispatch/routing/mapper.rb | 1 + railties/CHANGELOG.md | 5 +++++ railties/lib/rails/commands/routes/routes_command.rb | 2 ++ railties/test/commands/routes_test.rb | 6 ++++++ 5 files changed, 14 insertions(+), 2 deletions(-) diff --git a/actionpack/lib/action_dispatch/railtie.rb b/actionpack/lib/action_dispatch/railtie.rb index c66e2744d4..18d3a7d317 100644 --- a/actionpack/lib/action_dispatch/railtie.rb +++ b/actionpack/lib/action_dispatch/railtie.rb @@ -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 diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 8701806419..3a6cc8e3f1 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -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" diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 9b7504cb07..cb5afb68c5 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -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* diff --git a/railties/lib/rails/commands/routes/routes_command.rb b/railties/lib/rails/commands/routes/routes_command.rb index c979a9014b..a19a653bd1 100644 --- a/railties/lib/rails/commands/routes/routes_command.rb +++ b/railties/lib/rails/commands/routes/routes_command.rb @@ -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" diff --git a/railties/test/commands/routes_test.rb b/railties/test/commands/routes_test.rb index 58ccad1516..74a9dfadee 100644 --- a/railties/test/commands/routes_test.rb +++ b/railties/test/commands/routes_test.rb @@ -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