Skip to content

Commit 1625877

Browse files
authored
fix: correctly set MIX_HOME when using bundled Elixir (#461)
Fixes #460
1 parent 2a9097e commit 1625877

File tree

3 files changed

+41
-25
lines changed

3 files changed

+41
-25
lines changed

lib/next_ls.ex

+9-2
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ defmodule NextLS do
8989
cache = Keyword.fetch!(args, :cache)
9090
{:ok, logger} = DynamicSupervisor.start_child(dynamic_supervisor, {NextLS.Logger, lsp: lsp})
9191

92-
NextLS.Runtime.BundledElixir.install(bundle_base, logger, mix_home: mix_home)
93-
9492
{:ok,
9593
assign(lsp,
9694
auto_update: Keyword.get(args, :auto_update, false),
@@ -132,6 +130,13 @@ defmodule NextLS do
132130

133131
{:ok, init_opts} = __MODULE__.InitOpts.validate(init_opts)
134132

133+
mix_home =
134+
if init_opts.experimental.completions.enable do
135+
NextLS.Runtime.BundledElixir.mix_home(lsp.assigns.bundle_base)
136+
else
137+
nil
138+
end
139+
135140
{:reply,
136141
%InitializeResult{
137142
capabilities: %ServerCapabilities{
@@ -175,6 +180,7 @@ defmodule NextLS do
175180
server_info: %{name: "Next LS"}
176181
},
177182
assign(lsp,
183+
mix_home: mix_home,
178184
root_uri: root_uri,
179185
workspace_folders: workspace_folders,
180186
client_capabilities: caps,
@@ -868,6 +874,7 @@ defmodule NextLS do
868874
})
869875
end
870876

877+
NextLS.Runtime.BundledElixir.install(lsp.assigns.bundle_base, lsp.assigns.logger)
871878
GenLSP.log(lsp, "[Next LS] Booting runtimes...")
872879

873880
parent = self()

lib/next_ls/runtime.ex

+18-13
Original file line numberDiff line numberDiff line change
@@ -140,19 +140,24 @@ defmodule NextLS.Runtime do
140140
|> Path.join("cmd")
141141
|> Path.absname()
142142

143-
env = [
144-
{~c"LSP", ~c"nextls"},
145-
{~c"NEXTLS_PARENT_PID", parent},
146-
{~c"MIX_ENV", ~c"#{mix_env}"},
147-
{~c"MIX_TARGET", ~c"#{mix_target}"},
148-
{~c"MIX_BUILD_ROOT", ~c".elixir-tools/_build"},
149-
{~c"MIX_HOME", ~c"#{mix_home}"},
150-
{~c"ROOTDIR", false},
151-
{~c"BINDIR", false},
152-
{~c"RELEASE_ROOT", false},
153-
{~c"RELEASE_SYS_CONFIG", false},
154-
{~c"PATH", String.to_charlist(new_path)}
155-
]
143+
env =
144+
[
145+
{~c"LSP", ~c"nextls"},
146+
{~c"NEXTLS_PARENT_PID", parent},
147+
{~c"MIX_ENV", ~c"#{mix_env}"},
148+
{~c"MIX_TARGET", ~c"#{mix_target}"},
149+
{~c"MIX_BUILD_ROOT", ~c".elixir-tools/_build"},
150+
{~c"ROOTDIR", false},
151+
{~c"BINDIR", false},
152+
{~c"RELEASE_ROOT", false},
153+
{~c"RELEASE_SYS_CONFIG", false},
154+
{~c"PATH", String.to_charlist(new_path)}
155+
] ++
156+
if mix_home do
157+
[{~c"MIX_HOME", ~c"#{mix_home}"}]
158+
else
159+
[]
160+
end
156161

157162
args =
158163
[elixir_exe] ++

lib/next_ls/runtime/bundled_elixir.ex

+14-10
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ defmodule NextLS.Runtime.BundledElixir do
2020
Path.join([base, @dir])
2121
end
2222

23-
def install(base, logger, opts \\ []) do
24-
basedir = path(base)
25-
mixhome = Keyword.get(opts, :mix_home) || Path.join(basedir, ".mix")
23+
def mix_home(base) do
24+
Path.join(path(base), ".mix")
25+
end
26+
27+
def install(base, logger) do
28+
mixhome = mix_home(base)
29+
File.mkdir_p!(mixhome)
2630
binpath = binpath(base)
2731

2832
unless File.exists?(binpath) do
@@ -37,16 +41,16 @@ defmodule NextLS.Runtime.BundledElixir do
3741
for bin <- Path.wildcard(Path.join(binpath, "*")) do
3842
File.chmod(bin, 0o755)
3943
end
44+
end
4045

41-
new_path = "#{binpath}:#{System.get_env("PATH")}"
42-
mixbin = mixpath(base)
46+
new_path = "#{binpath}:#{System.get_env("PATH")}"
47+
mixbin = mixpath(base)
4348

44-
{_, 0} =
45-
System.cmd(mixbin, ["local.rebar", "--force"], env: [{"PATH", new_path}, {"MIX_HOME", mixhome}])
49+
{_, 0} =
50+
System.cmd(mixbin, ["local.rebar", "--force"], env: [{"PATH", new_path}, {"MIX_HOME", mixhome}])
4651

47-
{_, 0} =
48-
System.cmd(mixbin, ["local.hex", "--force"], env: [{"PATH", new_path}, {"MIX_HOME", mixhome}])
49-
end
52+
{_, 0} =
53+
System.cmd(mixbin, ["local.hex", "--force"], env: [{"PATH", new_path}, {"MIX_HOME", mixhome}])
5054

5155
:ok
5256
rescue

0 commit comments

Comments
 (0)