Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 8 additions & 18 deletions lib/mix/lib/mix/compilers/elixir.ex
Original file line number Diff line number Diff line change
Expand Up @@ -207,19 +207,17 @@ defmodule Mix.Compilers.Elixir do
modules_diff(modules, removed_modules, all_modules, timestamp)
end

unless_previous_warnings_as_errors(
previous_warnings,
opts,
{:ok, all_warnings, lazy_modules_diff}
)
Mix.Task.Compiler.notify_modules_compiled(lazy_modules_diff)

unless_previous_warnings_as_errors(previous_warnings, opts, {:ok, all_warnings})

{:error, errors, %{runtime_warnings: r_warnings, compile_warnings: c_warnings}, state} ->
# In case of errors, we show all previous warnings and all new ones.
{_, _, sources, _, _, _} = state
errors = Enum.map(errors, &diagnostic/1)
warnings = Enum.map(r_warnings ++ c_warnings, &diagnostic/1)
all_warnings = Keyword.get(opts, :all_warnings, errors == [])
{:error, previous_warnings(sources, all_warnings) ++ warnings ++ errors, nil}
{:error, previous_warnings(sources, all_warnings) ++ warnings ++ errors}
after
Code.compiler_options(previous_opts)
end
Expand Down Expand Up @@ -257,11 +255,7 @@ defmodule Mix.Compilers.Elixir do
all_warnings = Keyword.get(opts, :all_warnings, true)
previous_warnings = previous_warnings(sources, all_warnings)

unless_previous_warnings_as_errors(
previous_warnings,
opts,
{status, previous_warnings, nil}
)
unless_previous_warnings_as_errors(previous_warnings, opts, {status, previous_warnings})
end
end

Expand Down Expand Up @@ -1003,17 +997,13 @@ defmodule Mix.Compilers.Elixir do
File.rm(manifest <> ".checkpoint")
end

defp unless_previous_warnings_as_errors(
previous_warnings,
opts,
{status, all_warnings, modules_diff}
) do
defp unless_previous_warnings_as_errors(previous_warnings, opts, {status, all_warnings}) do
if previous_warnings != [] and opts[:warnings_as_errors] do
message = "Compilation failed due to warnings while using the --warnings-as-errors option"
IO.puts(:stderr, message)
{:error, all_warnings, modules_diff}
{:error, all_warnings}
else
{status, all_warnings, modules_diff}
{status, all_warnings}
end
end

Expand Down
30 changes: 26 additions & 4 deletions lib/mix/lib/mix/compilers/erlang.ex
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,11 @@ defmodule Mix.Compilers.Erlang do

# Get the previous entries from the manifest
timestamp = System.os_time(:second)
entries = read_manifest(manifest)
old_entries = entries = read_manifest(manifest)

# Files to remove are the ones in the manifest but they no longer have a source
removed =
entries
|> Enum.filter(fn {dest, _} -> not List.keymember?(mappings, dest, 2) end)
|> Enum.map(&elem(&1, 0))
for {dest, _} <- entries, not List.keymember?(mappings, dest, 2), do: dest

# Remove manifest entries with no source
Enum.each(removed, &File.rm/1)
Expand Down Expand Up @@ -151,6 +149,17 @@ defmodule Mix.Compilers.Erlang do
# Return status and diagnostics
warnings = manifest_warnings(entries) ++ to_diagnostics(warnings, :warning)

lazy_modules_diff = fn ->
{changed, added} =
stale
|> Enum.map(&elem(&1, 1))
|> Enum.split_with(fn dest -> List.keymember?(old_entries, dest, 0) end)

modules_diff(added, changed, removed, timestamp)
end

Mix.Task.Compiler.notify_modules_compiled(lazy_modules_diff)

case status do
:ok ->
{:ok, warnings}
Expand Down Expand Up @@ -320,4 +329,17 @@ defmodule Mix.Compilers.Erlang do
defp location_to_string({line, column}), do: "#{line}:#{column}:"
defp location_to_string(0), do: ""
defp location_to_string(line), do: "#{line}:"

defp modules_diff(added, changed, removed, timestamp) do
%{
added: Enum.map(added, &module_from_path/1),
changed: Enum.map(changed, &module_from_path/1),
removed: Enum.map(removed, &module_from_path/1),
timestamp: timestamp
}
end

