Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fields still cause compile-time dependencies #38

Open
axelson opened this issue Nov 29, 2022 · 2 comments
Open

Fields still cause compile-time dependencies #38

axelson opened this issue Nov 29, 2022 · 2 comments

Comments

@axelson
Copy link

axelson commented Nov 29, 2022

Modules referenced as fields still create compile-time dependencies. e.g. this is an issue:

defmodule Person do
  use TypedEctoSchema

  typed_schema "people" do
    field(:country, Country, enforce: true, null: false)
  end
end

While this is not an issue:

defmodule Person do
  use TypedEctoSchema

  typed_schema "people" do
    belongs_to(:company, Company)
  end
end

I've created a simple phoenix project that serves as a reproduction of this issue: https://github.com/axelson/typed_struct_compile_repro

In it I expect this command to not show any compile-time dependencies, but instead it shows one compile-time dependency:

mix xref graph --source lib/typed_demo/person.ex
lib/typed_demo/person.ex
├── lib/typed_demo/company.ex
└── lib/typed_demo/country.ex (compile)

I believe this is a subset that was not fixed with #18 (original issue filed as #6)

@dvic
Copy link
Collaborator

dvic commented Nov 29, 2022

The second example is using a belongs_to while the first one is using field :)

If you change it to belongs_to in the first example, you'll see that the compile-time dependency disappears :) In case Country was an Ecto.Type, then you would have to use field, but that compile-time dependency is actually introduced by Ecto, so there's not much we can do.

@axelson
Copy link
Author

axelson commented Jul 19, 2024

Guess I never responded to this 🫣
What you say makes a lot of sense, and we definitely can't remove a compile-dep that Ecto is adding.

Although what brings me here today is I found another case where typed_ecto_schema adds a compile-dep where Ecto doesn't have it, the join_through of a many_to_many:

defmodule Many.User do
  import TypedEctoSchema

  typed_schema "users" do
    field :name, :string
    field :email, :string

    many_to_many :book_reviews, Many.BookReview,
      join_through: Many.Book,
      unique: true,
      on_replace: :delete
  end
end

I tried spelunking through the code but I couldn't find anything that stood out to me.

Edit: I've created a reproduction repo for this here: https://github.com/axelson/typed_ecto_schema_repro

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

No branches or pull requests

2 participants