Skip to content

Commit

Permalink
Clear app config on build
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszsamson committed Jan 21, 2024
1 parent 9e84fd7 commit 56fb766
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions apps/language_server/lib/language_server/build.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ defmodule ElixirLS.LanguageServer.Build do
alias ElixirLS.Utils.MixfileHelpers
require Logger

defp store_required_apps() do
unless :persistent_term.get(:language_server_required_apps, false) do
apps = Application.loaded_applications() |> Enum.map(&elem(&1, 0))
:persistent_term.put(:language_server_required_apps, apps)
end
end

def build(parent, root_path, opts) when is_binary(root_path) do
Application.loaded_applications() |> Enum.map(&elem(&1, 0)) |> dbg

build_pid_reference =
spawn_monitor(fn ->
with_build_lock(fn ->
Expand Down Expand Up @@ -180,6 +189,8 @@ defmodule ElixirLS.LanguageServer.Build do
end

defp reload_project(mixfile, root_path) do
store_required_apps()

if File.exists?(mixfile) do
if module = Mix.Project.get() do
if module != ElixirLS.LanguageServer.MixProject do
Expand Down Expand Up @@ -235,6 +246,8 @@ defmodule ElixirLS.LanguageServer.Build do
Mix.Project.clear_deps_cache()
Mix.State.clear_cache()

reset_apps_config()

Mix.Task.clear()

if Version.match?(System.version(), ">= 1.15.0-dev") do
Expand Down Expand Up @@ -472,8 +485,6 @@ defmodule ElixirLS.LanguageServer.Build do
end

defp purge_app(app, purge_modules? \\ true) do
Logger.debug("Stopping #{app}")

case Application.stop(app) do
:ok -> :ok
{:error, {:not_started, _}} -> :ok
Expand All @@ -488,13 +499,10 @@ defmodule ElixirLS.LanguageServer.Build do
end

if modules != [] do
Logger.debug("Purging #{length(modules)} modules from #{app}")
for module <- modules, do: purge_module(module)
end
end

Logger.debug("Unloading #{app}")

case Application.unload(app) do
:ok -> :ok
{:error, {:not_loaded, _}} -> :ok
Expand All @@ -520,6 +528,8 @@ defmodule ElixirLS.LanguageServer.Build do
defp maybe_purge_dep(%Mix.Dep{status: status, deps: deps} = dep) do
for dep <- deps, do: maybe_purge_dep(dep)

dbg({dep.app, status})

purge? =
case status do
{:nomatchvsn, _} -> true
Expand All @@ -534,16 +544,7 @@ defmodule ElixirLS.LanguageServer.Build do
end

defp purge_dep(%Mix.Dep{app: app} = dep) do
if app in [
:language_server,
:elixir_ls_utils,
:elixir_sense,
:jason_v,
:path_glob_vendored,
:dialyxir_vendored,
:erlex_vendored,
:erl2ex_vendored
] do
if app in :persistent_term.get(:language_server_required_apps) do
raise "Unloading required #{app}"
end

Expand Down Expand Up @@ -610,6 +611,17 @@ defmodule ElixirLS.LanguageServer.Build do
end
end

defp reset_apps_config() do
apps = Application.loaded_applications() |> Enum.map(&elem(&1, 0))

for app <- apps -- :persistent_term.get(:language_server_required_apps) do
# workaround for https://github.com/elixir-lang/elixir/issues/13246
for {key, _} <- :application.get_all_env(app) do
:application.unset_env(app, key, persistent: true)
end
end
end

defp fetch_deps(current_deps) do
missing_deps =
current_deps
Expand Down

0 comments on commit 56fb766

Please sign in to comment.