defp module_from_path(path) do
path |> Path.basename() |> Path.rootname() |> String.to_atom()
end
end
25 changes: 9 additions & 16 deletions lib/mix/lib/mix/tasks/compile.elixir.ex
Original file line number Diff line number Diff line change
Expand Up @@ -127,22 +127,15 @@ defmodule Mix.Tasks.Compile.Elixir do

with_logger_app(project, fn ->
Mix.Project.with_build_lock(project, fn ->
{status, warnings, lazy_modules_diff} =
Mix.Compilers.Elixir.compile(
manifest,
srcs,
dest,
cache_key,
Mix.Tasks.Compile.Erlang.manifests(),
Mix.Tasks.Compile.Erlang.modules(),
opts
)

if lazy_modules_diff do
Mix.Task.Compiler.notify_modules_compiled(lazy_modules_diff)
end

{status, warnings}
Mix.Compilers.Elixir.compile(
manifest,
srcs,
dest,
cache_key,
Mix.Tasks.Compile.Erlang.manifests(),
Mix.Tasks.Compile.Erlang.modules(),
opts
)
end)
end)
end
Expand Down
30 changes: 26 additions & 4 deletions lib/mix/test/mix/tasks/compile_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,12 @@ defmodule Mix.Tasks.CompileTest do
File.write!("lib/b.ex", "defmodule B do end")
File.write!("lib/c.ex", "defmodule C do end")

File.mkdir_p!("src")

File.write!("src/a.erl", "-module(a).")
File.write!("src/b.erl", "-module(b).")
File.write!("src/c.erl", "-module(c).")

mix(["deps.compile"])

parent = self()
Expand All @@ -433,8 +439,9 @@ defmodule Mix.Tasks.CompileTest do
assert_receive {^port, {:data, "ok\n"}}, timeout
send(parent, :mix_started)

assert_receive {^port, {:data, output}}, timeout
send(parent, {:output, output})
assert_receive {^port, {:data, output_erl}}, timeout
assert_receive {^port, {:data, output_ex}}, timeout
send(parent, {:output, output_erl <> output_ex})
end)

assert_receive :mix_started, timeout
Expand All @@ -445,6 +452,11 @@ defmodule Mix.Tasks.CompileTest do
assert_receive {:output, output}, timeout

assert output == """
Received :modules_compiled with
added: [:a, :b, :c], changed: [], removed: []
app: :with_reloader
build_scm: Mix.SCM.Path
os_pid: "#{os_pid}"
Received :modules_compiled with
added: [A, B, C], changed: [], removed: []
app: :with_reloader
Expand All @@ -454,10 +466,14 @@ defmodule Mix.Tasks.CompileTest do

# Changed
File.write!("lib/a.ex", "defmodule A do @moduledoc false end")
File.write!("src/a.erl", "-module(a). -export([a/0]). a() -> ok.")
File.touch!("src/a.erl", System.os_time(:second) + 10)
# Removed
File.rm!("lib/b.ex")
File.rm!("src/b.erl")
# New
File.write!("lib/d.ex", "defmodule D do end")
File.write!("src/d.erl", "-module(d).")

spawn_link(fn ->
port =
Expand All @@ -473,8 +489,9 @@ defmodule Mix.Tasks.CompileTest do
assert_receive {^port, {:data, "ok\n"}}, timeout
send(parent, :mix_started)

assert_receive {^port, {:data, output}}, timeout
send(parent, {:output, output})
assert_receive {^port, {:data, output_erl}}, timeout
assert_receive {^port, {:data, output_ex}}, timeout
send(parent, {:output, output_erl <> output_ex})
end)

assert_receive :mix_started, timeout
Expand All @@ -485,6 +502,11 @@ defmodule Mix.Tasks.CompileTest do
assert_receive {:output, output}, timeout

assert output == """
Received :modules_compiled with
added: [:d], changed: [:a], removed: [:b]
app: :with_reloader
build_scm: Mix.SCM.Path
os_pid: "#{os_pid}"
Received :modules_compiled with
added: [D], changed: [A], removed: [B]
app: :with_reloader
Expand Down
Loading