From 479377c299bb8fd0c02015f03791af93197da01d Mon Sep 17 00:00:00 2001 From: Mitchell Hanberg Date: Thu, 9 May 2024 15:10:29 -0400 Subject: [PATCH 1/2] fix: correctly set MIX_HOME when using bundled Elixir Fixes #460 --- lib/next_ls.ex | 12 +++++++++-- lib/next_ls/runtime.ex | 31 ++++++++++++++++----------- lib/next_ls/runtime/bundled_elixir.ex | 24 ++++++++++++--------- 3 files changed, 42 insertions(+), 25 deletions(-) diff --git a/lib/next_ls.ex b/lib/next_ls.ex index f4fb6030..9b8d780b 100644 --- a/lib/next_ls.ex +++ b/lib/next_ls.ex @@ -89,8 +89,6 @@ defmodule NextLS do cache = Keyword.fetch!(args, :cache) {:ok, logger} = DynamicSupervisor.start_child(dynamic_supervisor, {NextLS.Logger, lsp: lsp}) - NextLS.Runtime.BundledElixir.install(bundle_base, logger, mix_home: mix_home) - {:ok, assign(lsp, auto_update: Keyword.get(args, :auto_update, false), @@ -130,8 +128,16 @@ defmodule NextLS do [%{name: Path.basename(root_uri), uri: root_uri}] end + {:ok, init_opts} = __MODULE__.InitOpts.validate(init_opts) + mix_home = + if init_opts.experimental.completions.enable do + NextLS.Runtime.BundledElixir.mix_home(lsp.assigns.bundle_base) + else + nil + end + {:reply, %InitializeResult{ capabilities: %ServerCapabilities{ @@ -175,6 +181,7 @@ defmodule NextLS do server_info: %{name: "Next LS"} }, assign(lsp, + mix_home: mix_home, root_uri: root_uri, workspace_folders: workspace_folders, client_capabilities: caps, @@ -868,6 +875,7 @@ defmodule NextLS do }) end + NextLS.Runtime.BundledElixir.install(lsp.assigns.bundle_base, lsp.assigns.logger) GenLSP.log(lsp, "[Next LS] Booting runtimes...") parent = self() diff --git a/lib/next_ls/runtime.ex b/lib/next_ls/runtime.ex index b8b5f1f7..87cc5f1e 100644 --- a/lib/next_ls/runtime.ex +++ b/lib/next_ls/runtime.ex @@ -140,19 +140,24 @@ defmodule NextLS.Runtime do |> Path.join("cmd") |> Path.absname() - env = [ - {~c"LSP", ~c"nextls"}, - {~c"NEXTLS_PARENT_PID", parent}, - {~c"MIX_ENV", ~c"#{mix_env}"}, - {~c"MIX_TARGET", ~c"#{mix_target}"}, - {~c"MIX_BUILD_ROOT", ~c".elixir-tools/_build"}, - {~c"MIX_HOME", ~c"#{mix_home}"}, - {~c"ROOTDIR", false}, - {~c"BINDIR", false}, - {~c"RELEASE_ROOT", false}, - {~c"RELEASE_SYS_CONFIG", false}, - {~c"PATH", String.to_charlist(new_path)} - ] + env = + [ + {~c"LSP", ~c"nextls"}, + {~c"NEXTLS_PARENT_PID", parent}, + {~c"MIX_ENV", ~c"#{mix_env}"}, + {~c"MIX_TARGET", ~c"#{mix_target}"}, + {~c"MIX_BUILD_ROOT", ~c".elixir-tools/_build"}, + {~c"ROOTDIR", false}, + {~c"BINDIR", false}, + {~c"RELEASE_ROOT", false}, + {~c"RELEASE_SYS_CONFIG", false}, + {~c"PATH", String.to_charlist(new_path)} + ] ++ + if mix_home do + [{~c"MIX_HOME", ~c"#{mix_home}"}] + else + [] + end args = [elixir_exe] ++ diff --git a/lib/next_ls/runtime/bundled_elixir.ex b/lib/next_ls/runtime/bundled_elixir.ex index 16edd0fa..cc24e0be 100644 --- a/lib/next_ls/runtime/bundled_elixir.ex +++ b/lib/next_ls/runtime/bundled_elixir.ex @@ -20,9 +20,13 @@ defmodule NextLS.Runtime.BundledElixir do Path.join([base, @dir]) end - def install(base, logger, opts \\ []) do - basedir = path(base) - mixhome = Keyword.get(opts, :mix_home) || Path.join(basedir, ".mix") + def mix_home(base) do + Path.join(path(base), ".mix") + end + + def install(base, logger) do + mixhome = mix_home(base) + File.mkdir_p!(mixhome) binpath = binpath(base) unless File.exists?(binpath) do @@ -37,16 +41,16 @@ defmodule NextLS.Runtime.BundledElixir do for bin <- Path.wildcard(Path.join(binpath, "*")) do File.chmod(bin, 0o755) end + end - new_path = "#{binpath}:#{System.get_env("PATH")}" - mixbin = mixpath(base) + new_path = "#{binpath}:#{System.get_env("PATH")}" + mixbin = mixpath(base) - {_, 0} = - System.cmd(mixbin, ["local.rebar", "--force"], env: [{"PATH", new_path}, {"MIX_HOME", mixhome}]) + {_, 0} = + System.cmd(mixbin, ["local.rebar", "--force"], env: [{"PATH", new_path}, {"MIX_HOME", mixhome}]) - {_, 0} = - System.cmd(mixbin, ["local.hex", "--force"], env: [{"PATH", new_path}, {"MIX_HOME", mixhome}]) - end + {_, 0} = + System.cmd(mixbin, ["local.hex", "--force"], env: [{"PATH", new_path}, {"MIX_HOME", mixhome}]) :ok rescue From 1ad422390ba1f1346d6cbd61e5446ccfd98363c8 Mon Sep 17 00:00:00 2001 From: Mitchell Hanberg Date: Thu, 9 May 2024 15:12:42 -0400 Subject: [PATCH 2/2] fixup! fix: correctly set MIX_HOME when using bundled Elixir --- lib/next_ls.ex | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/next_ls.ex b/lib/next_ls.ex index 9b8d780b..e695416a 100644 --- a/lib/next_ls.ex +++ b/lib/next_ls.ex @@ -128,7 +128,6 @@ defmodule NextLS do [%{name: Path.basename(root_uri), uri: root_uri}] end - {:ok, init_opts} = __MODULE__.InitOpts.validate(init_opts) mix_home =