Yesterday I encountered a very mysterious behaviour.
A dependency was started in :prod
while my app was compiling in :dev
.
I use various home-made apps as dependencies, hosted at GitLab.
This is from a Exto.MixProject
I am using:
defp deps do
[
{:my_dep_x, git: System.get_env("MIX_MY_DEP_X", "git@gitlab.com:my-user-name/my_dep_x.git"), system_env: deps_env()},
{:my_dep_y, git: System.get_env("MIX_MY_DEP_Y", "git@gitlab.com:my-user-name/my_dep_y.git"), system_env: deps_env()},
{:ecto, ">= 3.5.6"},
{:ecto_sql, ">= 3.5.4"},
{:postgrex, ">= 0.15.8"},
{:jason, "~> 1.2.2"},
{:elixir_uuid, "~> 1.2.1"}
]
end
Environment variables are used to fetch private repositories.
This is from a .gitlab-ci.yml
I am using:
test:
stage: test
variables:
MIX_ENV: "test"
MIX_MY_DEP_X: "https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com/my-user-name/my_dep_x.git"
MIX_MY_DEP_Y: "https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com/my-user-name/my_dep_y.git"
I use the system_env
option to “pass down” the “mix env”.
I never use Mix.env()
, instead I use the following:
def mix_env do
case System.get_env("MIX_ENV") do
"prod" -> :prod
"test" -> :test
_default -> :dev
end
end
def deps_env do
[{"MIX_ENV", Atom.to_string(mix_env())}]
end
After refactoring my_dep_y
it is now (also) depending on my_dep_x
.
Just now I ran mix deps.clean --all
for my current project that depends on both x and y.
Then I noticed something I can’t explain.
$ mix deps.get
Dependencies have diverged:
* my_dep_x (git@gitlab.com:my-user-name/my_dep_x.git)
the dependency my_dep_x in mix.exs is overriding a child dependency:
> In mix.exs:
{:my_dep_x, [system_env: [{"MIX_ENV", "dev"}], env: :prod, git: "git@gitlab.com:my-user-name/my_dep_x.git"]}
> In deps/my_dep_y/mix.exs:
{:my_dep_x, [env: :prod, git: "git@gitlab.com:my-user-name/my_dep_x.git"]}
Ensure they match or specify one of the above in your deps and set "override: true"
** (Mix) Can't continue due to errors on dependencies
Notice env: :prod
.
This probably explains why a dependency was started in :prod
while my app was compiling in :dev
.
I will try to override this magical setting manually.
Maybe someone wants to look into this.
I wouldn’t want my apps to suddenly start compiling/running in prod mode.