Getting env variable always returns "nil"

I have this plug to validate my API’s version:

dummy_api_web/version.ex

defmodule DummyApiWeb.Version do
  import Plug.Conn

  @versions Application.get_env(:dummy_api, :versions)

  def init(opts), do: opts

  def call(conn, opts) do
    version = validate_version!(opts[:version])
    assign(conn, :version, version)
  end

  defp validate_version!(provided_version) do
    if provided_version in @versions do
       provided_version
    else
      raise "(VersionNotFound) expected any of #{inspect(@versions)}, got #{provided_version}"
    end
  end

end

And this is my config/config.exs file:

use Mix.Config

config :dummy_api,
  versions: [:v1]
...
import_config "#{Mix.env()}.exs"

But for some reason @versions always gets set to nil

You may have to force recompilation of dummy_api_web/version.ex with a mix clean since it looks like it was not recompiled after you changed your config. This article talks about some of the potential drawbacks of module attributes: https://ropig.com/blog/be-careful-when-using-elixirs-module-attributes/

1 Like

I tried:

mix deps.clean plug --build && mix deps.get && mix deps.compile && mix compile

But I’m still getting:

[error] #PID<0.423.0> running DummyApiWeb.Endpoint (connection #PID<0.422.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: POST /v1/users/signup
** (exit) an exception was raised:
    ** (Protocol.UndefinedError) protocol Enumerable not implemented for nil. This protocol is implemented for: Ecto.Adapters.SQL.Stream, Postgrex.Stream, DBConnection.PrepareStream, DBConnection.Stream, Date.Range, File.Stream, Function, GenEvent.Stream, HashDict, HashSet, IO.Stream, List, Map, MapSet, Range, Stream
        (elixir) /root/deb/elixir_1.8.1-2/lib/elixir/lib/enum.ex:1: Enumerable.impl_for!/1
        (elixir) /root/deb/elixir_1.8.1-2/lib/elixir/lib/enum.ex:166: Enumerable.member?/2
        (elixir) lib/enum.ex:1553: Enum.member?/2
        (levy_api) lib/levy_api_web/version.ex:15: LevyApiWeb.Version.validate_version!/1
        (levy_api) lib/levy_api_web/version.ex:10: LevyApiWeb.Version.call/2
        (levy_api) LevyApiWeb.Router.v1/2
        (levy_api) lib/levy_api_web/router.ex:1: LevyApiWeb.Router.__pipe_through0__/1
        (phoenix) lib/phoenix/router.ex:270: Phoenix.Router.__call__/1
        (levy_api) lib/levy_api_web/endpoint.ex:1: LevyApiWeb.Endpoint.plug_builder_call/2
        (levy_api) lib/plug/debugger.ex:122: LevyApiWeb.Endpoint."call (overridable 3)"/2
        (levy_api) lib/levy_api_web/endpoint.ex:1: LevyApiWeb.Endpoint.call/2
        (phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:33: Phoenix.Endpoint.Cowboy2Handler.init/2
        (cowboy) /mnt/c/Users/CRISTHIAN/Documents/Elixir/levy_api/deps/cowboy/src/cowboy_handler.erl:41: :cowboy_handler.execute/2
        (cowboy) /mnt/c/Users/CRISTHIAN/Documents/Elixir/dummy_api/deps/cowboy/src/cowboy_stream_h.erl:296: :cowboy_stream_h.execute/3
        (cowboy) /mnt/c/Users/CRISTHIAN/Documents/Elixir/dummy_api/deps/cowboy/src/cowboy_stream_h.erl:274: :cowboy_stream_h.request_process/3
        (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

No, not clean plug, your project has to get cleaned, mix clean will do so, alternatively you can use mix compile --force

Still the same after trying mix clean and mix compile --force.

As you sometimes use dummy_app and there is also levi_app in the stack trace, can you please create a minimal project from scratch that shows the problem and push it to GitHub or similar services?

2 Likes

Ok, so I deleted the project, cloned it and install everything from scratch. Now it works without problems.

2 Likes

Thank you all for helping out. I love this community

2 Likes