mike_bianco

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:

  1. 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?
  2. 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

danschultzer

Pow Core Team

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.

Where Next?

Popular in Questions Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&amp;query=perfume&amp;page=2, I would like to get: ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; somethi...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New

We're in Beta

About us Mission Statement