bakpakin/Fennel

Line numbers in errors and stack incorrect in `dofile`

clarkeaa opened this issue · 2 comments

This is a simple reproduction of the problem I run into which is making working with fennel very difficult. I haven't seen line numbers ever correct. The names of the functions in the frames are correct though.

reproduction

run.lua:

require("fennel").install().dofile("main.fnl")

main.fnl

; Comment

(fn main []
  (error "hi"))

(main)

invocation:

lua54.exe .\run.lua

fennel version

1.4.0

output

PS C:\Users\ender\dev\fennel\stack-bug> lua54.exe .\run.lua
C:\Users\ender\dev\lua542\lua54.exe: main.fnl:2: hi
stack traceback:
        [C]: in function 'error'
        main.fnl:2: in function <main.fnl:1>
        (...tail calls...)
        .\run.lua:2: in main chunk
        [C]: in ?

Notice that it says the error was on line 2 but it was called on line 4.

This works fine if you run the program with fennel:

$ fennel /tmp/scratch.fnl 
runtime error: hi
stack traceback:
	[C]: in function 'error'
	/tmp/scratch.fnl:4: in function ?
	[C]: in function 'xpcall'
	/home/phil/bin/fennel:6409: in function ?
	[C]: in ?

If you want to run it specifically from Lua, you need to either enable correlate mode, or use fennel.traceback to get the line numbers instead of using the default traceback.

require("fennel").install().dofile("main.fnl", {correlate=true})

https://fennel-lang.org/api#get-fennel-aware-stack-traces

local fennel = require("fennel").install()

xpcall(function() fennel.dofile("scratch.fnl") end,
       function() print(fennel.traceback()) end)

Both these solutions will get you correct line numbers.

FWIW running through fennel isn't an option since I'm using raylib-lua and the entrypoint is hardcoded to be a lua file.