Skip to content

Commit f9f61bb

Browse files
committed
Do not recompile if compilation fails due to --warnings-as-errors
1 parent 4285cea commit f9f61bb

File tree

4 files changed

+15
-93
lines changed

4 files changed

+15
-93
lines changed

lib/elixir/lib/code.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1717,7 +1717,7 @@ defmodule Code do
17171717
def put_compiler_option(:warnings_as_errors, _value) do
17181718
IO.warn(
17191719
":warnings_as_errors is deprecated as part of Code.put_compiler_option/2, " <>
1720-
"pass it as option to Kernel.ParallelCompiler instead or as a --warnings-as-errors flag. " <>
1720+
"instead you must pass it as a --warnings-as-errors flag. " <>
17211721
"If you need to set it as a default in a mix task, you can also set it under aliases: " <>
17221722
"[compile: \"compile --warnings-as-errors\"]"
17231723
)

lib/elixir/lib/kernel/parallel_compiler.ex

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -257,28 +257,13 @@ defmodule Kernel.ParallelCompiler do
257257

258258
{status, modules_or_errors, info} =
259259
try do
260-
outcome = spawn_workers(schedulers, cache, files, output, options)
261-
{outcome, Keyword.get(options, :warnings_as_errors, false)}
260+
spawn_workers(schedulers, cache, files, output, options)
262261
else
263-
{{:ok, _, %{runtime_warnings: r_warnings, compile_warnings: c_warnings} = info}, true}
264-
when r_warnings != [] or c_warnings != [] ->
265-
message =
266-
"Compilation failed due to warnings while using the --warnings-as-errors option"
267-
268-
IO.puts(:stderr, message)
269-
errors = Enum.map(r_warnings ++ c_warnings, &Map.replace!(&1, :severity, :error))
270-
{:error, errors, %{info | runtime_warnings: [], compile_warnings: []}}
271-
272-
{{:ok, outcome, info}, _} ->
262+
{:ok, outcome, info} ->
273263
beam_timestamp = Keyword.get(options, :beam_timestamp)
274264
{:ok, write_module_binaries(outcome, output, beam_timestamp), info}
275265

276-
{{:error, errors, info}, true} ->
277-
%{runtime_warnings: r_warnings, compile_warnings: c_warnings} = info
278-
info = %{info | runtime_warnings: [], compile_warnings: []}
279-
{:error, c_warnings ++ r_warnings ++ errors, info}
280-
281-
{{:error, errors, info}, _} ->
266+
{:error, errors, info} ->
282267
{:error, errors, info}
283268
after
284269
Module.ParallelChecker.stop(cache)

lib/elixir/test/elixir/kernel/parallel_compiler_test.exs

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -454,40 +454,6 @@ defmodule Kernel.ParallelCompilerTest do
454454
end)
455455
end
456456

457-
test "supports warnings as errors" do
458-
[fixture] =
459-
write_tmp(
460-
"warnings_as_errors",
461-
warnings_as_errors: """
462-
defmodule WarningsSample do
463-
def hello(a), do: a
464-
def hello(b), do: b
465-
end
466-
"""
467-
)
468-
469-
output = tmp_path("not_to_be_used")
470-
471-
try do
472-
msg =
473-
capture_io(:stderr, fn ->
474-
assert {:error, [%{file: ^fixture, position: {3, 7}, message: "this clause " <> _}],
475-
@no_warnings} =
476-
Kernel.ParallelCompiler.compile_to_path([fixture], output,
477-
warnings_as_errors: true,
478-
return_diagnostics: true
479-
)
480-
end)
481-
482-
assert msg =~
483-
"Compilation failed due to warnings while using the --warnings-as-errors option\n"
484-
after
485-
purge([WarningsSample])
486-
end
487-
488-
refute File.exists?(output)
489-
end
490-
491457
test "does not use incorrect line number when error originates in another file" do
492458
File.mkdir_p!(tmp_path())
493459

