Skip to content

Commit 8f7ace9

Browse files
committed
improvement: only compile on uncompiled extension in formatter
fixes #238
1 parent 5b4a643 commit 8f7ace9

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

lib/spark/formatter.ex

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ if Code.ensure_loaded?(Sourceror) do
5858
end
5959

6060
def format(contents, opts) do
61-
safe_compile()
62-
6361
config =
6462
:spark
6563
|> Application.get_env(:formatter, [])
@@ -97,18 +95,6 @@ if Code.ensure_loaded?(Sourceror) do
9795
end
9896
end
9997

100-
# Compiles the project if safe to do so.
101-
# Skips compilation when running at umbrella root to avoid Mix.ProjectStack violations.
102-
# See: https://github.com/elixir-lang/elixir/issues/11759
103-
defp safe_compile do
104-
if not Mix.Project.umbrella?() do
105-
Mix.Task.reenable("compile")
106-
Mix.Task.reenable("loadpaths")
107-
Mix.Task.run("compile")
108-
Mix.Task.run("loadpaths")
109-
end
110-
end
111-
11298
defp format_resources(parsed, opts, config) do
11399
{_, patches} =
114100
Spark.CodeHelpers.prewalk(parsed, [], false, fn
@@ -324,17 +310,25 @@ if Code.ensure_loaded?(Sourceror) do
324310
end)
325311
end
326312

327-
defp safe_get_default_extensions(type) do
313+
defp safe_get_default_extensions(type, retry? \\ false) do
328314
type.default_extensions() || []
329315
rescue
330316
error ->
331-
Logger.warning("""
332-
Spark.Formatter: Could not load default_extensions for #{inspect(type)}.
333-
This can happen in umbrella projects when running format from the root.
334-
Try running format from within the sub-app, or compile first with: mix compile
335-
336-
Error: #{inspect(error)}
337-
""")
317+
if Mix.Project.umbrella?() || retry? do
318+
Logger.warning("""
319+
Spark.Formatter: Could not load default_extensions for #{inspect(type)}.
320+
This can happen in umbrella projects when running format from the root.
321+
Try running format from within the sub-app, or compile first with: mix compile
322+
323+
Error: #{inspect(error)}
324+
""")
325+
else
326+
Mix.Task.reenable("compile")
327+
Mix.Task.reenable("loadpaths")
328+
Mix.Task.run("compile")
329+
Mix.Task.run("loadpaths")
330+
safe_get_default_extensions(type, true)
331+
end
338332

339333
[]
340334
end

0 commit comments

Comments
 (0)