Skip to content

Commit

Permalink
Stacktraces: shorter for macro expansion and @Kwargs in Julia 1.10 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp authored Feb 15, 2024
1 parent 5f7233a commit 3e778de
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 34 deletions.
1 change: 0 additions & 1 deletion frontend/components/CellInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,6 @@ const InputContextMenu = ({ on_delete, cell_id, run_cell, skip_as_script, runnin

const prevously_focused_element_ref = useRef(/** @type {Element?} */ (null))
const setOpen = (val) => {
console.error("setOpen", val)
if (val) {
prevously_focused_element_ref.current = document.activeElement
}
Expand Down
46 changes: 23 additions & 23 deletions frontend/components/ErrorMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const StackFrameFilename = ({ frame, cell_id }) => {
if (sep_index != -1) {
const frame_cell_id = frame.file.substr(sep_index + 4, 36)
const a = html`<a
internal-file=${frame.file}
href="#"
onclick=${(e) => {
window.dispatchEvent(
Expand Down Expand Up @@ -58,24 +59,24 @@ export const ParseError = ({ cell_id, diagnostics }) => {
<jlerror>
<header><p>Syntax error</p></header>
<section>
<ol>
${diagnostics.map(
({ message, from, to, line }) =>
html`<li onmouseenter=${() => // NOTE: this could be moved move to `StackFrameFilename`
window.dispatchEvent(new CustomEvent("cell_highlight_range", { detail: { cell_id, from, to }}))
}
onmouseleave=${() =>
window.dispatchEvent(new CustomEvent("cell_highlight_range", { detail: { cell_id, from: null, to: null }}))
}
>
${message}<span>@</span>
<${StackFrameFilename} frame=${{file: "#==#" + cell_id, line}} cell_id=${cell_id} />
</li>`)
}
</ol>
<ol>
${diagnostics.map(
({ message, from, to, line }) =>
html`<li
onmouseenter=${() =>
// NOTE: this could be moved move to `StackFrameFilename`
window.dispatchEvent(new CustomEvent("cell_highlight_range", { detail: { cell_id, from, to } }))}
onmouseleave=${() =>
window.dispatchEvent(new CustomEvent("cell_highlight_range", { detail: { cell_id, from: null, to: null } }))}
>
${message}<span>@</span>
<${StackFrameFilename} frame=${{ file: "#==#" + cell_id, line }} cell_id=${cell_id} />
</li>`
)}
</ol>
</section>
</jlerror>
`;
`
}

export const ErrorMessage = ({ msg, stacktrace, cell_id }) => {
Expand All @@ -102,9 +103,9 @@ export const ErrorMessage = ({ msg, stacktrace, cell_id }) => {
<a
href="#"
onClick=${(e) => {
e.preventDefault()
pluto_actions.split_remote_cell(cell_id, boundaries, true)
}}
e.preventDefault()
pluto_actions.split_remote_cell(cell_id, boundaries, true)
}}
>Split this cell into ${boundaries.length} cells</a
>, or
</p>`
Expand Down Expand Up @@ -228,14 +229,13 @@ export const ErrorMessage = ({ msg, stacktrace, cell_id }) => {
: html`<section>
<ol>
${stacktrace.map(
(frame) =>
html`<li>
(frame) =>
html`<li>
<${Funccall} frame=${frame} />
<span>@</span>
<${StackFrameFilename} frame=${frame} cell_id=${cell_id} />
${frame.inlined ? html`<span>[inlined]</span>` : null}
</li>`
)}
)}
</ol>
</section>`}
</jlerror>`
Expand Down
37 changes: 27 additions & 10 deletions src/runner/PlutoRunner/src/PlutoRunner.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,16 @@ end

const has_julia_syntax = isdefined(Base, :JuliaSyntax) && fieldcount(Base.Meta.ParseError) == 2

function frame_is_from_plutorunner(frame::Base.StackTraces.StackFrame)
if frame.linfo isa Core.MethodInstance
frame.linfo.def.module === PlutoRunner
else
endswith(String(frame.file), "PlutoRunner.jl")
end
end

frame_is_from_usercode(frame::Base.StackTraces.StackFrame) = occursin("#==#", String(frame.file))

function format_output(val::CapturedException; context=default_iocontext)
if has_julia_syntax && val.ex isa Base.Meta.ParseError && val.ex.detail isa Base.JuliaSyntax.ParseError
dict = convert_parse_error_to_dict(val.ex.detail)
Expand All @@ -1230,21 +1240,23 @@ function format_output(val::CapturedException; context=default_iocontext)

# function_wrap_index = findfirst(f -> occursin("function_wrapped_cell", String(f.func)), stack)

function_wrap_index = findlast(f -> occursin("#==#", String(f.file)), stack)

if function_wrap_index === nothing
for _ in 1:2
until = findfirst(b -> b.func == :eval, reverse(stack))
stack = until === nothing ? stack : stack[1:end - until]
end
function_wrap_index = findlast(frame_is_from_usercode, stack)
internal_index = findfirst(frame_is_from_plutorunner, stack)

limit = if function_wrap_index !== nothing
function_wrap_index
elseif internal_index !== nothing
internal_index - 1
else
stack = stack[1:function_wrap_index]
nothing
end
stack_relevant = stack[1:something(limit, end)]

pretty = map(stack) do s
pretty = map(stack_relevant) do s
Dict(
:call => pretty_stackcall(s, s.linfo),
:inlined => s.inlined,
:from_c => s.from_c,
:file => basename(String(s.file)),
:path => String(s.file),
:line => s.line,
Expand Down Expand Up @@ -1290,7 +1302,12 @@ end

function pretty_stackcall(frame::Base.StackFrame, linfo::Core.MethodInstance)
if linfo.def isa Method
sprint(Base.show_tuple_as_call, linfo.def.name, linfo.specTypes)
@static if isdefined(Base.StackTraces, :show_spec_linfo) && hasmethod(Base.StackTraces.show_spec_linfo, Tuple{IO, Base.StackFrame})
sprint(Base.StackTraces.show_spec_linfo, frame; context=:backtrace => true)

else
split(string(frame), " at ") |> first
end
else
sprint(Base.show, linfo)
end
Expand Down

0 comments on commit 3e778de

Please sign in to comment.