Mpanarin

Mpanarin

Application not getting loaded

Hello everyone.

I am experiencing an issue, which I am pretty sure is caused just by me not understanding stuff. Which is usual :slight_smile:

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?

Marked As Solved

hauleth

hauleth

Using application/0 :env for compile-time-only environment values seems at least strange. Why use :env instead of the config/config.exs though?

And to answer your question more directly, as you see in documentation .app file is compiled after all Elixir files (as compile.app needs list of all modules in application), so it cannot be available during compilation. If you want to have compile-time environment then use config/config.exs.

Also Liked

Matsa59

Matsa59

I think https://hexdocs.pm/elixir/Application.html#module-the-application-callback-module could help you. I don’t spent many times on application but I think you miss mod

def application do
  [
    mod: {MyApp, []},
    extra_applications: [:logger],
    env: [
      foo: "bar"
    ]]
end

If 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

Matsa59

Gonna check this morning, is it an open source project?

Matsa59

Matsa59

Sounds like it doesn’t read value from the function application in mix.exs. If you write it in any config file it works …

Can’t find any mention of that behavior anywhere …

Edit: add code bellow

defmodule FooBar.Spam do
  @foo Application.fetch_env(:foobar, :foo)
end

Return :error, it seems the env var define in application/0 is available only in runtime.

Matsa59

Matsa59

In fact it’s a « normal » behavior. You asked for a compiled env. but application/0 sounds like to be available only in runtime.

So it doesn’t compile because you want a compiled env var that is not available.

However I don’t know if it’s a normal that the ˋapplication/0` is not available in compile time.

Share the issue please, I would like to follow that :wink:

For the moment add your env in config file it should work.

hairyhum

hairyhum

Hi,

This is somewhat a workaround, but it seems to be working for me.
I have my default configuration in the config/config.exs and adding following to the mix.exs:

def application do
  [
    ...
    env: Application.get_all_env(:my_app)
  ]
end

After I run mix compile there is my config in the ebin/my_app.app file

Because config/config.exs is loaded before the compilation, mix compile.app has access to the application config.

I’m not sure if that idiomatic, but it works.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement