fireproofsocks
Limits on what code modules you can call from inside config files
I was trying to add some code to one of my config files in order to convert the value from a string (as it would be read from an ENV variable) to a boolean – I had a custom is_truthy? function for this. However, I got compilation errors saying that MyApp is not available. I didn’t realize that you weren’t allowed to run your application’s modules and call its functions from config files. Is this really the case? And what’s the reasoning behind it?
Thanks for explanations!
Most Liked
Nicd
Just an addendum to this older post: In runtime config (config/runtime.exs), you can use your modules, as they have already been compiled. No applications are running at that point, but for example you can write config helpers like this:
Nicd
Your modules are compiled using the generated config, not the other way around. So when the config is being processed, you cannot use the modules because they have not been compiled. Otherwise there would be cyclic dependencies.
What you can do is use MFA (module, function, arguments) syntax to refer to functions that you will later run at runtime. So for example callback_function: {MyApp.MyModule, :function, [1, "foo"]} is fine.
As for the release config files that are evaluated during runtime at system startup, I think @LostKobrakai’s post covers that case. But I’m not an expert on them.
LostKobrakai
Both answers come down to: Your App depends on your configuration, so your configuration cannot depend on your app.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








