Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

use backtrace_cleaners when using Deprecation#warn

#3015

On ticket #2932 Deprecation warning for assert_redirected_to doesn't tell you where the offen..., we're seeing the deprecation warning isn't displaying the line that is using the deprecated code. Instead, it's showing some from inside the Rails framework.

There's already infrastructure in place for removing Rails framework stuff from backtraces, the Rails.backtrace_cleaner, so it would make sense to pass the callstack through it. The top item of the cleaned callstack should be then be the line where the deprecated code was called.

Reported by Josh Nichols · August 8th, 2009 @ 10:51 PM

State: stale
Milestone: none
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Josh Nichols
    Josh Nichols

    I've attached a patch which does this. I didn't include any tests, because I didn't find any good examples of tests verifying the use of Rails.backtrace_cleaner in other parts of the code base.

    I was able to verify its use though:

    rails 3015-verification
    cd 3015-verification
    script/generate scaffold beers
    # edit app/controller/beers_controller.rb to use redirect to formatted_beers_url in a few places
    script/server
    # go to http://localhost:3000/beers then click new, and create
    # check server log to see the deprecation notice points at the line the deprecated formated_beers_url was used.
    

    August 8th, 2009 @ 11:09 PM

  2. Josh Nichols
    Josh Nichols

    This seems like something that'd be useful in 2-3-stable, so attached a patch that applies cleanly there.

    August 8th, 2009 @ 11:33 PM

  3. Ed Ruder
    Ed Ruder

    I tried to reproduce the problem on frozen rails release=2.3.3, but couldn't. I followed Josh's steps to verify the patch, before applying the patch, and always got a reference to the correct line in my beers controller.

    P.S. When I executed script/generate scaffold beers (plural model name) per Josh's steps, the scaffold generator created a broken app! It warned of a plural model/using singularized, and created a Beer model, but the BeersController referenced a Beers model! Is that a known bug?

    August 9th, 2009 @ 05:49 AM

  4. Josh Nichols
    Josh Nichols

    The scaffold should probably be the singular 'beer', pardon my much anticipation for some plural beers which were to happen later in the eveing. The issue you are running into seem to be more generator oriented. Use script/generate beer instead.

    August 9th, 2009 @ 07:52 AM

  5. Dan Croak
    Dan Croak

    +1 verified 2-3-stable patch applies cleanly and produces a nice deprecation warning with file and line number:

    DEPRECATION WARNING: formatted_beers_url() has been deprecated.
    Please pass format to the standard beers_url method instead.

    (called from create at /Users/dancroak/dev/3015/app/controllers/beers_controller.rb:48)

    For anyone else looking to verify, my steps, including generating a Rails app with the applied patch:

    downloaded the 3015-stable patch into a bugmash directory
    git apply ~/bugmash/3015-stable-clean_deprecation_callstack.patch
    ruby ~/dev/rails/railties/bin/rails 3015
    cd 3015
    script/generate scaffold beer
    rake db:migrate
    edit line 48 of app/controllers/beers_controller.rb to include redirect_to(formatted_beers_url)
    script/server
    created a beer, observed the backtrace in the development log
    

    August 9th, 2009 @ 04:06 PM

  6. Dan Croak
    Dan Croak

    I've attached a patch that is a test to make sure the "called from #{method} at #{file}:#{line_number}" shows in the deprecation warning, but it passes without Josh's patch.

    I did some print debugging without Josh's patch and the warnings all had method/file/line_number information in the deprecation.

    August 9th, 2009 @ 04:43 PM

  7. Dan Croak
    Dan Croak

    Screwed around a bit with mocking the Rails.cleaner. Diff:

    http://gist.github.com/164808

    But getting an error:

    test_assert_deprecation_backtrace_is_cleaned(DeprecationTest):
    NoMethodError: You have a nil object when you didn't expect it!
    You might have expected an instance of Array.
    The error occurred while evaluating nil.first

    I'm giving up on an "integration" test with Rails.cleaner.

    I'd say Josh's patch is commit-able as-is. My unit test may be useful for regressions but not as proof of this particular patch.

    August 9th, 2009 @ 05:18 PM

  8. Dan Croak
    Dan Croak
    • Assigned user set to Pratik

    August 9th, 2009 @ 05:32 PM

  9. Rizwan Reza
    Rizwan Reza
    • Tag changed from bugmash, deprecation to deprecation, help

    January 20th, 2010 @ 11:13 AM

  10. Ryan Bigg
    Ryan Bigg
    • State changed from new to needs-more-info

    Josh, the link to your patch is broken :( Are you able to replicate this?

    June 12th, 2010 @ 03:31 AM

  11. Santiago Pastorino
    Santiago Pastorino
    • State changed from needs-more-info to open
    • Importance changed from to

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 04:37 PM

  12. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:37 PM