@@ -655,35 +621,5 @@ defmodule Kernel.ParallelCompilerTest do
655621
"cannot compile module WithBehaviourAndStruct (errors have been logged)"
656622
end) =~ expected_msg
657623
end
658-
659-
test "supports warnings as errors" do
660-
[fixture] =
661-
write_tmp(
662-
"warnings_as_errors",
663-
warnings_as_errors: """
664-
defmodule WarningsSample do
665-
def hello(a), do: a
666-
def hello(b), do: b
667-
end
668-
"""
669-
)
670-
671-
try do
672-
msg =
673-
capture_io(:stderr, fn ->
674-
assert {:error, [%{file: ^fixture, position: {3, 7}, message: "this clause " <> _}],
675-
@no_warnings} =
676-
Kernel.ParallelCompiler.require([fixture],
677-
warnings_as_errors: true,
678-
return_diagnostics: true
679-
)
680-
end)
681-
682-
assert msg =~
683-
"Compilation failed due to warnings while using the --warnings-as-errors option\n"
684-
after
685-
purge([WarningsSample])
686-
end
687-
end
688624
end
689625
end

lib/mix/lib/mix/compilers/elixir.ex

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,18 @@ defmodule Mix.Compilers.Elixir do
208208
end
209209

210210
Mix.Task.Compiler.notify_modules_compiled(lazy_modules_diff)
211-
212-
unless_previous_warnings_as_errors(previous_warnings, opts, {:ok, all_warnings})
211+
unless_warnings_as_errors(opts, {:ok, all_warnings})
213212

214213
{:error, errors, %{runtime_warnings: r_warnings, compile_warnings: c_warnings}, state} ->
214+
{errors, warnings} =
215+
if opts[:warnings_as_errors],
216+
do: {errors ++ r_warnings ++ c_warnings, []},
217+
else: {errors, r_warnings ++ c_warnings}
218+
215219
# In case of errors, we show all previous warnings and all new ones.
216220
{_, _, sources, _, _, _} = state
217221
errors = Enum.map(errors, &diagnostic/1)
218-
warnings = Enum.map(r_warnings ++ c_warnings, &diagnostic/1)
222+
warnings = Enum.map(warnings, &diagnostic/1)
219223
all_warnings = Keyword.get(opts, :all_warnings, errors == [])
220224
{:error, previous_warnings(sources, all_warnings) ++ warnings ++ errors}
221225
after
@@ -254,8 +258,7 @@ defmodule Mix.Compilers.Elixir do
254258

255259
all_warnings = Keyword.get(opts, :all_warnings, true)
256260
previous_warnings = previous_warnings(sources, all_warnings)
257-
258-
unless_previous_warnings_as_errors(previous_warnings, opts, {status, previous_warnings})
261+
unless_warnings_as_errors(opts, {status, previous_warnings})
259262
end
260263
end
261264

@@ -1012,8 +1015,8 @@ defmodule Mix.Compilers.Elixir do
10121015
File.rm(manifest <> ".checkpoint")
10131016
end
10141017

1015-
defp unless_previous_warnings_as_errors(previous_warnings, opts, {status, all_warnings}) do
1016-
if previous_warnings != [] and opts[:warnings_as_errors] do
1018+
defp unless_warnings_as_errors(opts, {status, all_warnings}) do
1019+
if all_warnings != [] and opts[:warnings_as_errors] do
10171020
message = "Compilation failed due to warnings while using the --warnings-as-errors option"
10181021
IO.puts(:stderr, message)
10191022
{:error, all_warnings}
@@ -1049,7 +1052,6 @@ defmodule Mix.Compilers.Elixir do
10491052
threshold = opts[:long_compilation_threshold] || 10
10501053
profile = opts[:profile]
10511054
verbose = opts[:verbose] || false
1052-
warnings_as_errors = opts[:warnings_as_errors] || false
10531055

10541056
pid =
10551057
spawn_link(fn ->
@@ -1071,8 +1073,7 @@ defmodule Mix.Compilers.Elixir do
10711073
long_compilation_threshold: threshold,
10721074
profile: profile,
10731075
beam_timestamp: timestamp,
1074-
return_diagnostics: true,
1075-
warnings_as_errors: warnings_as_errors
1076+
return_diagnostics: true
10761077
]
10771078

10781079
response = Kernel.ParallelCompiler.compile_to_path(stale, dest, compile_opts)

0 commit comments

Comments
 (0)