Skip to content

Commit

Permalink
Load and compile plugins from cache, closes #12880
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Aug 23, 2023
1 parent 84c8d23 commit dc8cfcd
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions lib/mix/lib/mix/tasks/format.ex
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,7 @@ defmodule Mix.Tasks.Format do
Mix.raise("Expected :plugins to return a list of modules, got: #{inspect(plugins)}")
end

if plugins != [] do
Mix.Task.run("loadpaths", [])
end

if not Enum.all?(plugins, &Code.ensure_loaded?/1) do
Mix.Task.run("compile", [])
end
maybe_load_and_compile_plugins(plugins)

for plugin <- plugins do
cond do
Expand Down Expand Up @@ -351,12 +345,19 @@ defmodule Mix.Tasks.Format do
else
manifest = Path.join(Mix.Project.manifest_path(), @manifest)

{{locals_without_parens, subdirectories}, sources} =
{cached?, {{locals_without_parens, subdirectories}, sources}} =
maybe_cache_in_manifest(dot_formatter, manifest, fn ->
{subdirectories, sources} = eval_subs_opts(subs, cwd, sources)
{{eval_deps_opts(deps), subdirectories}, sources}
end)

# If we read from cache, we may still need to load and compile plugins.
if cached? do
Enum.each(subdirectories, fn {_path, {opts, _deps}} ->
maybe_load_and_compile_plugins(Keyword.get(opts, :plugins, []))
end)
end

formatter_opts =
Keyword.update(
formatter_opts,
Expand All @@ -369,11 +370,21 @@ defmodule Mix.Tasks.Format do
end
end

defp maybe_load_and_compile_plugins(plugins) do
if plugins != [] do
Mix.Task.run("loadpaths", [])
end

if not Enum.all?(plugins, &Code.ensure_loaded?/1) do
Mix.Task.run("compile", [])
end
end

defp maybe_cache_in_manifest(dot_formatter, manifest, fun) do
cond do
is_nil(Mix.Project.get()) or dot_formatter != ".formatter.exs" -> fun.()
entry = read_manifest(manifest) -> entry
true -> write_manifest!(manifest, fun.())
is_nil(Mix.Project.get()) or dot_formatter != ".formatter.exs" -> {false, fun.()}
entry = read_manifest(manifest) -> {true, entry}
true -> {false, write_manifest!(manifest, fun.())}
end
end

Expand Down

0 comments on commit dc8cfcd

Please sign in to comment.