Skip to content

Commit db4b5fb

Browse files
committed
extract prune_existing_mix_project
1 parent 7899d19 commit db4b5fb

File tree

1 file changed

+50
-45
lines changed
  • apps/language_server/lib/language_server

1 file changed

+50
-45
lines changed

apps/language_server/lib/language_server/build.ex

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -191,51 +191,7 @@ defmodule ElixirLS.LanguageServer.Build do
191191
store_required_apps()
192192

193193
if File.exists?(mixfile) do
194-
if module = Mix.Project.get() do
195-
if module != ElixirLS.LanguageServer.MixProject do
196-
build_path = Mix.Project.config()[:build_path]
197-
198-
deps_paths =
199-
try do
200-
# this call can raise (RuntimeError) cannot retrieve dependencies information because dependencies
201-
# were not loaded. Please invoke one of "deps.loadpaths", "loadpaths", or "compile" Mix task
202-
Mix.Project.deps_paths()
203-
catch
204-
kind, payload ->
205-
{payload, stacktrace} = Exception.blame(kind, payload, __STACKTRACE__)
206-
message = Exception.format(kind, payload, stacktrace)
207-
Logger.warning("Unable to prune mix project: #{message}")
208-
[]
209-
end
210-
211-
for {app, path} <- deps_paths do
212-
child_module =
213-
try do
214-
Mix.Project.in_project(app, path, [build_path: build_path], fn mix_project ->
215-
mix_project
216-
end)
217-
catch
218-
kind, payload ->
219-
{payload, stacktrace} = Exception.blame(kind, payload, __STACKTRACE__)
220-
message = Exception.format(kind, payload, stacktrace)
221-
Logger.warning("Unable to prune mix project module for #{app}: #{message}")
222-
nil
223-
end
224-
225-
if child_module do
226-
purge_module(child_module)
227-
end
228-
end
229-
230-
unload_mix_project_apps()
231-
232-
Mix.Project.pop()
233-
purge_module(module)
234-
else
235-
# don't do any pruning in language server tests
236-
Mix.Project.pop()
237-
end
238-
end
194+
prune_existing_mix_project()
239195

240196
# We need to clear persistent cache, otherwise `deps.loadpaths` task fails with
241197
# (Mix.Error) Can't continue due to errors on dependencies
@@ -431,6 +387,55 @@ defmodule ElixirLS.LanguageServer.Build do
431387
end
432388
end
433389

390+
# If a project is already loaded, prune its modules so that we start fresh.
391+
defp prune_existing_mix_project() do
392+
if module = Mix.Project.get() do
393+
if module != ElixirLS.LanguageServer.MixProject do
394+
build_path = Mix.Project.config()[:build_path]
395+
396+
deps_paths =
397+
try do
398+
# this call can raise (RuntimeError) cannot retrieve dependencies information because dependencies
399+
# were not loaded. Please invoke one of "deps.loadpaths", "loadpaths", or "compile" Mix task
400+
Mix.Project.deps_paths()
401+
catch
402+
kind, payload ->
403+
{payload, stacktrace} = Exception.blame(kind, payload, __STACKTRACE__)
404+
message = Exception.format(kind, payload, stacktrace)
405+
Logger.warning("Unable to prune mix project: #{message}")
406+
[]
407+
end
408+
409+
for {app, path} <- deps_paths do
410+
child_module =
411+
try do
412+
Mix.Project.in_project(app, path, [build_path: build_path], fn mix_project ->
413+
mix_project
414+
end)
415+
catch
416+
kind, payload ->
417+
{payload, stacktrace} = Exception.blame(kind, payload, __STACKTRACE__)
418+
message = Exception.format(kind, payload, stacktrace)
419+
Logger.warning("Unable to prune mix project module for #{app}: #{message}")
420+
nil
421+
end
422+
423+
if child_module do
424+
purge_module(child_module)
425+
end
426+
end
427+
428+
unload_mix_project_apps()
429+
430+
Mix.Project.pop()
431+
purge_module(module)
432+
else
433+
# don't do any pruning in language server tests
434+
Mix.Project.pop()
435+
end
436+
end
437+
end
438+
434439
defp run_mix_compile(force?) do
435440
opts = [
436441
"--return-errors",

0 commit comments

Comments
 (0)