pxp9
1
Hello Elixir Folks,
I will try to explain what I want to archive first and then tell my problem.
The desired behavior is have req
as an optional dependency and start it only if the module is loaded by the user of my library.
so my mix.exs
looks like this.
{:req, "~> 0;5", optional: true}
and then my application does something like this.
if Code.loaded?(Req.Application) do
_result = Application.ensure_started(:req)
end |> dbg()
and this is the error I am getting
mix lazy_doc
** (UndefinedFunctionError) function Req.Application.start/2 is undefined (module Req.Application is not available)
Req.Application.start("", "")
lib/mix/tasks/lazy_doc.ex:26: Mix.Tasks.LazyDoc.run/1
(mix 1.18.3) lib/mix/task.ex:495: anonymous fn/3 in Mix.Task.run_task/5
(mix 1.18.3) lib/mix/cli.ex:107: Mix.CLI.run_task/2
/nix/store/1bxb1apx89k1jr2qnvha0d5zc33qhnkg-elixir-1.18.3/bin/mix:2: (file)
The application is a mix task.
The weird part is when I build the application as a binary with extra_applications [:req, ...]
, and this way it works.
What am I doing wrong ?
Thank you in advance.
1 Like
Where does this code reside? I‘ve also always used Code.endure_loaded?
not sure how that differs.
1 Like
pxp9
3
You can check this PR, it is where I implement all these things.
kip
4
From the docs:
This function doesn’t attempt to load the module. For such behavior,
ensure_loaded?/1 can be used.
I recommend using Code.ensure_loaded?
(Req.Application) as @LostKobrakai suggests.
The differences in a release is that all modules are loaded at startup (by default) whereas in development they are loaded on demand.
1 Like
pxp9
5
I think I have a different issue, This is useful for sure, but I think the issue is other because I tried your solution and did not work.
The main issue is why Req.Application
module is not found if I have installed as an optional dependency.
pxp9
6
Okay , I found the issue, for the record.
I have installed lazy_doc
as archive before started testing with esbuild
and it was using that lazy_doc
so it was not working any of my changes. 
Thank you for helping me and suggesting better code.
@kip and @LostKobrakai
1 Like