When dependency is optional it is only added to optional_applications. How to do it when we want it to start? How to add it to applications too?
Hey @RareKoala welcome! Can you elaborate a bit on your situation? Dependencies in mix.exs
that are optional: true
are really only relevant for libraries, are you writing a library or using one?
Thank you @benwilson512! I am writing a library. I depend on Ecto and when it is dependency it is put into applications and is auto started. Now i am wanting to only optionally depend on it but it is not added to application. This do not matter?
Just a blind shot in the dark, it’s IMO best to have the core of your library and a separate library building on top of it with Ecto bindings e.g. blurp
and blurp-ecto
.
Can you show your mix.exs
file for your library?
If a dependency (Ecto) is optional, then your library functions without it, and therefore Ecto is not included in your application. However, if the user of your library also uses Ecto, Mix will recompile your library so it includes Ecto accordingly.
defmodule Proj do
use Mix.Project
def project do
[
app: :proj,
version: "0.1.0",
elixir: "~> 1.14",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger],
optional_applications: [:ecto]
]
end
# Run "mix help deps" to learn about dependencies.
defp deps do
[
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
{:ecto, "~> 3.10", optional: true}
]
end
end
Not just this but more general, I want to know how to start optional dependency before current application. I can tell mix.exs that it is optional by optional_applications, but how to start? I want to build optional OTP application and OTP application need to start yes?
@dimitarvp yes but it feels like it is work around?
Thank you @josevalim! I was wrong earlier, what I want to write is OTP application and not library. Yes, you are correct. But how will users optionally use my application? It is okey for me to make them start manually but I am just wondering if possible to automatically.
Mix should take care of everything. If they depend on your optional dependency, both your dependency and the optional one are started.