Skip to content

Add compilers option to Mix.install/2 #14577

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 9 additions & 2 deletions lib/mix/lib/mix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,7 @@ defmodule Mix do
config: [],
config_path: nil,
consolidate_protocols: true,
compilers: nil,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
compilers: nil,
compilers: [:elixir],

Maybe can handle the default here (?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

absolutely, I feel a bit stupid now :D

elixir: nil,
force: false,
lockfile: nil,
Expand All @@ -899,6 +900,12 @@ defmodule Mix do
consolidate_protocols? = Keyword.fetch!(opts, :consolidate_protocols)
start_applications? = Keyword.fetch!(opts, :start_applications)

compilers =
case Keyword.fetch!(opts, :compilers) do
nil -> [:elixir]
[_ | _] = compilers -> compilers
end

id =
{deps, config, system_env, consolidate_protocols?}
|> :erlang.term_to_binary()
Expand All @@ -923,7 +930,8 @@ defmodule Mix do
dynamic_config = [
deps: deps,
consolidate_protocols: consolidate_protocols?,
config_path: config_path
config_path: config_path,
compilers: compilers
]

:ok =
Expand Down Expand Up @@ -1093,7 +1101,6 @@ defmodule Mix do
app: @mix_install_app,
erlc_paths: [],
elixirc_paths: [],
compilers: [:elixir],
prune_code_paths: false
] ++ dynamic_config
end
Expand Down
43 changes: 40 additions & 3 deletions lib/mix/test/mix_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ defmodule MixTest do
assert Protocol.consolidated?(InstallTest.Protocol)

assert_received {:mix_shell, :info, ["==> install_test"]}
assert_received {:mix_shell, :info, ["Compiling 2 files (.ex)"]}
assert_received {:mix_shell, :info, ["Compiling 3 files (.ex)"]}
assert_received {:mix_shell, :info, ["Generated install_test app"]}
refute_received _

Expand Down Expand Up @@ -71,7 +71,7 @@ defmodule MixTest do

assert File.dir?(Path.join(tmp_dir, "installs"))
assert_received {:mix_shell, :info, ["==> install_test"]}
assert_received {:mix_shell, :info, ["Compiling 2 files (.ex)"]}
assert_received {:mix_shell, :info, ["Compiling 3 files (.ex)"]}
assert_received {:mix_shell, :info, ["Generated install_test app"]}
refute_received _

Expand Down Expand Up @@ -361,7 +361,7 @@ defmodule MixTest do
])

assert_received {:mix_shell, :info, ["==> install_test"]}
assert_received {:mix_shell, :info, ["Compiling 2 files (.ex)"]}
assert_received {:mix_shell, :info, ["Compiling 3 files (.ex)"]}
assert_received {:mix_shell, :info, ["Generated install_test app"]}
refute_received _

Expand Down Expand Up @@ -430,6 +430,29 @@ defmodule MixTest do
System.delete_env("MIX_INSTALL_RESTORE_PROJECT_DIR")
end

test "custom compilers", %{tmp_dir: tmp_dir} do
Mix.install(
[
{:install_test, path: Path.join(tmp_dir, "install_test")}
],
compilers: [:elixir, :install_test]
)

assert File.dir?(Path.join(tmp_dir, "installs"))

assert Protocol.consolidated?(InstallTest.Protocol)

assert_received {:mix_shell, :info, ["==> install_test"]}
assert_received {:mix_shell, :info, ["Compiling 3 files (.ex)"]}
assert_received {:mix_shell, :info, ["Generated install_test app"]}
assert_received {:mix_shell, :info, ["==> mix_install"]}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure why this line is printed as well.

assert_received {:mix_shell, :info, ["Hello from custom compiler!"]}
refute_received _

assert List.keyfind(Application.started_applications(), :install_test, 0)
assert apply(InstallTest, :hello, []) == :world
end

test "installed?", %{tmp_dir: tmp_dir} do
refute Mix.installed?()

Expand Down Expand Up @@ -509,6 +532,20 @@ defmodule MixTest do
end
""")

File.mkdir_p!("#{tmp_dir}/install_test/lib/mix/tasks/compile/")

File.write!("#{tmp_dir}/install_test/lib/mix/tasks/compile/install_test.ex", """
defmodule Mix.Tasks.Compile.InstallTest do
use Mix.Task.Compiler

def run(_args) do
Mix.shell().info("Hello from custom compiler!")

:noop
end
end
""")

[tmp_dir: tmp_dir]
end

Expand Down
Loading