Skip to content

Commit 4fc5b77

Browse files
authored
Code.with_diagnostics/2 formats paths as relative (#12590)
* Code.with_diagnostics/2 formats paths as relative * Fix in :elixir_errors instead * Fix it in parallel checker too
1 parent d4d7c8b commit 4fc5b77

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

lib/elixir/lib/kernel/parallel_compiler.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,11 +632,11 @@ defmodule Kernel.ParallelCompiler do
632632
spawn_workers(queue, spawned, waiting, files, result, warnings, errors, state)
633633

634634
{:diagnostic, %{severity: :warning} = diagnostic} ->
635-
warnings = [diagnostic | warnings]
635+
warnings = [Module.ParallelChecker.format_diagnostic_file(diagnostic) | warnings]
636636
wait_for_messages(queue, spawned, waiting, files, result, warnings, errors, state)
637637

638638
{:diagnostic, %{severity: :error} = diagnostic} ->
639-
errors = [diagnostic | errors]
639+
errors = [Module.ParallelChecker.format_diagnostic_file(diagnostic) | errors]
640640
wait_for_messages(queue, spawned, waiting, files, result, warnings, errors, state)
641641

642642
{:file_ok, child_pid, ref, file, lexical} ->

lib/elixir/lib/module/parallel_checker.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ defmodule Module.ParallelChecker do
169169
defp collect_results(count, diagnostics) do
170170
receive do
171171
{:diagnostic, diagnostic} ->
172+
diagnostic = format_diagnostic_file(diagnostic)
172173
collect_results(count, [diagnostic | diagnostics])
173174

174175
{__MODULE__, _module, new_diagnostics} ->
@@ -287,6 +288,11 @@ defmodule Module.ParallelChecker do
287288
end
288289
end
289290

291+
@doc false
292+
def format_diagnostic_file(%{file: file} = diagnostic) do
293+
%{diagnostic | file: file && Path.absname(file)}
294+
end
295+
290296
## Warning helpers
291297

292298
defp group_warnings(warnings) do

lib/elixir/src/elixir_errors.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ print_diagnostic(#{severity := Severity, message := Message, stacktrace := Stack
3838
emit_diagnostic(Severity, Position, File, Message, Stacktrace) ->
3939
Diagnostic = #{
4040
severity => Severity,
41-
file => if File =:= nil -> nil; true -> filename:absname(File) end,
41+
file => File,
4242
position => Position,
4343
message => unicode:characters_to_binary(Message),
4444
stacktrace => Stacktrace

lib/elixir/test/elixir/code_test.exs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,27 @@ defmodule CodeTest do
198198
:code.purge(CodeTest.CheckerWarning)
199199
:code.delete(CodeTest.CheckerWarning)
200200
end
201+
202+
test "formats diagnostic file paths as relatives" do
203+
{_, diagnostics} =
204+
Code.with_diagnostics(fn ->
205+
try do
206+
Code.eval_string("x", [])
207+
rescue
208+
e -> e
209+
end
210+
end)
211+
212+
assert [
213+
%{
214+
message: "undefined variable \"x\"",
215+
position: 1,
216+
file: "nofile",
217+
stacktrace: [],
218+
severity: :error
219+
}
220+
] = diagnostics
221+
end
201222
end
202223

203224
describe "eval_quoted/1" do

0 commit comments

Comments
 (0)