fireproofsocks

fireproofsocks

Circular dependencies during compilation

This is a bit of a theoretical question – I was cleaning up some logic in my Phoenix router.ex and I noticed that I can’t reference public functions that are defined in the router because the module isn’t yet available. When I think about it, I guess that makes sense: the plug and other macros have to run in order for the module to compile, so any functions that the module has are not yet available when the macros are running. So I put some of my helper functions into a separate file.

But that got me thinking… is it possible to have circular dependencies when compiling? E.g. module A has macros that call functions in module B, but module B has macros that call functions in module A? = Deadlock? Is that possible?

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

It is not, and the compiler will provide a handy error message if you do this by accident:

== Compilation error in file lib/a.ex ==
** (CompileError)  deadlocked waiting on module B
    lib/a.ex:2: (module)
    (stdlib 3.11.2) erl_eval.erl:680: :erl_eval.do_apply/6

== Compilation error in file lib/b.ex ==
** (CompileError)  deadlocked waiting on module A
    lib/b.ex:2: (module)
    (stdlib 3.11.2) erl_eval.erl:680: :erl_eval.do_apply/6

Compilation failed because of a deadlock between files.
The following files depended on the following modules:

  lib/a.ex => B
  lib/b.ex => A

Ensure there are no compile-time dependencies between those files and that the modules they reference exist and are correctly named

Where:

# a.ex
defmodule A do
  B.foo()
end

# b.ex
defmodule B do
  A.foo()
end

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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

Other popular topics Top

msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New
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
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement