Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support setting MIX_TARGET #299

Merged
merged 1 commit into from
Jun 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion apps/language_server/lib/language_server/build.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ defmodule ElixirLS.LanguageServer.Build do
with_build_lock(fn ->
{us, _} =
:timer.tc(fn ->
IO.puts("Compiling with Mix env #{Mix.env()}")
IO.puts("MIX_ENV: #{Mix.env()}")

if function_exported?(Mix, :target, 0) do
# Even though we know we have the function here,
# Compilation will fail calling Mix.target/0 directly
# since Elixir 1.7 is used to compile - So we get around
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's time we dropped support for 1.7

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was brought up in the original issue, but ultimately decided that things weren't ready to drop the minimum to away from 1.8 - But I'm definitely open to suggestions about making this workaround look better if desired?

# that with apply/3
IO.puts("MIX_TARGET: #{apply(Mix, :target, [])}")
end

prev_deps = cached_deps()
:ok = Mix.Project.clear_deps_cache()
Expand Down
34 changes: 34 additions & 0 deletions apps/language_server/lib/language_server/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -748,11 +748,13 @@ defmodule ElixirLS.LanguageServer.Server do
Dialyzer.check_support() == :ok && Map.get(settings, "dialyzerEnabled", true)

mix_env = Map.get(settings, "mixEnv", "test")
mix_target = Map.get(settings, "mixTarget")
project_dir = Map.get(settings, "projectDir")

state =
state
|> set_mix_env(mix_env)
|> maybe_set_mix_target(mix_target)
|> set_project_dir(project_dir)
|> set_dialyzer_enabled(enable_dialyzer)

Expand Down Expand Up @@ -787,6 +789,38 @@ defmodule ElixirLS.LanguageServer.Server do
state
end

defp maybe_set_mix_target(state, nil), do: state

defp maybe_set_mix_target(state, target) do
if Version.match?(System.version(), ">= 1.8.0") do
set_mix_target(state, target)
else
JsonRpc.show_message(
:warning,
"MIX_TARGET was set, but it requires Elixir >= 1.8.0. This setting will be ignored"
)

state
end
end

defp set_mix_target(state, target) do
target = target || "host"

prev_target = state.settings["mixTarget"]

if is_nil(prev_target) or target == prev_target do
# We've already checked for Elixir >= 1.8.0 by this point
# but compilation will fail if we just call Mix.target/0
# so we get around that via apply/3
apply(Mix, :target, [String.to_atom(target)])
else
JsonRpc.show_message(:warning, "You must restart ElixirLS after changing Mix target")
end

state
end

defp set_project_dir(%{project_dir: prev_project_dir, root_uri: root_uri} = state, project_dir)
when is_binary(root_uri) do
root_dir = SourceFile.path_from_uri(root_uri)
Expand Down