Can't find module in prod environment but test is working

Hi,

I am a newbie in Phoenix so I hope I wont bother you with my question. I thank you in advance to show me the light :smiley:

I build an app on Phoenix, nothing special just a test to learn.

I added a library in my lib/

>ls lib/mylib/mymod.ex
lib/mylib/mymod.ex

Where I defined a MyLib.Mymod and a function foo that return bar

I wrote a test:

>ls test/mylib/mymod_test.exs
test/mylib/mymod_test.exs

where I assert MyLib.Mymod.foo() == "bar"

And the test run properly:

>mix test
...........

Finished in 0.1 seconds
11 tests, 0 failures

Now I use the module MyLib.Mymod in config/prod.exs where I call foo/0

And the result is

>MIX_ENV=prod mix phx.server
** (UndefinedFunctionError) function Mylib.Mymod.get_env/0 is undefined (module Mylib.Mymod is not available)
    Mylib.Mymod.get_env()

Mix.exs paths seems ok:

  defp elixirc_paths(:test), do: ["lib", "test/support"]
  defp elixirc_paths(_), do: ["lib"]

From the test output, assuming it’s the actual output, the “l” in Mylib needs to be capitalized.

Excuse me, I did a mistake from a copy and past from my terminal:

> mix test
...........

Finished in 0.2 seconds
11 tests, 0 failures

Randomized with seed 9483
> cat test/mylib/mymod_test.exs
defmodule Mylib.Mymodtest do
  use ExUnit.Case

  test "Existing env var SHELL" do
    assert MyLib.Mymod.foo() == "bar"
  end
end
> cat config/prod.exs
use Mix.Config

MyLib.Mymod.foo()
> MIX_ENV=prod mix compile
** (UndefinedFunctionError) function MyLib.Mymod.foo/0 is undefined (module MyLib.Mymod is not available)
    MyLib.Mymod.foo()
    (stdlib) erl_eval.erl:680: :erl_eval.do_apply/6
    (elixir) lib/code.ex:240: Code.eval_string/3
    (mix) lib/mix/config.ex:158: anonymous fn/2 in Mix.Config.__import__!/2
    (elixir) lib/enum.ex:1948: Enum."-reduce/3-lists^foldl/2-0-"/3
    (mix) lib/mix/config.ex:157: Mix.Config.__import__!/2
    (stdlib) erl_eval.erl:680: :erl_eval.do_apply/6

You can’t call modules in the various config exs files. Those config files are evaluated before any compilation happens.

2 Likes

Thanks :slight_smile: