Skip to content

Commit

Permalink
Do not emit warnings on code generation for escript builds
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Dec 10, 2024
1 parent 3f01b31 commit db2a39a
Showing 1 changed file with 38 additions and 32 deletions.
70 changes: 38 additions & 32 deletions lib/mix/lib/mix/tasks/escript.build.ex
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ defmodule Mix.Tasks.Escript.Build do
quote do
@spec main(OptionParser.argv()) :: any
def main(args) do
unquote(main_body_for(language, module, app, compile_config, runtime_config))
unquote(main_body_for(language, module, compile_config, runtime_config))
end

defp load_config(config) do
Expand All @@ -389,33 +389,8 @@ defmodule Mix.Tasks.Escript.Build do
:ok
end

defp start_app(nil) do
:ok
end

defp start_app(app) do
case :application.ensure_all_started(app) do
{:ok, _} ->
:ok

{:error, {app, reason}} ->
formatted_error =
case :code.ensure_loaded(Application) do
{:module, Application} -> Application.format_error(reason)
{:error, _} -> :io_lib.format(~c"~p", [reason])
end

error_message = [
"ERROR! Could not start application ",
:erlang.atom_to_binary(app, :utf8),
": ",
formatted_error,
?\n
]

io_error(error_message)
:erlang.halt(1)
end
defp start_app() do
unquote(start_app_for(app))
end

defp io_error(message) do
Expand All @@ -427,7 +402,7 @@ defmodule Mix.Tasks.Escript.Build do
[{~c"#{name}.beam", binary}]
end

defp main_body_for(:elixir, module, app, compile_config, runtime_config) do
defp main_body_for(:elixir, module, compile_config, runtime_config) do
config =
if runtime_config do
quote do
Expand All @@ -452,7 +427,7 @@ defmodule Mix.Tasks.Escript.Build do
args = Enum.map(args, &List.to_string(&1))
System.argv(args)
load_config(unquote(config))
start_app(unquote(app))
start_app()
Kernel.CLI.run(fn _ -> unquote(module).main(args) end)

error ->
Expand All @@ -462,11 +437,42 @@ defmodule Mix.Tasks.Escript.Build do
end
end

defp main_body_for(:erlang, module, app, compile_config, _runtime_config) do
defp main_body_for(:erlang, module, compile_config, _runtime_config) do
quote do
load_config(unquote(compile_config))
start_app(unquote(app))
start_app()
unquote(module).main(args)
end
end

defp start_app_for(nil) do
:ok
end

defp start_app_for(app) do
quote do
case :application.ensure_all_started(unquote(app)) do
{:ok, _} ->
:ok

{:error, {app, reason}} ->
formatted_error =
case :code.ensure_loaded(Application) do
{:module, Application} -> Application.format_error(reason)
{:error, _} -> :io_lib.format(~c"~p", [reason])
end

error_message = [
"ERROR! Could not start application ",
:erlang.atom_to_binary(app, :utf8),
": ",
formatted_error,
?\n
]

io_error(error_message)
:erlang.halt(1)
end
end
end
end

0 comments on commit db2a39a

Please sign in to comment.