Skip to content

avoid compile-time dependency on modules named in derive - #4779

Merged
josevalim merged 1 commit into
elixir-ecto:masterfrom
woylie:avoid-compile-dependency-through-derive
Aug 9, 2026
Merged

avoid compile-time dependency on modules named in derive#4779
josevalim merged 1 commit into
elixir-ecto:masterfrom
woylie:avoid-compile-dependency-through-derive

Conversation

@woylie

@woylie woylie commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

If the @derive attribute is read at compile time, a compile dependency is added to any module that is named in the @derive options. This PR changes the order of the if conditions, so that @derive is only read if the schema has redacted fields. That should avoid unnecessary compile dependencies at least some of the time.

Example:

defmodule Repro.Post do
  use Ecto.Schema
  alias Repro.Other

  @derive {Repro.MyProtocol, some_option: {Other, :something}}
  
  schema "posts" do
    field :name, :string
  end
end

Before the change:

$ mix xref trace lib/post.ex
lib/post.ex:5: alias Repro.Other (runtime)
lib/post.ex:7: require Repro.MyProtocol (export)
lib/post.ex:7: struct Repro.Post (export)
lib/post.ex:7: call Repro.MyProtocol.behaviour_info/1 (runtime)
lib/post.ex:7: call Repro.MyProtocol.Any.__deriving__/3 (compile)
@derive:5: alias Repro.MyProtocol (compile)
@derive:5: alias Repro.Other (compile)

After the change:

$ mix xref trace lib/post.ex
lib/post.ex:5: alias Repro.Other (runtime)
lib/post.ex:7: require Repro.MyProtocol (export)
lib/post.ex:7: struct Repro.Post (export)
lib/post.ex:7: call Repro.MyProtocol.behaviour_info/1 (runtime)
lib/post.ex:7: call Repro.MyProtocol.Any.__deriving__/3 (compile)

This was observed with the ecto_type option and the Flop.Schema protocol: woylie/flop#583.

@josevalim
josevalim merged commit a33eed3 into elixir-ecto:master Aug 9, 2026
8 checks passed
@josevalim

Copy link
Copy Markdown
Member

Great find! 💚 💙 💜 💛 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants