mjlorenzo

mjlorenzo

Module Does Not Recompile When Referenced Module Changes

Hi folks, got a question about how Elixir determines when to recompile a module. The documentation says a module will recompile when one of its dependencies changes, but that doesn’t seem to be the case here and I’m not sure why. Here’s the scenario:

I have a module that defines a schema for the library I’m building:

defmodule Schema do
  import Library.Schema.Notation

  schema :name do
    # macros and schema definition...
  end
end

When this compiles, it exposes a callback __schema__ to be consumed in another module. While Library.Client actually consumes the schema, Testbed is the intended product module:

defmodule Testbed do
  use Library.Client, schema: Schema
  #. (Library.Client.__using__() actually calls Schema.__schema__())
  # ... callbacks and implementation
end

I figured because Testbed references Schema, then it should know to recompile if Schema recompiles. I’m consistently seeing however that changes made to Schema do cause itself to recompile, but Testbed still doesn’t and I have to manually recompile it to reflect the changes in Schema. This ring a bell for anyone?

Thanks,
Mike

Most Liked

sodapopcan

sodapopcan

With an oversimplified mix project, I am unable to reproduce this:

defmodule Recompile.Schema do
  defmacro __schema__() do
    quote do
      "I do schema stuff!"
    end
  end
end

defmodule Recompile.Notation do
  defmacro __using__([schema: schema]) do
    quote do
      require unquote(schema)
      unquote(schema).__schema__()
    end
  end
end

defmodule Recompile.TestBed do
  use Recompile.Notation, schema: Recompile.Schema
end
$ mix xref graph --label compile --sink lib/recompile/schema.ex
lib/recompile/test_bed.ex
└── lib/recompile/schema.ex (compile)

and when I change Schema:

$ mix compile --verbose
Compiling 2 files (.ex)
Compiled lib/recompile/schema.ex
Compiled lib/recompile/test_bed.ex

Could you possibly share more code and your Elixir version? Is this in a mix project?

sodapopcan

sodapopcan

Yes, that is correct behaviour in this situation because it’s inside a function which makes it a runtime dependency.

sodapopcan

sodapopcan

Mostly, yes. Nothing outside of the returned quoted expression will be injected into Testbed so once that module is compiled, all that stuff is gone. So in this case it’s not that it isn’t interpreting Schema as anything other than the atom it’s that Schema isn’t even present in the compiled code! Again it’s hard to see without more context but there doesn’t seem to be anything about that code snippet that suggests you need Macro.expand over requireing or importing within a quote block which of course you want anyway to create the dependency. It depends what you’re trying to accomplish, of course.

I’ve also never actually seen ensure_compiled! used inside a module before. I’m not a master of macros, though, so maybe there is a good reason, but AFAIK, ensure_compiled! is essentially require for use outside of a defmodule. Don’t quote me on that, though :slight_smile:

sodapopcan

sodapopcan

import yes, require no. require literally just ensures the module is compiled (namely that its macros have been expanded) before the module it’s called in, ie, required for the current module to work :slight_smile: It’s needed if there are macros in the dependency otherwise simply calling a function from it will suffice. For example:

defmodule Foo do
  Bar.a_regular_ol_function_that_is_called_in_the_module_body_for_some_reason()
end

This will make Bar a dependency of Foo. It doesn’t need a require if Bar isn’t calling a macro.

(sorry for all the edits if you noticed that, my dog is bugging me to go outside but wanted to finish the response, lol)

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
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
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement