My code which compiles:
defmodule MyModule do
use Plug.Builder
if System.get_env("MIX_ENV") == "prod" do
# [......]
end
end
but
defmodule MyModule do
use Plug.Builder
unless System.get_env("MIX_ENV") == "prod" do
# [......]
end
end
won’t:
== Compilation error in file lib/web/my_module.ex ==
** (FunctionClauseError) no function clause matching in String.split/3
The following arguments were given to String.split/3:
# 1
nil
# 2
"/"
# 3
[]
Attempted function clauses (showing 3 out of 3):
def split(string, %Regex{} = pattern, options) when is_binary(string)
def split(string, "", options) when is_binary(string)
def split(string, pattern, options) when is_binary(string)
(elixir 1.10.4) lib/string.ex:471: String.split/3
lib/plug/router/utils.ex:120: Plug.Router.Utils.split/1
lib/plug/static.ex:158: Plug.Static.init/1
lib/plug/builder.ex:304: Plug.Builder.init_module_plug/4
lib/plug/builder.ex:288: anonymous fn/5 in Plug.Builder.compile/3
(elixir 1.10.4) lib/enum.ex:2111: Enum."-reduce/3-lists^foldl/2-0-"/3
lib/plug/builder.ex:286: Plug.Builder.compile/3
expanding macro: Plug.Builder.__before_compile__/1
And vise versa:
# compilation error
if System.get_env("MIX_ENV") != "prod" do
# [......]
end
# compiles
unless System.get_env("MIX_ENV") != "prod" do
# [......]
end
Why?
Also, what’s a reliable way, nowadays, to determine the current enviroment from within a Phoenix project itself and an Elixir library that’ll be used in a Phoenix project? I deploy my projects via mix, manually.
update1
It can be due to the fact that inside that condition if
or unless
I have this:
@cfg Application.get_env(:my_lib, MyLib)
plug(Plug.Static,
at: @cfg[:path1],
from: @cfg[:path2]
)
and I comment this out, both if
or unless
will compile.
But why is this an issue? Previously, I used it with no compilation error.