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 Application — Elixir v1.20.2 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.

Last Post!

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

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
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
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement