Code.LoadError import_config“#{Mix.env}.exs”
Code.LoadError on import_config "#{Mix.env}.exs"
我将其导入 config.exs
文件:
use Mix.Config
import_config "#{Mix.env}.exs"
或
import_config "#{Mix.env()}.exs"
当我尝试 运行 mix test
时,它在抱怨:
mix test
** (Code.LoadError) could not load /Users/romenigld/workspace/elixir/ebooks/issues/config/test.exs
(elixir) lib/code.ex:1147: Code.find_file/2
(elixir) lib/code.ex:706: Code.eval_file/2
(mix) lib/mix/config.ex:187: anonymous fn/2 in Mix.Config.__import__!/2
(elixir) lib/enum.ex:1925: Enum."-reduce/3-lists^foldl/2-0-"/3
(mix) lib/mix/config.ex:186: Mix.Config.__import__!/2
(stdlib) erl_eval.erl:680: :erl_eval.do_apply/6
(elixir) lib/code.ex:232: Code.eval_string/3
这是一个错误还是我需要做一些不同的事情?
import_config/1
的工作原理
来自 Mix.Config#import_config/1:
Imports configuration from the given file or files.
[...]
If path_or_wildcard is not a wildcard but a path to a single file, then that file is imported; in case the file doesn't exist, an error is raised.
If path/wildcard is a relative path/wildcard, it will be expanded relatively to the directory the current configuration file is in.
Mix.env/0
的工作原理
来自 Mix#env/0:
Returns the Mix environment.
当您的应用程序启动时,它将从环境变量 MIX_ENV
中读取值,如果未设置环境变量,则设置 :dev
。 Combare implementation.
调用 mix test
时 运行 的任务 tells Mix to default to the :test
environment through the @preferred_cli_env
attribute。
合并 import_config/1
和 Mix.env/1
一行代码相当于
import_config "#{Mix.env()}.exs"
会在编译时被求值(第一次运行宁mix test
时)到
import_config "test.exs"
由于路径是 relative 它将在包含调用 import_config
的文件所在的同一目录中查找文件 test.exs
.
在你的情况下是 /Users/romenigld/workspace/elixir/ebooks/issues/config/
所以你必须在 /Users/romenigld/workspace/elixir/ebooks/issues/config/test.exs
中创建一个有效的配置文件并且对于所有其他环境你的应用程序应该 运行 在(可能 dev
和 prod
).
您可以通过在调用 import_config
:
之前检查环境来绕过为所有环境创建配置文件
unless Mix.env() == :prod do
import_config("#{Mix.env()}.exs")
end
我将其导入 config.exs
文件:
use Mix.Config
import_config "#{Mix.env}.exs"
或
import_config "#{Mix.env()}.exs"
当我尝试 运行 mix test
时,它在抱怨:
mix test
** (Code.LoadError) could not load /Users/romenigld/workspace/elixir/ebooks/issues/config/test.exs
(elixir) lib/code.ex:1147: Code.find_file/2
(elixir) lib/code.ex:706: Code.eval_file/2
(mix) lib/mix/config.ex:187: anonymous fn/2 in Mix.Config.__import__!/2
(elixir) lib/enum.ex:1925: Enum."-reduce/3-lists^foldl/2-0-"/3
(mix) lib/mix/config.ex:186: Mix.Config.__import__!/2
(stdlib) erl_eval.erl:680: :erl_eval.do_apply/6
(elixir) lib/code.ex:232: Code.eval_string/3
这是一个错误还是我需要做一些不同的事情?
import_config/1
的工作原理
来自 Mix.Config#import_config/1:
Imports configuration from the given file or files.
[...]
If path_or_wildcard is not a wildcard but a path to a single file, then that file is imported; in case the file doesn't exist, an error is raised.
If path/wildcard is a relative path/wildcard, it will be expanded relatively to the directory the current configuration file is in.
Mix.env/0
的工作原理
来自 Mix#env/0:
Returns the Mix environment.
当您的应用程序启动时,它将从环境变量 MIX_ENV
中读取值,如果未设置环境变量,则设置 :dev
。 Combare implementation.
调用 mix test
时 运行 的任务 tells Mix to default to the :test
environment through the @preferred_cli_env
attribute。
合并 import_config/1
和 Mix.env/1
一行代码相当于
import_config "#{Mix.env()}.exs"
会在编译时被求值(第一次运行宁mix test
时)到
import_config "test.exs"
由于路径是 relative 它将在包含调用 import_config
的文件所在的同一目录中查找文件 test.exs
.
在你的情况下是 /Users/romenigld/workspace/elixir/ebooks/issues/config/
所以你必须在 /Users/romenigld/workspace/elixir/ebooks/issues/config/test.exs
中创建一个有效的配置文件并且对于所有其他环境你的应用程序应该 运行 在(可能 dev
和 prod
).
您可以通过在调用 import_config
:
unless Mix.env() == :prod do
import_config("#{Mix.env()}.exs")
end