mike_bianco
Inspecting dependency package version at runtime
Hello!
New to Elixir & Phoenix. Really incredible work here—thanks for all of the amazing efforts to make an amazing language!
I’m coming from the Ruby & Rails community. Right now, I’m working on upgrading the geoip package to support a more recent version of poison to eliminate a dependency conflict.
In ruby-land, many times you’ll check the version of dependency and modify the package logic to avoid forcing users to upgrade another package to use the latest version of a different package. For example:
if SomeGemName::VERSION > 3.0
do_it_this_way
else
do_it_that_way
end
Here’s the questions I can’t seem to find an answer to:
- I haven’t seen this pattern in the (limited) elixir code I’ve read—is this something that isn’t done in the Elixir community? What’s the best practice here?
- I can’t figure out how to inspect a package version at runtime.
Poison.Mixfile.project[:version]doesn’t seem to be available during runtime and I can’t find any other interfaces to inspect a package version.
Really appreciate everyone’s help here!
Most Liked Responses
danschultzer
I prefer to either force developers to update their dependencies, or find alternative ways to make the code work independently of the dependency.
In the case of poison requirement, I would guess a better solution would be to just relax requirement in mix.exs, or make it so geoip doesn’t care whether poison is there, and instead you can switch out the json decoder the same way as in Phoenix and e.g. use jason instead.
If I got no other options, then I would do the following (this is from Pow):
@spec dependency_vsn_match?(atom(), binary()) :: boolean()
def dependency_vsn_match?(dep, req) do
case :application.get_key(dep, :vsn) do
{:ok, actual} ->
actual
|> List.to_string()
|> Version.match?(req)
_any ->
false
end
end
I can then check the dependency version requirement like this:
if Pow.dependency_vsn_match?(:ecto, "< 3.0.0"), do: Mix.Ecto, else: Mix.EctoSQL
I use the above only in mix tasks or at compile time, since the dependency version is fixed at compile. I wouldn’t do this at runtime.









