skip to content

In Rails 8.1, how do config.active_support.deprecation, disallowed_deprecation_warnings and Rails.application.deprecators help keep the next upgrade cheap?

level: seniorimportance: should knowfreq 32%

answer

  1. log, stderr, silent by environment
  2. :raise, :notify, :report behaviours
  3. disallowed list as a ratchet
  4. one collection of deprecators
  5. no more ActiveSupport::Deprecation.warn

basics

~10 s

config.active_support.deprecation picks how warnings are reported; disallowed_deprecation_warnings with disallowed_deprecation = :raise turns chosen warnings into errors; Rails.application.deprecators applies both to every framework deprecator and to ones you register.

solid answer

~40 s

A generated Rails 8.1 app logs deprecations in development (`config.active_support.deprecation = :log`), prints them to stderr in test (`:stderr`) and silences them in production (`config.active_support.report_deprecations = false`). Other behaviours are `:raise`, `:notify` (an Active Support notification), `:report` (the error reporter) and `:silence`. To stop fixed deprecations from coming back, list them in `config.active_support.disallowed_deprecation_warnings` (strings, regexps or `:all`); matches use the `config.active_support.disallowed_deprecation` behaviour, and a deprecator's default disallowed behaviour is `:raise`. These settings act on `Rails.application.deprecators` (since 7.1), the collection holding each framework's deprecator. Register your own with `Rails.application.deprecators[:catalogue] = ActiveSupport::Deprecation.new("3.0", "Catalogue")` and it inherits them. Calling `ActiveSupport::Deprecation.warn` on the class was removed in 7.2.

code

ruby · 9 lines
ruby
# config/environments/test.rb
Rails.application.configure do
  config.active_support.deprecation = :stderr
  config.active_support.disallowed_deprecation = :raise
  config.active_support.disallowed_deprecation_warnings = [
    /order dependent finder/i,
    "Hold#renew! is deprecated"
  ]
end

go deeper

for a junior

Know that deprecation warnings list what the next Rails upgrade will break, and where they appear in development and test.

for a middle

Explain the behaviours, the generated per-environment settings, and how the disallowed list turns chosen warnings into errors.

for a senior

Run the ratchet in CI, collect production deprecations with :notify or :report, and give internal gems their own registered deprecators.

for a principal

Make a deprecation-free suite the entry ticket for every upgrade, so each minor costs days rather than a project.

## Why deprecations are the upgrade signal Rails pairs every breaking change with a **deprecation notice in the previous minor or major release**. A deprecation warning is therefore a list of the things the next upgrade will break. An app that runs warning-free on its current minor is ready for the next one; an app that ignores them pays all at once. ## Where warnings go by default Each generated environment file picks a behaviour: | Environment | Generated setting | Result | |---|---|---| | development | `config.active_support.deprecation = :log` | written to the Rails logger | | test | `config.active_support.deprecation = :stderr` | printed during the test run | | production | `config.active_support.report_deprecations = false` | all deprecators silenced | The full set of built-in behaviours in `ActiveSupport::Deprecation::DEFAULT_BEHAVIORS`: - `:raise` raises `ActiveSupport::DeprecationException`; - `:stderr` prints to `$stderr`; - `:log` writes to the Rails logger; - `:notify` instruments a `deprecation.<gem name>` event, `deprecation.rails` for Rails' own deprecators, through `ActiveSupport::Notifications`; - `:report` sends the warning to the Rails error reporter; - `:silence` does nothing. `report_deprecations = false` overrides the rest: when it is set, the app silences every deprecator and ignores the other settings. A team that wants production evidence can replace it with `config.active_support.deprecation = :notify` or `:report`. ## The ratchet: disallowed deprecations Fixing a deprecation is only half the job; the other half is stopping it from coming back. Two settings do that: 1. `config.active_support.disallowed_deprecation_warnings` takes an array of strings, symbols or regular expressions matched against the warning text, or the symbol `:all`. 2. `config.active_support.disallowed_deprecation` is the behaviour for matching warnings. A deprecator's default disallowed behaviour is `:raise`. The workflow is a ratchet: 1. Fix one deprecation across the codebase. 2. Add a pattern for its message to the disallowed list in `config/environments/test.rb` (and development if you like). 3. Any new code that triggers it now fails the suite, while the remaining, unfixed warnings still only print. For a known, temporary exception, `deprecator.allow(["pattern"]) { ... }` permits a matching warning inside a block, and `Rails.application.deprecators.silence { ... }` silences all of them for a block. ## One collection for every deprecator In Rails 8.1 each framework owns its own deprecator: `ActiveRecord.deprecator`, `ActionController.deprecator`, `ActiveSupport.deprecator`, `Rails.deprecator` and so on. Their railties register them in `Rails.application.deprecators`, an `ActiveSupport::Deprecation::Deprecators` collection. The `config.active_support.*` deprecation settings are applied to the whole collection, and setting `behavior`, `silenced`, `debug` or `disallowed_warnings` on it reaches every member. An app or an internal gem adds its own deprecator the same way: ```ruby module Catalogue def self.deprecator @deprecator ||= ActiveSupport::Deprecation.new("3.0", "Catalogue") end end Rails.application.deprecators[:catalogue] = Catalogue.deprecator Catalogue.deprecator.warn("Hold#renew! is deprecated, use Hold#extend!") ``` Registering applies the collection's current options to the new deprecator, so the app's environment settings govern it too. ## A worked ratchet A library catalogue app on 8.1 prints a handful of warnings in its test run, among them the finder-order deprecation ("Calling order dependent finder methods ... is deprecated and will raise `ActiveRecord::MissingRequiredOrderError` in Rails 8.2"). A ratchet removes them one by one: 1. Fix every call site the warning names, here by giving the model an order column or the relation an `order`. 2. Add `/order dependent finder/i` to `disallowed_deprecation_warnings` in `config/environments/test.rb`. 3. Merge. From now on a new unordered `first` on such a model fails the suite instead of printing. 4. Pick the next warning. When none is left, replace the list with `:all`. In production, switching from silence to `:notify` gives a count of the warnings real traffic triggers, which the test suite may never reach. ## Version notes on the path to 8.1 - `Rails.application.deprecators` arrived in 7.1, and 7.1 deprecated the singleton `ActiveSupport::Deprecation`. - 7.2 removed the delegation from the class to an instance, so `ActiveSupport::Deprecation.warn("...")` no longer works; use a deprecator instance. - Rails 8.1's deprecator names `8.2` as its horizon, so its messages announce removal in Rails 8.2. That is a plan, not shipped behaviour.

  • Why might a production app replace report_deprecations = false with :notify or :report?
    The generated production setting silences everything, so warnings triggered only by production data or traffic are never seen. `:notify` instruments `deprecation.rails` for a subscriber to count, and `:report` sends each warning to the Rails error reporter, giving evidence before the next upgrade without failing requests.
  • Why not set config.active_support.deprecation = :raise in test straight away?
    It raises on every deprecation at once, so a suite with many unfixed warnings fails wholesale and blocks other work. The disallowed list lets you fail only on what you have already fixed and tighten it one deprecation at a time, ending at `:all` when the app is clean.

  • Production logs deprecation warnings by default, so we will see them there.
  • config.active_support.deprecation = :raise is the only way to fail on a deprecation.
  • Call ActiveSupport::Deprecation.warn for app deprecations, as before.
  • A custom deprecator ignores the app's deprecation settings.
  • Deprecation warnings are noise until the version that removes the API.

Assumes Rails 8.1; Rails.application.deprecators exists since 7.1, and calling ActiveSupport::Deprecation.warn on the class was removed in 7.2.