Skip to content

Commit 2c5fe6f

Browse files
authored
Merge pull request rails#50964 from Shopify/ruby-head-route-location
Improve routes source location detection
2 parents 0add5db + bbc7ec4 commit 2c5fe6f

File tree

4 files changed

+48
-14
lines changed

4 files changed

+48
-14
lines changed

actionpack/lib/action_dispatch/railtie.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ class Railtie < Rails::Railtie # :nodoc:
6666
ActionDispatch::Cookies::CookieJar.always_write_cookie = config.action_dispatch.always_write_cookie
6767

6868
ActionDispatch::Routing::Mapper.route_source_locations = Rails.env.development?
69-
ActionDispatch::Routing::Mapper.backtrace_cleaner = Rails.backtrace_cleaner
7069

7170
ActionDispatch.test_app = app
7271
end

actionpack/lib/action_dispatch/routing/mapper.rb

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,19 @@
1010
module ActionDispatch
1111
module Routing
1212
class Mapper
13+
class BacktraceCleaner < ActiveSupport::BacktraceCleaner # :nodoc:
14+
def initialize
15+
super
16+
remove_silencers!
17+
add_core_silencer
18+
add_stdlib_silencer
19+
end
20+
end
21+
1322
URL_OPTIONS = [:protocol, :subdomain, :domain, :host, :port]
1423

1524
cattr_accessor :route_source_locations, instance_accessor: false, default: false
16-
cattr_accessor :backtrace_cleaner, instance_accessor: false, default: ActiveSupport::BacktraceCleaner.new
25+
cattr_accessor :backtrace_cleaner, instance_accessor: false, default: BacktraceCleaner.new
1726

1827
class Constraints < Routing::Endpoint # :nodoc:
1928
attr_reader :app, :constraints
@@ -366,11 +375,10 @@ def route_source_location
366375
Thread.each_caller_location do |location|
367376
next if location.path.start_with?(action_dispatch_dir)
368377

369-
if cleaned_path = Mapper.backtrace_cleaner.clean_frame(location.path)
370-
return "#{cleaned_path}:#{location.lineno}"
371-
else
372-
return nil
373-
end
378+
cleaned_path = Mapper.backtrace_cleaner.clean_frame(location.path)
379+
next if cleaned_path.nil?
380+
381+
return "#{cleaned_path}:#{location.lineno}"
374382
end
375383
nil
376384
end
@@ -382,11 +390,10 @@ def route_source_location
382390
caller_locations.each do |location|
383391
next if location.path.start_with?(action_dispatch_dir)
384392

385-
if cleaned_path = Mapper.backtrace_cleaner.clean_frame(location.path)
386-
return "#{cleaned_path}:#{location.lineno}"
387-
else
388-
return nil
389-
end
393+
cleaned_path = Mapper.backtrace_cleaner.clean_frame(location.path)
394+
next if cleaned_path.nil?
395+
396+
return "#{cleaned_path}:#{location.lineno}"
390397
end
391398
nil
392399
end

railties/lib/rails/backtrace_cleaner.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ class BacktraceCleaner < ActiveSupport::BacktraceCleaner # :nodoc:
1010

1111
def initialize
1212
super
13-
@root = "#{Rails.root}/"
1413
add_filter do |line|
15-
line.start_with?(@root) ? line.from(@root.size) : line
14+
# We may be called before Rails.root is assigned.
15+
# When that happens we fallback to not truncating.
16+
@root ||= Rails.root && "#{Rails.root}/"
17+
@root && line.start_with?(@root) ? line.from(@root.size) : line
1618
end
1719
add_filter do |line|
1820
if RENDER_TEMPLATE_PATTERN.match?(line)

railties/test/commands/routes_test.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,127 +242,153 @@ class Rails::Command::RoutesTest < ActiveSupport::TestCase
242242
run_routes_command([ "--expanded" ])
243243
end
244244

