简体   繁体   中英

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. 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.

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. 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.

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 :my_app, :some_config, "some value"

Then your config/prod.exs might have something like this:

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.

This is just a guess based on my experience, but your stack trace would lead me to believe I'm wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM