Skip to content

Commit 2f84c1e

Browse files
committed
Store warnings emitted from external resources
1 parent 31f3a74 commit 2f84c1e

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

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

+22-5
Original file line numberDiff line numberDiff line change
@@ -744,21 +744,38 @@ defmodule Mix.Compilers.Elixir do
744744
end
745745

746746
defp apply_warnings(sources, runtime_warnings, compile_warnings) do
747+
cwd = File.cwd!()
747748
runtime_group = Enum.group_by(runtime_warnings, & &1.source)
748749
compile_group = Enum.group_by(compile_warnings, & &1.source)
749750

750751
for {source_path, source_entry} <- sources, into: %{} do
751-
key = Path.absname(source_path)
752-
753752
source(
754753
runtime_warnings: runtime_warnings,
755-
compile_warnings: compile_warnings
754+
compile_warnings: compile_warnings,
755+
external: external
756756
) = source_entry
757757

758+
keys = [
759+
Path.absname(source_path, cwd)
760+
| Enum.map(external, &(&1 |> elem(0) |> Path.absname(cwd)))
761+
]
762+
763+
runtime_warnings =
764+
case Enum.flat_map(keys, &Map.get(runtime_group, &1, [])) do
765+
[] -> runtime_warnings
766+
runtime_warnings -> runtime_warnings
767+
end
768+
769+
compile_warnings =
770+
case Enum.flat_map(keys, &Map.get(compile_group, &1, [])) do
771+
[] -> compile_warnings
772+
compile_warnings -> compile_warnings
773+
end
774+
758775
{source_path,
759776
source(source_entry,
760-
runtime_warnings: Map.get(runtime_group, key, runtime_warnings),
761-
compile_warnings: Map.get(compile_group, key, compile_warnings)
777+
runtime_warnings: runtime_warnings,
778+
compile_warnings: compile_warnings
762779
)}
763780
end
764781
end

lib/mix/test/mix/tasks/compile.elixir_test.exs

+27-1
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,6 @@ defmodule Mix.Tasks.Compile.ElixirTest do
983983
# is not valid but let's ensure we don't crash
984984
@external_resource "lib"
985985
@external_resource "lib/a.eex"
986-
987986
@external_resource #{inspect(tmp)}
988987
def a, do: :ok
989988
end
@@ -1032,6 +1031,33 @@ defmodule Mix.Tasks.Compile.ElixirTest do
10321031
File.rm(tmp_path("c.eex"))
10331032
end
10341033

1034+
test "tracks warnings from external resources" do
1035+
in_fixture("no_mixfile", fn ->
1036+
Mix.Project.push(MixTest.Case.Sample)
1037+
File.touch!("lib/a.eex")
1038+
1039+
File.write!("lib/a.ex", """
1040+
defmodule A do
1041+
@external_resource "lib/a.eex"
1042+
IO.warn("oops", file: Path.absname("lib/a.eex"), line: 13)
1043+
end
1044+
""")
1045+
1046+
# Compiles with missing external resources
1047+
file = Path.absname("lib/a.eex")
1048+
1049+
assert capture_io(:stderr, fn ->
1050+
assert {:ok, [%Mix.Task.Compiler.Diagnostic{file: ^file, position: 13}]} =
1051+
Mix.Tasks.Compile.Elixir.run([])
1052+
1053+
assert {:noop, [%Mix.Task.Compiler.Diagnostic{file: ^file, position: 13}]} =
1054+
Mix.Tasks.Compile.Elixir.run(["--all-warnings"])
1055+
end) =~ "oops"
1056+
1057+
purge([A])
1058+
end)
1059+
end
1060+
10351061
test "recompiles modules with exports tracking" do
10361062
in_fixture("no_mixfile", fn ->
10371063
Mix.Project.push(MixTest.Case.Sample)

0 commit comments

Comments
 (0)