justapilgrim
justapilgrim

Reputation: 6882

How to list callers of a function in Elixir?

I'm refactoring a function and I want to know every file that calls it. With aliases and imports, a simple grep would list other functions with the same name in different modules or would miss some calls.

I tried using mix xref but it doesn't work with functions, only modules (I'm using Elixir 1.12.1).

$ mix xref callers MySchema.changeset/2
** (Mix) xref callers MODULE expects a MODULE, got: MySchema.changeset/2

Is there a tool or a xref command to list callers of a function in Elixir?

Upvotes: 0

Views: 453

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121010

There is a deprecated Mix.Tasks.Xref.calls/1 function, but it has been deprecated for a reason, Compilation tracers are way more powerful.

You might set a tracer for {:remote_function, _, YourModule, :your_fun, your_arity} and simply IO.puts/2 from there.

Upvotes: 4

Related Questions