Skip to content

Commit 7b6a3f5

Browse files
committed
cache Mix.Project data and make all accesses to it safe from Mix.State push/pop during compile
1 parent 56ff770 commit 7b6a3f5

File tree

1 file changed

+33
-11
lines changed
  • apps/language_server/lib/language_server/mix_tasks

1 file changed

+33
-11
lines changed

apps/language_server/lib/language_server/mix_tasks/format.ex

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,14 @@ defmodule Mix.Tasks.ElixirLSFormat do
267267
Mix.raise("--no-exit can only be used together with --check-formatted")
268268
end
269269

270+
opts =
271+
Keyword.merge(opts,
272+
deps_paths: Mix.Project.deps_paths(),
273+
manifest_path: Mix.Project.manifest_path(),
274+
config_mtime: Mix.Project.config_mtime(),
275+
mix_project: Mix.Project.get()
276+
)
277+
270278
{formatter_opts_and_subs, _sources} =
271279
eval_deps_and_subdirectories(cwd, dot_formatter, formatter_opts, [dot_formatter], opts)
272280

@@ -411,6 +419,15 @@ defmodule Mix.Tasks.ElixirLSFormat do
411419
@doc since: "1.13.0"
412420
def formatter_for_file(file, opts \\ []) do
413421
cwd = Keyword.get_lazy(opts, :root, &File.cwd!/0)
422+
423+
opts =
424+
Keyword.merge(opts,
425+
deps_paths: Keyword.get_lazy(opts, :deps_paths, &Mix.Project.deps_paths/0),
426+
manifest_path: Keyword.get_lazy(opts, :manifest_path, &Mix.Project.manifest_path/0),
427+
config_mtime: Keyword.get_lazy(opts, :config_mtime, &Mix.Project.config_mtime/0),
428+
mix_project: Keyword.get_lazy(opts, :mix_project, &Mix.Project.get/0)
429+
)
430+
414431
{dot_formatter, formatter_opts} = eval_dot_formatter(cwd, opts)
415432

416433
{formatter_opts_and_subs, _sources} =
@@ -464,13 +481,19 @@ defmodule Mix.Tasks.ElixirLSFormat do
464481
if deps == [] and subs == [] do
465482
{{formatter_opts, []}, sources}
466483
else
467-
manifest = Path.join(Mix.Project.manifest_path(), @manifest_dot_formatter)
484+
manifest = Path.join(opts[:manifest_path], @manifest_dot_formatter)
468485

469486
{{locals_without_parens, subdirectories}, sources} =
470-
maybe_cache_in_manifest(dot_formatter, manifest, fn ->
471-
{subdirectories, sources} = eval_subs_opts(subs, cwd, sources, opts)
472-
{{eval_deps_opts(deps, opts), subdirectories}, sources}
473-
end)
487+
maybe_cache_in_manifest(
488+
dot_formatter,
489+
opts[:mix_project],
490+
manifest,
491+
opts[:config_mtime],
492+
fn ->
493+
{subdirectories, sources} = eval_subs_opts(subs, cwd, sources, opts)
494+
{{eval_deps_opts(deps, opts), subdirectories}, sources}
495+
end
496+
)
474497

475498
formatter_opts =
476499
Keyword.update(
@@ -484,19 +507,19 @@ defmodule Mix.Tasks.ElixirLSFormat do
484507
end
485508
end
486509

487-
defp maybe_cache_in_manifest(dot_formatter, manifest, fun) do
510+
defp maybe_cache_in_manifest(dot_formatter, mix_project, manifest, config_mtime, fun) do
488511
cond do
489-
is_nil(Mix.Project.get()) or dot_formatter != ".formatter.exs" -> fun.()
490-
entry = read_manifest(manifest) -> entry
512+
is_nil(mix_project) or dot_formatter != ".formatter.exs" -> fun.()
513+
entry = read_manifest(manifest, config_mtime) -> entry
491514
true -> write_manifest!(manifest, fun.())
492515
end
493516
end
494517

495-
defp read_manifest(manifest) do
518+
defp read_manifest(manifest, config_mtime) do
496519
with {:ok, binary} <- File.read(manifest),
497520
{:ok, {@manifest_vsn, entry, sources}} <- safe_binary_to_term(binary),
498521
expanded_sources = Enum.flat_map(sources, &Path.wildcard(&1, match_dot: true)),
499-
false <- Mix.Utils.stale?([Mix.Project.config_mtime() | expanded_sources], [manifest]) do
522+
false <- Mix.Utils.stale?([config_mtime | expanded_sources], [manifest]) do
500523
{entry, sources}
501524
else
502525
_ -> nil
@@ -1069,4 +1092,3 @@ defmodule Mix.Tasks.ElixirLSFormat do
10691092
end
10701093
end
10711094
end
1072-

0 commit comments

Comments
 (0)