Skip to content

Commit 4a69a00

Browse files
numsojosevalim
authored andcommitted
Report the correct line number when raising inside a macro (#10040)
1 parent a9ad1f1 commit 4a69a00

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

lib/elixir/lib/kernel/parallel_compiler.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,12 @@ defmodule Kernel.ParallelCompiler do
652652
end
653653
end
654654

655+
defp get_line(file, _reason, [{_, _, _, [file: 'expanding macro']}, {_, _, _, info} | _]) do
656+
if Keyword.get(info, :file) == to_charlist(Path.relative_to_cwd(file)) do
657+
Keyword.get(info, :line)
658+
end
659+
end
660+
655661
defp get_line(file, _reason, [{_, _, _, info} | _]) do
656662
if Keyword.get(info, :file) == to_charlist(Path.relative_to_cwd(file)) do
657663
Keyword.get(info, :line)

lib/mix/test/mix/tasks/compile_test.exs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,39 @@ defmodule Mix.Tasks.CompileTest do
146146
end)
147147
end
148148

149+
test "calling raise inside a macro returns a diagnostic with a position" do
150+
in_fixture("no_mixfile", fn ->
151+
File.write!("lib/a.ex", """
152+
defmodule A do
153+
defmacro custom_macro do
154+
raise "error"
155+
end
156+
end
157+
""")
158+
159+
File.write!("lib/b.ex", """
160+
defmodule B do
161+
require A
162+
A.custom_macro()
163+
end
164+
""")
165+
166+
file = Path.absname("lib/b.ex")
167+
168+
ExUnit.CaptureIO.capture_io(fn ->
169+
assert {:error, [diagnostic]} = Mix.Task.run("compile", ["--return-errors"])
170+
171+
assert %Mix.Task.Compiler.Diagnostic{
172+
file: ^file,
173+
severity: :error,
174+
position: 3,
175+
message: "** (RuntimeError) error\n expanding macro: A.custom_macro/0" <> _,
176+
compiler_name: "Elixir"
177+
} = diagnostic
178+
end)
179+
end)
180+
end
181+
149182
test "returns syntax error from an Erlang file when --return-errors is set" do
150183
in_fixture("no_mixfile", fn ->
151184
import ExUnit.CaptureIO

0 commit comments

Comments
 (0)