palkan / anyway_config

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

::Rails.application.config.credentials.content_path is a String, not a Pathname #131

Closed shoffing closed 1 year ago

shoffing commented 1 year ago

What did you do?

Installed the gem and attempted to run Rails.

What did you expect to happen?

Rails starts successfully.

What actually happened?

Our application defines its credentials like:

config.credentials.content_path = "config/credentials/#{credentials_env}.yml.enc"
config.credentials.key_path = "config/credentials/#{credentials_env}.key"

So content_path is a String.

Your library is trying to treat content_path as a Pathname, and this is causing Rails to error on start:

.../.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/anyway_config-2.4.1/lib/anyway/rails/loaders/credentials.rb:56:in `credentials_path': undefined method `relative_path_from' for "config/credentials/development.yml.enc":String (NoMethodError)

            ::Rails.application.config.credentials.content_path.relative_path_from(::Rails.root).to_s.relative_path_from(::Rails.root).to_s
                                                               ^^^^^^^^^^^^^^^^^^^

https://github.com/palkan/anyway_config/blob/master/lib/anyway/rails/loaders/credentials.rb#L56

Was there a change to credentials in some recent Rails version? How can I fix this?

Additional context

This gem was installed as a dependency of AnyCable.

Environment

Ruby Version:

ruby 3.2.1 (2023-02-08 revision 31819e82c8) [x86_64-linux]

Framework Version (Rails, whatever):

Rails 7.0.4.3

Anyway Config Version:

anyway_config (2.4.1)

shoffing commented 1 year ago

Changing our config to use Rails.root.join fixes the issue:

config.credentials.content_path = Rails.root.join("config/credentials/#{credentials_env}.yml.enc")

However, using Strings for config seems common, e.g. in this stackoverflow post: https://stackoverflow.com/a/69216001/1570142

So, this library should probably have support for this pattern.

palkan commented 1 year ago

Thanks for the report!

It seems that Rails supports both strings and pathnames implicitly, because it always uses Rails.root to convert the path to a relative one: https://github.com/rails/rails/blob/e6af9f55e6140ea396f73dcb60882d18131686a7/railties/lib/rails/commands/credentials/credentials_command.rb#L66

this library should probably have support for this pattern.

Agree. Will provide a fix soon.