Fix handling of erlang syntax errors (#2800)

This commit is contained in:
Jean Klingler 2024-09-30 16:31:31 +09:00 committed by GitHub
parent c374ac6a4b
commit 3470f2de48
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 42 additions and 27 deletions

View file

@ -785,29 +785,10 @@ defmodule Livebook.Runtime.Evaluator do
end
defp make_snippet(code, location) do
start_line = 1
start_column = 1
line = :erl_anno.line(location)
case :erl_anno.column(location) do
:undefined ->
nil
column ->
lines = :string.split(code, "\n", :all)
snippet = :lists.nth(line - start_line + 1, lines)
offset =
if line == start_line do
column - start_column
else
column - 1
end
case :string.trim(code, :leading) do
[] -> nil
_ -> %{content: snippet, offset: offset}
end
if :erl_anno.column(location) != :undefined and :string.trim(code, :leading) != [] do
line = :erl_anno.line(location)
lines = :string.split(code, "\n", :all)
:lists.nth(line, lines)
end
end

View file

@ -1373,22 +1373,56 @@ defmodule Livebook.Runtime.EvaluatorTest do
# Incomplete input
Evaluator.evaluate_code(evaluator, :erlang, "X =", :code_1, [])
assert_receive {:runtime_evaluation_response, :code_1, error(message), metadata()}
assert "\e[31m** (TokenMissingError)" <> _ = message
assert clean_message(message) === """
** (TokenMissingError) token missing on nofile:1:4:
error: syntax error before:
1 X =
^
nofile:1:4\
"""
# Parser error
Evaluator.evaluate_code(evaluator, :erlang, "X ==/== a.", :code_2, [])
assert_receive {:runtime_evaluation_response, :code_2, error(message), metadata()}
assert "\e[31m** (SyntaxError)" <> _ = message
assert clean_message(message) === """
** (SyntaxError) invalid syntax found on nofile:1:5:
error: syntax error before: /=
1 X ==/== a.
^
nofile:1:5\
"""
# Tokenizer error
Evaluator.evaluate_code(evaluator, :erlang, "$a$", :code_3, [])
assert_receive {:runtime_evaluation_response, :code_3, error(message), metadata()}
assert "\e[31m** (SyntaxError)" <> _ = message
assert clean_message(message) === """
** (SyntaxError) invalid syntax found on nofile:1:3:
error: unterminated character
1 $a$
^
nofile:1:3\
"""
# Erlang exception
Evaluator.evaluate_code(evaluator, :erlang, "list_to_binary(1).", :code_4, [])
assert_receive {:runtime_evaluation_response, :code_4, error(message), metadata()}
assert "\e[31mexception error: bad argument" <> _ = message
assert clean_message(message) === """
exception error: bad argument
in function list_to_binary/1
called as list_to_binary(1)
*** argument 1: not an iolist term
in call from erl_eval:do_apply/7 (erl_eval.erl, line 900)\
"""
end
end