Missing stacktrace information on panics in imported files
One bigger problem I have now that my tengo scripts are growing in size is that many times the stacktrace from a runtime error seems to be missing file and line info from the most recent frames.
I believe this happens only when the code causing a panic is in imported modules, possibly depth > 1 e.g. imported module in imported module. The stacktrace info looks like this:
panic: Runtime Error: not callable: map
at -
at /thepath/test.tengo:98:6
at (main):16:18
In this case, the error was caused by code in a totally different tengo file whose exported function was called by test.tengo:98.
It can have even multple "at -" lines depending on the function call or module depth. This makes it extremely hard to find where the error was generated. I have to sprinkle prints all over the file to narrow down the exact location.
It would be great if stacktrace info was available at all times.
I tested tengo CLI to reproduce your problem but no luck, stack trace is correct. Could you please share some (minimal) code to reproduce the problem again and debug it?
go run cmd/tengo/main.go -resolve testdata/cli/test.tengo
Runtime Error: not callable: map
at /home/verigraf/go/src/github.com/ozanh/tengo/testdata/cli/two/four/four.tengo:4:9
at /home/verigraf/go/src/github.com/ozanh/tengo/testdata/cli/three.tengo:4:16
at /home/verigraf/go/src/github.com/ozanh/tengo/testdata/cli/two/two.tengo:4:16
at /home/verigraf/go/src/github.com/ozanh/tengo/testdata/cli/one.tengo:5:16
at test.tengo:9:9
exit status 1
Hi @ozanh
I've come up with a minimal example to reproduce:
- make a test.tengo with the following content:
x := undefined
export func() { x() }
Run it in the app via NewScript() and import the file there or I guess from the cli you need to make another .tengo file first that imports test.tengo and calls the returned function.
fn := import("test")
fn()
I noticed that the stacktraces are missing when the declared variable x is outside the exported function. If it is inside, the stacktrace is ok.
I think I found the root cause, not the solution yet. It is about closures. Module import is not very different than a creating a function with a closure, though.
func() {
a := undefined
return func(){
a()
}()
}()
Runtime Error: not callable: undefined
at -
at (main):3:9
at (main):1:1
Edit
I found a solution @d5, it is about OpClosure in VM. OpClosure does not add source map to newly created compiled function because of that it cannot determine file pos when there is an error in OpCall. After my fix stack becomes normal like this;
Runtime Error: not callable: undefined
at (main):4:3
at (main):3:9
at (main):1:1
Should I create a PR or am I missing something?