bortzmeyer

bortzmeyer

I have a program which can provide different services, depending on its run-time configuration (through a configuration file or command-line options). Some of the services, but not all, depend on external libraries, such as HTTPoison, so I add in my mix.exs:

      {:httpoison, "~> 1.8"} 

But I would like to avoid this dependency (HTTPoison brings a lot of other libraries) if not necessary. How to make it conditional, and allow the program to know if it has been included or not, so it can produce a proper error message if the user tries to activate the service, without having the library?

I’ve read this StackOverflow post, which seems to be about a different problem, and this Elixir Forum discussion which was inconclusive.

Showing Posts 1 to 10

mayel

mayel

There’s an optional option, though it’s meant more for dependencies within libraries, so the app using the library can choose to include it or not:

{:httpoison, "~> 1.8", optional: true} 

But maybe something like this:

def dep_enabled?(env_key), do: not is_nil(System.get_env(env_key))
def deps() do 
[{:httpoison, "~> 1.8", optional: dep_enabled?("POISON_IS_GOOD_FOR_YOU")} 
...
mayel

mayel

Actually, re-reading the docs it looks like in your case (if this is a top level app) it would always be included, so maybe a similar approach but using only to specify [:dev, :test] if the dep is disabled, and [:dev, :test, :prod] if it should be enabled?

moogle19

moogle19

I think target is what you want.
In your mix.exs you would use it like:

{:httpoison, "~> 1.8", targets: :your_target_name}

and during build just set MIX_TARGET=your_target_name to build your application with httpoison.

I think during runtime you can check if httpoison was compiled via:

:application.info[:loaded]

It should appear in the list of loaded applications. Not sure if there is a more elegant way.

bortzmeyer

bortzmeyer OP

Unfortunately, the code cannot compile (“HTTPoison.Response.struct/0 is undefined”) so I also need a compile-time condition.

moogle19

moogle19

Yes, of course. I forgot to mention that.

You can do something like this:

defmodule SomeModule do
  if Mix.target() == :your_target_no do
    def some_function do
      HTTPoison.get("forum.elixirforum.com")
    end
  else
    def some_function do
      # Do something without HTTPoison
    end
  end
end

Not sure if that suits your needs and how complex the httpoison integration is in your app.

bortzmeyer

bortzmeyer OP

[Sorry for the delay to reply.]

Unfortunately, it does not help since it is at compile-time that Elixir needs to know things such as data structures (see the error message I gave). Function calls may be handled by your trick but not pattern matching with types. I’m afraid there is no easy solution.

mayel

mayel

Are you wrapping the functions that have pattern matching with conditions checking for the target like in @moogle19’s example? This would be applied at compile time, vs at runtime if your conditions are defined within the function.

bortzmeyer

bortzmeyer OP

For the record, here is a complete example, showing it doesn’t work.

mix.exs:

defmodule ConditionalDependencies.MixProject do
  use Mix.Project

  def project do
    [
      app: :conditional_dependencies,
      version: "0.1.0",
      elixir: "~> 1.9",
      start_permanent: Mix.env() == :prod,
      deps: deps()
    ]
  end

  # Run "mix help deps" to learn about dependencies.
  defp deps do
    [
      {:httpoison, "~> 1.8", targets: :http}
    ]
  end
end

conditional_dependencies.ex:

  def hello do
    # By default, Mix.target() == host
    if Mix.target() == :http do
      HTTPoison.start()
      IO.puts("HTTPoison started")
      result = HTTPoison.get("https://forum.elixirforum.com/t/conditional-dependencies-in-mix-exs/48468")
      case result do
	{:ok, %HTTPoison.Response{status_code: 200} = _data} ->
	  IO.puts("It worked")
	_other ->
	  IO.puts("HTTP failed")
      end
    else
      IO.puts("No HTTPoison")
    end
  end
end

ConditionalDependencies.hello

And the results:

% MIX_TARGET=http mix run conditional_dependencies.ex
HTTPoison started
It worked
% mix run conditional_dependencies.ex                
** (CompileError) conditional_dependencies.ex:9: HTTPoison.Response.__struct__/0 is undefined, cannot expand struct HTTPoison.Response
    (stdlib) lists.erl:1354: :lists.mapfoldl/3
    (stdlib) lists.erl:1355: :lists.mapfoldl/3
    (elixir) expanding macro: Kernel.if/2

I also note that mix deps.get (without target) retrieves HTTPoison and its dependencies.

al2o3cr

al2o3cr

Calling functions from the Mix module inside of functions at runtime can be tricky - for instance it will fail loudly if running in a release.

In this case, the compiler still has to compile hello’s contents even if the target isn’t set. That gets the error that you’re seeing. An alternative way is to prevent the compiler from seeing the code at all:

if Mix.target() == :http do
  def hello do
    HTTPoison.start()
    IO.puts("HTTPoison started")
    result = HTTPoison.get("https://forum.elixirforum.com/t/conditional-dependencies-in-mix-exs/48468")
    case result do
      {:ok, %HTTPoison.Response{status_code: 200} = _data} ->
        IO.puts("It worked")

      _other ->
        IO.puts("HTTP failed")
    end
  end
else
  def hello do
    IO.puts("No HTTPoison")
  end
end
mayel

mayel

Ah I hadn’t seen @al2o3cr and was gonna suggest something similar:

defmodule ConditionalDependencies do

  if Code.ensure_loaded?(HTTPoison) do
    def hello do
      HTTPoison.start()
      IO.puts("HTTPoison started")
      case HTTPoison.get("https://forum.elixirforum.com/t/conditional-dependencies-in-mix-exs/48468") do
        {:ok, %HTTPoison.Response{status_code: 200} = _data} ->
          IO.puts("It worked")
        _other ->
          IO.puts("HTTP failed")
      end
    end
  else
    def hello do
      IO.puts("No HTTPoison")
    end
  end

end

ConditionalDependencies.hello

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews