Skip to content

Commit

Permalink
Store warnings emitted from external resources
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Aug 31, 2024
1 parent 31f3a74 commit 2f84c1e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
27 changes: 22 additions & 5 deletions lib/mix/lib/mix/compilers/elixir.ex
Original file line number Diff line number Diff line change
Expand Up @@ -744,21 +744,38 @@ defmodule Mix.Compilers.Elixir do
end

defp apply_warnings(sources, runtime_warnings, compile_warnings) do
cwd = File.cwd!()
runtime_group = Enum.group_by(runtime_warnings, & &1.source)
compile_group = Enum.group_by(compile_warnings, & &1.source)

for {source_path, source_entry} <- sources, into: %{} do
key = Path.absname(source_path)

source(
runtime_warnings: runtime_warnings,
compile_warnings: compile_warnings
compile_warnings: compile_warnings,
external: external
) = source_entry

keys = [
Path.absname(source_path, cwd)
| Enum.map(external, &(&1 |> elem(0) |> Path.absname(cwd)))
]

runtime_warnings =
case Enum.flat_map(keys, &Map.get(runtime_group, &1, [])) do
[] -> runtime_warnings
runtime_warnings -> runtime_warnings
end

compile_warnings =
case Enum.flat_map(keys, &Map.get(compile_group, &1, [])) do
[] -> compile_warnings
compile_warnings -> compile_warnings
end

{source_path,
source(source_entry,
runtime_warnings: Map.get(runtime_group, key, runtime_warnings),
compile_warnings: Map.get(compile_group, key, compile_warnings)
runtime_warnings: runtime_warnings,
compile_warnings: compile_warnings
)}
end
end
Expand Down
28 changes: 27 additions & 1 deletion lib/mix/test/mix/tasks/compile.elixir_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,6 @@ defmodule Mix.Tasks.Compile.ElixirTest do
# is not valid but let's ensure we don't crash
@external_resource "lib"
@external_resource "lib/a.eex"
@external_resource #{inspect(tmp)}
def a, do: :ok
end
Expand Down Expand Up @@ -1032,6 +1031,33 @@ defmodule Mix.Tasks.Compile.ElixirTest do
File.rm(tmp_path("c.eex"))
end

test "tracks warnings from external resources" do
in_fixture("no_mixfile", fn ->
Mix.Project.push(MixTest.Case.Sample)
File.touch!("lib/a.eex")

File.write!("lib/a.ex", """
defmodule A do
@external_resource "lib/a.eex"
IO.warn("oops", file: Path.absname("lib/a.eex"), line: 13)
end
""")

# Compiles with missing external resources
file = Path.absname("lib/a.eex")

assert capture_io(:stderr, fn ->
assert {:ok, [%Mix.Task.Compiler.Diagnostic{file: ^file, position: 13}]} =
Mix.Tasks.Compile.Elixir.run([])

assert {:noop, [%Mix.Task.Compiler.Diagnostic{file: ^file, position: 13}]} =
Mix.Tasks.Compile.Elixir.run(["--all-warnings"])
end) =~ "oops"

purge([A])
end)
end

test "recompiles modules with exports tracking" do
in_fixture("no_mixfile", fn ->
Mix.Project.push(MixTest.Case.Sample)
Expand Down

0 comments on commit 2f84c1e

Please sign in to comment.