palkan / anyway_config

Configuration library for Ruby gems and applications
MIT License
778 stars 52 forks source link

Unexpected behavior of #load_from_secrets when Rails app has not finished initialization #14

Closed sclinede closed 4 years ago

sclinede commented 6 years ago

There could be a case when some config inherited from Anyway::Config (e.g., MyConfig < Anyway::Config) will be instantiated before Rails finished the initialization process. The only caveat here is that when I call MyConfig.new - inside there is a call #load_from_secrets which will ask Rails.application.secrets. If that call happens before Rails finished initialization (e.g., during require of my gem) we could end up with broken Rails.application.secrets, here is why.

Inside Rails that method (#secrets) has memoization and read all the data only once, but secret_token and secret_key_base could be not yet loaded to config.

I suggest resetting the @secrets instance variable of Rails.application if Rails has not finished initialization yet. Otherwise, we'll have a pretty hard debugging case.