Mpanarin
Hello everyone.
I am experiencing an issue, which I am pretty sure is caused just by me not understanding stuff. Which is usual ![]()
I am currently writing a small package. Its only purpose is providing a few helper mix tasks. I want them to be configurable by the user through his Config but provide some defaults as well.
The issue is that even though I specified :env: key in the &application/0 in mix.exs the defaults are still missing and all of my Application.compile_env calls are failing obviously.
I traced the issue to my application not getting loaded. But why? I don’t understand why the hell application that I am trying to compile is not getting loaded?
Elixir version is 1.10.3 compiled with erlang 23.
important contents of the mix.exs:
defmodule FooBar.MixProject do
use Mix.Project
def project do
[
app: :foobar,
version: "0.1.0",
elixir: "~> 1.10"
]
end
def application do
[
extra_applications: [:logger],
env: [
foo: "bar"
]
]
end
end
somewhere in the project:
defmodule FooBar.Spam do
@foo Application.compile_env!(:foobar, :foo)
end
and when I run mix compile I get an error:
** (ArgumentError) could not fetch application environment :foo for application :foobar because the application was not loaded/started. If your application depends on :foobar at runtime, make sure to load/start it or list it under :extra_applications in your mix.exs file
So, the issue is pretty clear - application is not loaded. And if I load it manually when standing on pry it works.
My question is why it is not loaded by default? If I should load it myself, where is the best place to do so?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Marked As Solved- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
hauleth
Using
application/0:envfor compile-time-only environment values seems at least strange. Why use:envinstead of theconfig/config.exsthough?And to answer your question more directly, as you see in documentation
.appfile is compiled after all Elixir files (ascompile.appneeds list of all modules in application), so it cannot be available during compilation. If you want to have compile-time environment then useconfig/config.exs.Also Liked
Matsa59
I think Application — Elixir v1.20.2 could help you. I don’t spent many times on application but I think you miss
modIf someone could confirm (I don’t have my computer :/)
Edit : after re-reading I feel like I’m wrong and don’t reply to your problem
Matsa59
Gonna check this morning, is it an open source project?
Matsa59
Sounds like it doesn’t read value from the function
applicationinmix.exs. If you write it in any config file it works …Can’t find any mention of that behavior anywhere …
Edit: add code bellow
Return
:error, it seems the env var define inapplication/0is available only in runtime.Last Post!
hairyhum
Hi,
This is somewhat a workaround, but it seems to be working for me.
I have my default configuration in the
config/config.exsand adding following to themix.exs:After I run
mix compilethere is my config in theebin/my_app.appfileBecause
config/config.exsis loaded before the compilation,mix compile.apphas access to the application config.I’m not sure if that idiomatic, but it works.