BartOtten
How to switch between local or Hex dependency?
For my public libs I have created demo apps. During development the demo app should depend on the development version of the library; so I use path: ../my_lib. However, when the demo app is cloned by a user it should depend on the Hex version instead.
Currently I set an environment flag which toggles the location of the dependency, but it keeps nagging in my head: this should be easier. Maybe detecting the existence of a file not committed to Git…?
Anyone having a better idea?
Ps. Detecting :dev runtime environment is not gonna cut it, as the user will probably run the demo in :dev too.
Most Liked
Nicodemus
I do this in the demo for one of my projects, in “mix.exs”:
defp deps do
[
{:phoenix, "~> 1.7.14"},
...,
my_lib_dep()
]
end
defp my_lib_dep() do
if File.exists?(Path.join(__DIR__, "../my_lib/mix.exs")) do
{:my_lib, path: "../my_lib"}
else
{:my_lib, "~> 0.1.0"}
end
end
Or you can append an entire list to the end if you have multiple deps. The nice thing about “mix.exs” is that it’s code, and is executed every time you run mix.
BartOtten
Current implementation is like this. I did commit an .envrc file which sets the path. It seems checking path existence is the only ‘fully native’ solution. Combining it with env override and a warning might hit the sweet spot.
defp routex_dep() do
if path = System.get_env("ROUTEX_PATH") do
IO.puts(">> !! USING LOCAL ROUTEX PATH !! <<")
{:routex, path: path}
else
{:routex, ">= 0.0.0"}
end
end
zachdaniel
Something that might be useful is that Mix.env() returns :prod when your package is compiled for use in a user’s application.
So
@dependency if Mix.env() == :prod do
{:package, "~> ..."}
else
{:package, path: "..."}
end
sodapopcan
@BartOtten already mentioned the env variable approach which I do like better. I’m certainly aware mix.exs is just code which I thought was implied by my saying I already considered @Nicodemus’s approach (well, same goes for checking an env var, really).
BartOtten
So far my top pick: Demo code in same repo (excluded from Hex package)
Pre:
- use demo app for integration test
- (major) 1 clone and easy hacking in both example as lib
- no split commits; no sync issues
——-
Also on the table: dotfile + env var
- Presence check a dotfile which is .gitignored. (Default). If available, use
path: …/my_lib - If env set and path exists, use path of env var. (Override)
- If env set and false: use Hex (Override)
Pre over only env variable:
- no need for an env-setter.
- no clobbering ~.profile etc.
- shell and terminal agnostic.
- Easy (un)set:
touch .local/rm .local.
Pre over only path check:
- can set set non-default path.
- can force Hex.
no env, no file == no possible path execution








