fix: avoid compile-time deps from schema function options (#38) - #67
Merged
Merged
Conversation
The syntax sugar for schema functions injected the full options list, unescaped, into the generated `TypeBuilder.add_field/5` call. Every option value was therefore evaluated in the module body at compile time, so a module alias in an option like `join_through:` became a compile-time dependency — one plain Ecto.Schema does not have. Forward only the options add_field actually reads (`:null`, `:enforce`, `:default`, `:values`, `:__typed_ecto_type__`, and the belongs_to-specific `:define_field`/`:foreign_key`/`:type`), mirroring the polymorphic embeds path. The forwarded values keep their original AST, so `values: @attr` still resolves in the module body, and the full original options still go to the re-emitted Ecto call untouched. Also adds the compile-time dependency regression test requested in #26, using a compiler tracer: the `join_through:` module must not be referenced at compile time, while a custom `Ecto.Type` used with `field` must (that dependency is inherent to Ecto). Closes #38 Closes #26 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
bamorim
added a commit
that referenced
this pull request
Aug 25, 2026
bamorim
added a commit
that referenced
this pull request
Aug 25, 2026
* release: prepare 0.5.0 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * docs: note additional_types + polymorphic_embed interaction in changelog Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * docs: warn that list fields' named types represent a single element A plural field name like :roles or :channels gets a named type that is the element union, not the list — call that out explicitly since the pluralized name reads as if it were the list type. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * docs: add #67 and #68 to the 0.5.0 changelog Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
many_to_many(:reviews, Review, join_through: Book)insidetyped_schemacreated a compile-time dependency onBook, while the identical schema with plainEcto.Schemadoes not (#38, reproduced in axelson/typed_ecto_schema_repro). This causes recompilation cascades in user projects.Root cause
The syntax sugar for schema functions injected the full options list, unescaped, into the generated
TypeBuilder.add_field/5call:The
typewas escaped (PR #18), butoptswas not — so every option value was evaluated in the module body at compile time, and a module alias in an option likejoin_through:resolved into a compile-time dependency. (Ecto itself avoids this by expanding such aliases under a fake function env.)Fix
Forward only the options
add_field/5actually reads —:null,:enforce,:default,:values,:__typed_ecto_type__, and thebelongs_to-specific:define_field/:foreign_key/:type— mirroring how the polymorphic embeds support already handles this. Notes:values: @attrstill resolves to the real list in the module body — theadditional_typesfeature and enum type inference keep working.Keyword.takeat runtime.Ecto.Typecase from the issue (field(:country, Country)) is untouched: that compile-time dependency is inherent to Ecto (it exists with plainEcto.Schematoo).Regression test (#26)
Adds
test/typed_ecto_schema/compile_deps_test.exs: it compiles a schema from a string with a compiler tracer attached and records every module referenced at compile time (env.function == nil, which is what makesmix xrefdraw a compile edge). It asserts thejoin_through:module is never referenced at compile time, while a customEcto.Typeused withfieldis — proving both that the tracer setup detects deps and that the Ecto-inherent case stays untouched. The test fails on master and passes with this fix.Verification with
mix xrefThrowaway project using this branch as a path dep, with two schemas —
TypedUser(typed_schema) andPlainUser(plain Ecto.Schema), both declaringmany_to_many(:reviews, XrefRepro.Review, join_through: XrefRepro.Book)in separate files.Before (master):
After (this branch):
mix test(66 passed),mix credo --strict, andmix dialyzerall pass.Closes #38
Closes #26
🤖 Generated with Claude Code