We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
Find wide compile dependencies with `mix xref graph`
almirsarajcic
When a small change recompiles a large part of an application, mix xref graph can show which file has the widest compile-time reach.
# Show files with the most compile-time dependents.
mix xref graph --format stats --label compile-connected
Top 10 files with most incoming dependencies:
lib/my_app_web.ex (187)
lib/my_app/repo.ex (54)
lib/my_app_web/components/core_components.ex (31)
The compile-connected label includes transitive compile dependencies, so the stats show how many files may need recompiling after a given file changes.
Macros commonly create these dependencies through use, require, and macro imports. Code evaluated in a module body can create them as well. An ordinary remote function call inside a function body is a runtime dependency instead.
Use --sink or --source to inspect a file from either direction:
# Files with a path to my_app_web.ex.
mix xref graph --sink lib/my_app_web.ex --label compile-connected
# Files referenced by this LiveView.
mix xref graph --source lib/my_app_web/live/post_live/index.ex
In a Phoenix application, lib/my_app_web.ex often appears near the top because controllers, components, and LiveViews all use it.
Keep those quoted definitions small and stable. Helpers that change often are usually better placed in an ordinary module:
# Editing this quoted helper can recompile every module that uses it.
defmodule MyAppWeb do
def live_view do
quote do
use Phoenix.LiveView
def page_title(suffix), do: "#{suffix} · MyApp"
end
end
end
# Keep the implementation in a regular module.
defmodule MyAppWeb do
def live_view do
quote do
use Phoenix.LiveView
import MyAppWeb.PageHelpers
end
end
end
The same task can also find compile cycles and produce a Graphviz file:
# List compile-time cycles.
mix xref graph --format cycles --label compile-connected
# Render the graph as an image.
mix xref graph --format dot --label compile-connected
dot -Tpng xref_graph.dot -o xref_graph.png
Calls to Application.compile_env/3 are another source of compile-time dependencies, so they are worth checking in files with a large reach.
copied to clipboard