245+
rails_gem_root = File.expand_path("../../../../", __FILE__)
246+
245247
assert_equal <<~MESSAGE, output
246248
--[ Route 1 ]--------------
247249
Prefix | cart
248250
Verb | GET
249251
URI | /cart(.:format)
250252
Controller#Action | cart#show
253+
Source Location | #{app_path}/config/routes.rb:2
251254
--[ Route 2 ]--------------
252255
Prefix | rails_postmark_inbound_emails
253256
Verb | POST
254257
URI | /rails/action_mailbox/postmark/inbound_emails(.:format)
255258
Controller#Action | action_mailbox/ingresses/postmark/inbound_emails#create
259+
Source Location | #{rails_gem_root}/actionmailbox/config/routes.rb:5
256260
--[ Route 3 ]--------------
257261
Prefix | rails_relay_inbound_emails
258262
Verb | POST
259263
URI | /rails/action_mailbox/relay/inbound_emails(.:format)
260264
Controller#Action | action_mailbox/ingresses/relay/inbound_emails#create
265+
Source Location | #{rails_gem_root}/actionmailbox/config/routes.rb:6
261266
--[ Route 4 ]--------------
262267
Prefix | rails_sendgrid_inbound_emails
263268
Verb | POST
264269
URI | /rails/action_mailbox/sendgrid/inbound_emails(.:format)
265270
Controller#Action | action_mailbox/ingresses/sendgrid/inbound_emails#create
271+
Source Location | #{rails_gem_root}/actionmailbox/config/routes.rb:7
266272
--[ Route 5 ]--------------
267273
Prefix | rails_mandrill_inbound_health_check
268274
Verb | GET
269275
URI | /rails/action_mailbox/mandrill/inbound_emails(.:format)
270276
Controller#Action | action_mailbox/ingresses/mandrill/inbound_emails#health_check
277+
Source Location | #{rails_gem_root}/actionmailbox/config/routes.rb:10
271278
--[ Route 6 ]--------------
272279
Prefix | rails_mandrill_inbound_emails
273280
Verb | POST
274281
URI | /rails/action_mailbox/mandrill/inbound_emails(.:format)
275282
Controller#Action | action_mailbox/ingresses/mandrill/inbound_emails#create
283+
Source Location | #{rails_gem_root}/actionmailbox/config/routes.rb:11
276284
--[ Route 7 ]--------------
277285
Prefix | rails_mailgun_inbound_emails
278286
Verb | POST
279287
URI | /rails/action_mailbox/mailgun/inbound_emails/mime(.:format)
280288
Controller#Action | action_mailbox/ingresses/mailgun/inbound_emails#create
289+
Source Location | #{rails_gem_root}/actionmailbox/config/routes.rb:14
281290
--[ Route 8 ]--------------
282291
Prefix | rails_conductor_inbound_emails
283292
Verb | GET
284293
URI | /rails/conductor/action_mailbox/inbound_emails(.:format)
285294
Controller#Action | rails/conductor/action_mailbox/inbound_emails#index
295+
Source Location | #{rails_gem_root}/actionmailbox/config/routes.rb:19
286296
--[ Route 9 ]--------------
287297
Prefix |#{" "}
288298
Verb | POST
289299
URI | /rails/conductor/action_mailbox/inbound_emails(.:format)
290300
Controller#Action | rails/conductor/action_mailbox/inbound_emails#create
301+
Source Location | #{rails_gem_root}/actionmailbox/config/routes.rb:19
291302
--[ Route 10 ]-------------
292303
Prefix | new_rails_conductor_inbound_email
293304
Verb | GET
294305
URI | /rails/conductor/action_mailbox/inbound_emails/new(.:format)
295306
Controller#Action | rails/conductor/action_mailbox/inbound_emails#new
307+
Source Location | #{rails_gem_root}/actionmailbox/config/routes.rb:19
296308
--[ Route 11 ]-------------
297309
Prefix | rails_conductor_inbound_email
298310
Verb | GET
299311
URI | /rails/conductor/action_mailbox/inbound_emails/:id(.:format)
300312
Controller#Action | rails/conductor/action_mailbox/inbound_emails#show
313+
Source Location | #{rails_gem_root}/actionmailbox/config/routes.rb:19
301314
--[ Route 12 ]-------------
302315
Prefix | new_rails_conductor_inbound_email_source
303316
Verb | GET
304317
URI | /rails/conductor/action_mailbox/inbound_emails/sources/new(.:format)
305318
Controller#Action | rails/conductor/action_mailbox/inbound_emails/sources#new
319+
Source Location | #{rails_gem_root}/actionmailbox/config/routes.rb:20
306320
--[ Route 13 ]-------------
307321
Prefix | rails_conductor_inbound_email_sources
308322
Verb | POST
309323
URI | /rails/conductor/action_mailbox/inbound_emails/sources(.:format)
310324
Controller#Action | rails/conductor/action_mailbox/inbound_emails/sources#create
325+
Source Location | #{rails_gem_root}/actionmailbox/config/routes.rb:21
311326
--[ Route 14 ]-------------
312327
Prefix | rails_conductor_inbound_email_reroute
313328
Verb | POST
314329
URI | /rails/conductor/action_mailbox/:inbound_email_id/reroute(.:format)
315330
Controller#Action | rails/conductor/action_mailbox/reroutes#create
331+
Source Location | #{rails_gem_root}/actionmailbox/config/routes.rb:23
316332
--[ Route 15 ]-------------
317333
Prefix | rails_conductor_inbound_email_incinerate
318334
Verb | POST
319335
URI | /rails/conductor/action_mailbox/:inbound_email_id/incinerate(.:format)
320336
Controller#Action | rails/conductor/action_mailbox/incinerates#create
337+
Source Location | #{rails_gem_root}/actionmailbox/config/routes.rb:24
321338
--[ Route 16 ]-------------
322339
Prefix | rails_service_blob
323340
Verb | GET
324341
URI | /rails/active_storage/blobs/redirect/:signed_id/*filename(.:format)
325342
Controller#Action | active_storage/blobs/redirect#show
343+
Source Location | #{rails_gem_root}/activestorage/config/routes.rb:5
326344
--[ Route 17 ]-------------
327345
Prefix | rails_service_blob_proxy
328346
Verb | GET
329347
URI | /rails/active_storage/blobs/proxy/:signed_id/*filename(.:format)
330348
Controller#Action | active_storage/blobs/proxy#show
349+
Source Location | #{rails_gem_root}/activestorage/config/routes.rb:6
331350
--[ Route 18 ]-------------
332351
Prefix |#{" "}
333352
Verb | GET
334353
URI | /rails/active_storage/blobs/:signed_id/*filename(.:format)
335354
Controller#Action | active_storage/blobs/redirect#show
355+
Source Location | #{rails_gem_root}/activestorage/config/routes.rb:7
336356
--[ Route 19 ]-------------
337357
Prefix | rails_blob_representation
338358
Verb | GET
339359
URI | /rails/active_storage/representations/redirect/:signed_blob_id/:variation_key/*filename(.:format)
340360
Controller#Action | active_storage/representations/redirect#show
361+
Source Location | #{rails_gem_root}/activestorage/config/routes.rb:9
341362
--[ Route 20 ]-------------
342363
Prefix | rails_blob_representation_proxy
343364
Verb | GET
344365
URI | /rails/active_storage/representations/proxy/:signed_blob_id/:variation_key/*filename(.:format)
345366
Controller#Action | active_storage/representations/proxy#show
367+
Source Location | #{rails_gem_root}/activestorage/config/routes.rb:10
346368
--[ Route 21 ]-------------
347369
Prefix |#{" "}
348370
Verb | GET
349371
URI | /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format)
350372
Controller#Action | active_storage/representations/redirect#show
373+
Source Location | #{rails_gem_root}/activestorage/config/routes.rb:11
351374
--[ Route 22 ]-------------
352375
Prefix | rails_disk_service
353376
Verb | GET
354377
URI | /rails/active_storage/disk/:encoded_key/*filename(.:format)
355378
Controller#Action | active_storage/disk#show
379+
Source Location | #{rails_gem_root}/activestorage/config/routes.rb:13
356380
--[ Route 23 ]-------------
357381
Prefix | update_rails_disk_service
358382
Verb | PUT
359383
URI | /rails/active_storage/disk/:encoded_token(.:format)
360384
Controller#Action | active_storage/disk#update
385+
Source Location | #{rails_gem_root}/activestorage/config/routes.rb:14
361386
--[ Route 24 ]-------------
362387
Prefix | rails_direct_uploads
363388
Verb | POST
364389
URI | /rails/active_storage/direct_uploads(.:format)
365390
Controller#Action | active_storage/direct_uploads#create
391+
Source Location | #{rails_gem_root}/activestorage/config/routes.rb:15
366392
MESSAGE
367393
end
368394

0 commit comments

Comments
 (0)