Skip to content

Commit 207da35

Browse files
authored
Merge pull request rails#42913 from ghiculescu/deprecation-opt-out
Allow entirely opting out of deprecation warnings
2 parents 549ef60 + ea3185e commit 207da35

File tree

7 files changed

+64
-26
lines changed

7 files changed

+64
-26
lines changed

activesupport/CHANGELOG.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
* Allow entirely opting out of deprecation warnings
2+
3+
Previously if you did `app.config.active_support.deprecation = :silence`, some work would
4+
still be done on each call to `ActiveSupport::Deprecation.warn`. In very hot paths, this could
5+
cause performance issues.
6+
7+
Now, you can make `ActiveSupport::Deprecation.warn` a no-op:
8+
9+
```ruby
10+
config.active_support.report_deprecations = false
11+
```
12+
13+
This is the default in production for new apps. It is the equivalent to:
14+
15+
```ruby
16+
config.active_support.deprecation = :silence
17+
config.active_support.disallowed_deprecation = :silence
18+
```
19+
20+
but will take a more optimised code path.
21+
22+
*Alex Ghiculescu*
23+
124
* Faster tests by parallelizing only when overhead is justified by the number
225
of them.
326

activesupport/lib/active_support/deprecation/behaviors.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class Deprecation
5454
# [+stderr+] Log all deprecation warnings to <tt>$stderr</tt>.
5555
# [+log+] Log all deprecation warnings to +Rails.logger+.
5656
# [+notify+] Use +ActiveSupport::Notifications+ to notify +deprecation.rails+.
57-
# [+silence+] Do nothing.
57+
# [+silence+] Do nothing. On Rails, set `config.active_support.report_deprecations = false` to disable all behaviors.
5858
#
5959
# Setting behaviors only affects deprecations that happen after boot time.
6060
# For more information you can read the documentation of the +behavior=+ method.
@@ -93,6 +93,9 @@ def disallowed_behavior
9393
# ActiveSupport::Deprecation.behavior = ->(message, callstack, deprecation_horizon, gem_name) {
9494
# # custom stuff
9595
# }
96+
#
97+
# If you are using Rails, you can set `config.active_support.report_deprecations = false` to disable
98+
# all deprecation behaviors. This is similar to the `silence` option but more performant.
9699
def behavior=(behavior)
97100
@behavior = Array(behavior).map { |b| DEFAULT_BEHAVIORS[b] || arity_coerce(b) }
98101
end

activesupport/lib/active_support/railtie.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,22 @@ class Railtie < Rails::Railtie # :nodoc:
3939
end
4040

4141
initializer "active_support.deprecation_behavior" do |app|
42-
if deprecation = app.config.active_support.deprecation
43-
ActiveSupport::Deprecation.behavior = deprecation
44-
end
42+
if app.config.active_support.report_deprecations == false
43+
ActiveSupport::Deprecation.silenced = true
44+
ActiveSupport::Deprecation.behavior = :silence
45+
ActiveSupport::Deprecation.disallowed_behavior = :silence
46+
else
47+
if deprecation = app.config.active_support.deprecation
48+
ActiveSupport::Deprecation.behavior = deprecation
49+
end
4550

46-
if disallowed_deprecation = app.config.active_support.disallowed_deprecation
47-
ActiveSupport::Deprecation.disallowed_behavior = disallowed_deprecation
48-
end
51+
if disallowed_deprecation = app.config.active_support.disallowed_deprecation
52+
ActiveSupport::Deprecation.disallowed_behavior = disallowed_deprecation
53+
end
4954

50-
if disallowed_warnings = app.config.active_support.disallowed_deprecation_warnings
51-
ActiveSupport::Deprecation.disallowed_warnings = disallowed_warnings
55+
if disallowed_warnings = app.config.active_support.disallowed_deprecation_warnings
56+
ActiveSupport::Deprecation.disallowed_warnings = disallowed_warnings
57+
end
5258
end
5359
end
5460

activesupport/test/deprecation_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,12 @@ def test_silence
298298
end
299299

300300
ActiveSupport::Deprecation.silenced = true
301+
assert ActiveSupport::Deprecation.silenced
302+
301303
assert_not_deprecated { @dtc.partially }
304+
302305
ActiveSupport::Deprecation.silenced = false
306+
assert_not ActiveSupport::Deprecation.silenced
303307
end
304308

305309
def test_silence_threaded

