Skip to content

Commit

Permalink
fix: Correctly set MIX_HOME when using bundled Elixir
Browse files Browse the repository at this point in the history
Fixes #460
  • Loading branch information
mhanberg committed May 9, 2024
1 parent 2a9097e commit 43d0841
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
12 changes: 10 additions & 2 deletions lib/next_ls.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down
31 changes: 18 additions & 13 deletions lib/next_ls/runtime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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] ++
Expand Down
24 changes: 14 additions & 10 deletions lib/next_ls/runtime/bundled_elixir.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 43d0841

Please sign in to comment.