[英]Elixir - setting MIX_ENV on a Mac yields FunctionClauseError
I'm having trouble getting my Elixir apps to read the MIX_ENV
variable from the local environment on my Mac. 我无法让Elixir应用程序从Mac上的本地环境读取
MIX_ENV
变量。 For example, running the command 例如,运行命令
$ MIX_ENV=prod iex -S mix
throws the following error: 引发以下错误:
** (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 4 out of 4):
def split(string, %Regex{} = pattern, options) when is_binary(string)
def split(string, "", options) when is_binary(string)
def split(string, pattern, []) when is_tuple(pattern) or is_binary(string)
def split(string, pattern, options) when is_binary(string)
(elixir) lib/string.ex:407: String.split/3
(stdlib) erl_eval.erl:680: :erl_eval.do_apply/6
(stdlib) erl_eval.erl:888: :erl_eval.expr_list/6
(stdlib) erl_eval.erl:240: :erl_eval.expr/5
(stdlib) erl_eval.erl:232: :erl_eval.expr/5
(stdlib) erl_eval.erl:888: :erl_eval.expr_list/6
(stdlib) erl_eval.erl:411: :erl_eval.expr/5
(stdlib) erl_eval.erl:126: :erl_eval.exprs/5
This also occurs if I set the MIX_ENV
in a separate step. 如果我在单独的步骤中设置
MIX_ENV
,也会发生这种情况。
From the documentation at https://elixir-lang.org/getting-started/mix-otp/introduction-to-mix.html , it looks to me like I'm doing this correctly, but it would seem that I'm not. 根据https://elixir-lang.org/getting-started/mix-otp/introduction-to-mix.html上的文档,在我看来我做得正确,但似乎我在不。 Is there a different way that I need to set this?
我需要设置其他方式吗?
As noted, I'm working on a Mac, Mojave 10.14.6, and my Elixir version is 1.8.1. 如前所述,我正在Mac上运行Mojave 10.14.6,而我的Elixir版本是1.8.1。
I would check your configuration. 我会检查您的配置。 For me a lot of times this comes from prod expecting an environment variable to be set.
对我而言,很多时候这是由于期望设置环境变量而产生的。
My guess is you have some code that looks something like this: 我的猜测是您有一些看起来像这样的代码:
:my_app
|> Application.get_env(:some_config)
|> String.split(" ")
|> do_something_else()
Then in your config/config.exs
or config/dev.exs
you probably have something like this: 然后,在您的
config/config.exs
或config/dev.exs
您可能会有类似以下内容:
config :my_app, :some_config, "some value"
Then your config/prod.exs
might have something like this: 然后,您的
config/prod.exs
可能具有以下内容:
config :my_app, :some_config, System.get_env("MY_ENV_VAR")
If MY_ENV_VAR
is not set, but you run your app in prod (for example doing MIX_ENV=prod iex -S mix
), Application.get_env(:my_app, :some_config)
will return nil
, which you would then be trying to split like it's a string. 如果未设置
MY_ENV_VAR
,但是您在产品中运行您的应用程序(例如,执行MIX_ENV=prod iex -S mix
),则Application.get_env(:my_app, :some_config)
将返回nil
,然后您将尝试像这样拆分一个字符串。
This is just a guess based on my experience, but your stack trace would lead me to believe I'm wrong. 这只是基于我的经验的猜测,但是您的堆栈跟踪会使我相信我错了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.