guides/source/configuring.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -922,19 +922,17 @@ There are a few configuration options available in Active Support:
922922
923923
* `config.active_support.cache_format_version` specifies which version of the cache serializer to use. Possible values are `6.1` and `7.0`.
924924
925-
* `ActiveSupport::Logger.silencer` is set to `false` to disable the ability to silence logging in a block. The default is `true`.
926-
927-
* `ActiveSupport::Cache::Store.logger` specifies the logger to use within cache store operations.
925+
* `config.active_support.deprecation` configures the behavior of deprecation warnings. The options are `:raise`, `:stderr`, `:log`, `:notify`, or `:silence`. The default is `:stderr`. Alternatively, you can set `ActiveSupport::Deprecation.behavior`.
928926
929-
* `ActiveSupport::Deprecation.behavior` alternative setter to `config.active_support.deprecation` which configures the behavior of deprecation warnings for Rails.
927+
* `config.active_support.disallowed_deprecation` configures the behavior of disallowed deprecation warnings. The options are `:raise`, `:stderr`, `:log`, `:notify`, or `:silence`. The default is `:raise`. Alternatively, you can set `ActiveSupport::Deprecation.disallowed_behavior`.
930928
931-
* `ActiveSupport::Deprecation.disallowed_behavior` alternative setter to `config.active_support.disallowed_deprecation` which configures the behavior of disallowed deprecation warnings for Rails.
929+
* `config.active_support.disallowed_deprecation_warnings` configures deprecation warnings that the Application considers disallowed. This allows, for example, specific deprecations to be treated as hard failures. Alternatively, you can set `ActiveSupport::Deprecation.disallowed_warnings`.
932930
933-
* `ActiveSupport::Deprecation.disallowed_warnings` alternative setter to `config.active_support.disallowed_deprecation_warnings` which configures deprecation warnings that the Application considers disallowed. This allows, for example, specific deprecations to be treated as hard failures.
931+
* `config.active_support.report_deprecations` allows you to disable all deprecation warnings (including disallowed deprecations); it makes `ActiveSupport::Deprecation.warn` a no-op. This is enabled by default in production.
934932
935-
* `ActiveSupport::Deprecation.silence` takes a block in which all deprecation warnings are silenced.
933+
* `ActiveSupport::Logger.silencer` is set to `false` to disable the ability to silence logging in a block. The default is `true`.
936934
937-
* `ActiveSupport::Deprecation.silenced` sets whether or not to display deprecation warnings. The default is `false`.
935+
* `ActiveSupport::Cache::Store.logger` specifies the logger to use within cache store operations.
938936
939937
* `ActiveSupport.utc_to_local_returns_utc_offset_times` configures
940938
`ActiveSupport::TimeZone.utc_to_local` to return a time with a UTC offset
@@ -1673,7 +1671,7 @@ Below is a comprehensive list of all the initializers found in Rails in the orde
16731671
16741672
* `i18n.callbacks`: In the development environment, sets up a `to_prepare` callback which will call `I18n.reload!` if any of the locales have changed since the last request. In production this callback will only run on the first request.
16751673
1676-
* `active_support.deprecation_behavior`: Sets up deprecation reporting for environments, defaulting to `:log` for development, `:notify` for production, and `:stderr` for test. If a value isn't set for `config.active_support.deprecation` then this initializer will prompt the user to configure this line in the current environment's `config/environments` file. Can be set to an array of values. This initializer also sets up behaviors for disallowed deprecations, defaulting to `:raise` for development and test and `:log` for production. Disallowed deprecation warnings default to an empty array.
1674+
* `active_support.deprecation_behavior`: Sets up deprecation reporting for environments, defaulting to `:log` for development, `:silence` for production, and `:stderr` for test. Can be set to an array of values. This initializer also sets up behaviors for disallowed deprecations, defaulting to `:raise` for development and test and `:silence` for production. Disallowed deprecation warnings default to an empty array.
16771675
16781676
* `active_support.initialize_time_zone`: Sets the default time zone for the application based on the `config.time_zone` setting, which defaults to "UTC".
16791677

railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,8 @@ Rails.application.configure do
8484
# the I18n.default_locale when a translation cannot be found).
8585
config.i18n.fallbacks = true
8686

87-
# Send deprecation notices to registered listeners.
88-
config.active_support.deprecation = :notify
89-
90-
# Log disallowed deprecations.
91-
config.active_support.disallowed_deprecation = :log
92-
93-
# Tell Active Support which deprecation messages to disallow.
94-
config.active_support.disallowed_deprecation_warnings = []
87+
# Don't log any deprecations.
88+
config.active_support.report_deprecations = false
9589

9690
# Use default logging formatter so that PID and timestamp are not suppressed.
9791
config.log_formatter = ::Logger::Formatter.new

railties/test/application/configuration_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3459,6 +3459,16 @@ def new(app); self; end
34593459
assert_equal true, ActiveSupport::TimeWithZone.methods(false).include?(:name)
34603460
end
34613461

3462+
test "can entirely opt out of ActiveSupport::Deprecations" do
3463+
add_to_config "config.active_support.report_deprecations = false"
3464+
3465+
app "production"
3466+
3467+
assert_equal true, ActiveSupport::Deprecation.silenced
3468+
assert_equal [ActiveSupport::Deprecation::DEFAULT_BEHAVIORS[:silence]], ActiveSupport::Deprecation.behavior
3469+
assert_equal [ActiveSupport::Deprecation::DEFAULT_BEHAVIORS[:silence]], ActiveSupport::Deprecation.disallowed_behavior
3470+
end
3471+
34623472
private
34633473
def set_custom_config(contents, config_source = "custom".inspect)
34643474
app_file "config/custom.yml", contents

0 commit comments

Comments
 (0)