|
| 1 | +--- |
| 2 | +created_at: 2025-08-01 13:07:00 +0200 |
| 3 | +author: Łukasz Reszke |
| 4 | +tags: [] |
| 5 | +publish: false |
| 6 | +--- |
| 7 | + |
| 8 | +# Watch out for this one deprecation warning when upgrading from Rails 7.1 to 7.2 on Heroku |
| 9 | + |
| 10 | +We recently upgraded Rails from 7.0 to 7.1 for one of our clients. It went smoothly. |
| 11 | + |
| 12 | +When Rails 7.1 went live, we were pleased to see a new set of deprecation warnings. To avoid being overwhelmed by them, we decided to address the issue right away. |
| 13 | + |
| 14 | +However, we ran into a nasty issue. |
| 15 | + |
| 16 | +The application didn't crash. |
| 17 | + |
| 18 | +The server wasn't throwing 500s like a crazy Viking throwing axes. |
| 19 | + |
| 20 | +Either of those would have been better. The worst that can happen is silence. |
| 21 | + |
| 22 | +The deprecation warning was: |
| 23 | + |
| 24 | +```ruby |
| 25 | +[DEPRECATION] DEPRECATION WARNING: `Rails.application.secrets` is deprecated in favor of `Rails.application.credentials` and will be removed in Rails 7.2. |
| 26 | +``` |
| 27 | + |
| 28 | +We removed values from `ENV["SECRET_KEY_BASE"]` to credentials and checked that the value was correct by calling |
| 29 | +`Rails.application.credentials.secret_key_base`. |
| 30 | + |
| 31 | +It turned out that you can also get the secret_key_base by calling `Rails.application.secret_key_base`. |
| 32 | + |
| 33 | + |
| 34 | +Let's take a look at this code:
|
| 35 | + |
| 36 | +```ruby |
| 37 | +def secret_key_base |
| 38 | + if Rails.env.development? || Rails.env.test? |
| 39 | + secrets.secret_key_base ||= generate_development_secret |
| 40 | + else |
| 41 | + validate_secret_key_base( |
| 42 | + ENV["SECRET_KEY_BASE"] || credentials.secret_key_base || secrets.secret_key_base |
| 43 | + ) |
| 44 | + end |
| 45 | +end |
| 46 | +``` |
| 47 | + |
| 48 | +Ok so to sum it up, until now: |
| 49 | +- We removed ENV |
| 50 | +- So it should take the value from credentials |
| 51 | + |
| 52 | +Right? |
| 53 | + |
| 54 | +But instead... |
| 55 | + |
| 56 | +Instead it failed silently. So where’s the poop? |
| 57 | + |
| 58 | + |
| 59 | +The poop is in Heroku trying to be smarter than developers. Unfortunately. It turned out that removing `SECRET_KEY_BASE` env leads to.. regenerating it with new **random** value. |
| 60 | + |
| 61 | +So our external devices depending on it couldn’t work because of new, randomly generated key. |
| 62 | + |
| 63 | +## Summary |
| 64 | + |
| 65 | +To sum it up: |
| 66 | +- If you’re getting rid of the `Rails.application.secrets` is deprecated in favor of `Rails.application.credentials` and will be removed in Rails 7.2 |
| 67 | +- And you’re on Heroku |
| 68 | +- And you’re using Heroku Buildpacks |
| 69 | +- Make sure you keep `SECRET_KEY_BASE` in both credentials and in Heroku ENV variable |
| 70 | +- Either way... you may end up in nasty silent error. Which is not good. |
0 commit comments