Skip to content

Add --min-transitive-dependencies to xref graph - #16196

Open
MrYawe wants to merge 1 commit into
elixir-lang:mainfrom
MrYawe:xref-stats-fail-above
Open

MrYawe wants to merge 1 commit into
elixir-lang:mainfrom
MrYawe:xref-stats-fail-above

Conversation

@MrYawe

@MrYawe MrYawe commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

This adds --min-transitive-dependencies to mix xref graph --label compile-connected, as discussed below. The first version of this pull request changed --fail-above for stats; that part is gone.

Today a compile-time dependency is compile-connected when the file it points to has at least one dependency. The new option raises that minimum, counting transitive dependencies, so compile-time dependencies on files with only a few dependencies can be left out:

mix xref graph --label compile-connected --min-transitive-dependencies 6 --fail-above 0

The tests use these files:

# lib/a.ex
defmodule A do
  def a, do: :ok
  B.b2()                        # a → b  (compile: call in module body)
end

# lib/b.ex
defmodule B do
  def b1, do: A.a() == C.c()    # b → a, b → c  (runtime: inside a function)
  def b2, do: :ok
  :e.e()                        # b → e  (compile: call in module body)
end

# lib/c.ex
defmodule C do
  def c, do: :ok
  :d.d()                        # c → d  (compile)
end

# lib/d.ex
defmodule :d do
  def d, do: :ok
  def e, do: :e.e()             # d → e  (runtime)
end

# lib/e.ex
defmodule :e do
  def e, do: :ok                # no dependencies
end

b has 4 transitive dependencies (a, c, d and e) and d has 1 (e), so:

$ mix xref graph --label compile-connected
lib/a.ex
└── lib/b.ex (compile)
lib/c.ex
└── lib/d.ex (compile)

$ mix xref graph --label compile-connected --min-transitive-dependencies 2
lib/a.ex
└── lib/b.ex (compile)

$ mix xref graph --label compile-connected --min-transitive-dependencies 2 --fail-above 0
lib/a.ex
└── lib/b.ex (compile)
** (Mix) Too many references (found: 1, permitted: 0)

$ mix xref graph --label compile-connected --min-transitive-dependencies 5 --fail-above 0

Some notes:

  • the dependencies are counted the same way as in the stats and in mix xref explain: all files reachable from the file, with any label. b has 3 direct dependencies and is still listed with a minimum of 4
  • the option requires --label compile-connected and a value greater than 0, and raises otherwise
  • it applies wherever the compile-connected label does, so --format stats --label compile-connected also uses it for its lists. Without the option nothing changes
  • it does not apply to --format cycles, where compile-connected already means compile
  • to keep it cheap, the transitive dependencies are only computed for files that have compile-time dependents. In stats the filter is now built once instead of once per file

@MrYawe
MrYawe marked this pull request as ready for review November 24, 2026 10:36
@josevalim

Copy link
Copy Markdown
Member

Thanks @MrYawe! I am wondering if we can't already emit something similar by using --fail-above on mix xref graph --label compile-connected or similar?

@MrYawe

MrYawe commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@josevalim
Yes, that works for the failure itself. The issue for us is that it counts every compile-connected dependency, and most of ours are fine.

Concrete example: we have a base schema module that all our Ecto schemas use (use MyApp.Schema), 495 files. It only depends on a small clock module for the timestamps. So that's 495 compile-connected dependencies, but in practice they never trigger recompilation. In total we have 1,689 of those, all on files with 5 transitive dependencies or less.

So --fail-above 1689 would fail every time someone adds a schema. And the day the schema module starts depending on half of the app, the count is still 495 and CI stays green, which is exactly the case we want to catch.

What I really need is only the threshold. So maybe simpler: I drop the stats part, and the option applies to the label:

mix xref graph --label compile-connected --min-transitive-dependencies 6 --fail-above 0

compile-connected already means "compile dependency on a file with at least 1 dependency", so this would just make that 1 configurable. WDYT?

@josevalim

Copy link
Copy Markdown
Member

Yes, the smaller one is better. But also, quick question, couldn't you use --exclude or --group?

With --label compile-connected, a compile-time dependency is only considered compile-connected if the file it points to has at least the given number of transitive dependencies.
@MrYawe
MrYawe force-pushed the xref-stats-fail-above branch from 8676d54 to 656141a Compare November 24, 2026 14:54
@MrYawe MrYawe changed the title Support --fail-above in xref stats Add --min-transitive-dependencies to xref graph Nov 24, 2026
@MrYawe

MrYawe commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

I tried both on our app. --group works in principle, and it even catches the case where the schema module grows, so fair point.

In practice though, groups can't overlap, so files sharing a dependency end up merged together. For us that gives 17 groups and about 130 paths to keep in the CI command, the biggest one with 53 files, and everything inside a group becomes invisible. --exclude is about 50 paths, and half of them are files other modules depend on at compile time, so we would hide those too.

And each time someone adds a small helper to one of those modules, the list needs an update. With the threshold it's one number and nothing to maintain.

I'll update the PR with the smaller version (the option on the label, no change to stats).

Comment on lines +100 to +105
Once those files are addressed, you can keep new ones from being introduced,
for example in your continuous integration server, by failing whenever there
is a compile-time dependency on a file with too many transitive dependencies:

$ mix xref graph --label compile-connected --min-transitive-dependencies 10 --fail-above 0

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove this from the intro section and perhaps have a new section on CI checks, where we can talk about this, --exclude and --group. WDYT?

end
end

test "min transitive dependencies requires the compile-connected label" do

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are probably too many tests, they have a good amount of redundancy. For example, the one that checks --fail-above does not raise can be mixed with the ones that prints and so on.

This branch has not been deployed

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants