Umbrella and Code.ensure_loaded?

38 views
Skip to first unread message

Wojtek Mach

unread,
Jul 12, 2016, 4:07:19 PM7/12/16
to elixir-lang-talk
Hi everyone,

I have an umbrella project with 2 apps: first, `ledger`, is using Ecto and the 2nd, `money`, one is mostly just a struct with some functions.

Now, the 2nd app has an "optional" Ecto support, it defines a custom Ecto.Type for that struct. The Ecto support is wrapped in a `if Code.ensure_loaded?(Ecto.Type)` because I didn't want to specify Ecto as a dependency.

When I run the tests from the root of the app everything works well.
Now, when I `cd apps/money && touch && lib/money/ecto.ex && mix test` that works too. But when I then re-compile from within the 1st app: `cd apps/ledger && mix test` my custom types isn't defined. Which makes sense, because when I compiled the 2nd app, `Ecto.Type` wasn't available at that time and some the generated .beam does not have that.

My temporary solution is to just recompile stuff from the root. Another solution is to introduce `apps/ledger/lib/money_ext.ex` and define the extension there - the downside is that I'd rather keep that stuff in the `money` app.
Finally, I could just make `money` depend on Ecto directly, but I'm wondering if there's another way. If I'd ever publish the package on Hex I wouldn't want to depend on `ecto` directly (make that optional instead) and so I'd likely end up with the similar `if Code.ensure_loaded` guard.


Thanks,
Wojtek

Michał Muskała

unread,
Jul 12, 2016, 4:20:01 PM7/12/16
to elixir-l...@googlegroups.com
Hello,

I think if you define the dependency on ecto as optional in the “money” app this will ensure ecto is available by the time that dependency is compiled.

You can do that with {:ecto, “~> 2.0”, optional: true} as the dependency declaration.

Michał.

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/06404fb8-363c-4942-af28-a96e1b2d62f9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

signature.asc

Wojtek Mach

unread,
Jul 12, 2016, 5:15:58 PM7/12/16
to elixir-lang-talk
Hey Michał, thanks!
I've actually tried that option and it works in the minimal repro production. In my main project though I was running into a separate issue (looks like something to do with postgrex perhaps) and I'm still working on a minimal repro. Anyway, thanks again!
Reply all
Reply to author
Forward
0 new messages