From 2f84c1e9456d896af03fd6e7902c413aae467f5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 31 Aug 2024 20:05:59 +0200 Subject: [PATCH] Store warnings emitted from external resources --- lib/mix/lib/mix/compilers/elixir.ex | 27 ++++++++++++++---- .../test/mix/tasks/compile.elixir_test.exs | 28 ++++++++++++++++++- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/lib/mix/lib/mix/compilers/elixir.ex b/lib/mix/lib/mix/compilers/elixir.ex index 826a97cee5d..464fdcf47be 100644 --- a/lib/mix/lib/mix/compilers/elixir.ex +++ b/lib/mix/lib/mix/compilers/elixir.ex @@ -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 diff --git a/lib/mix/test/mix/tasks/compile.elixir_test.exs b/lib/mix/test/mix/tasks/compile.elixir_test.exs index 02dfbd96e8a..bc8ee4f2a5a 100644 --- a/lib/mix/test/mix/tasks/compile.elixir_test.exs +++ b/lib/mix/test/mix/tasks/compile.elixir_test.exs @@ -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 @@ -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)