mvanle
mvanle

Reputation: 2015

TCL stack trace not showing desired error line number

The -errorline element of the return options dictionary for the following TCL script is "2":

puts [info patchlevel]

try {
    error "this is an error"
} trap {} {result ropts} {
    puts $result
    puts $ropts
}

How do I get the stacktrace to display the line number in the source file where the error was actually raised (ie. line 4 instead of 2) ?

Example screenshot:

enter image description here

Upvotes: 1

Views: 306

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137797

Tcl often has that information available, but doesn't use it.

It has the information available because you have a chance to retrieve it with info frame and getbytecode (which is in the tcl::unsupported namespace, mostly because we reserve the right to change how the bytecodes themselves work at any time). I'm not quite sure if that would work in your specific case, but if you put your test code in a procedure then it definitely would. (There are complexities here with fragility that I don't fully understand.)

It doesn't use it because, for backward-compatibility with existing tooling, it uses the line numbers it was using prior to the creation of the machinery to support info frame. Those line numbers are relative to the local script fragment (which is whatever reports the line number in the error info trace first); in this case, that is the body of the try.

I don't like that it works like that at all. However, changing things is a bit tricky because we'd need to also figure out what else to report and what to do in the cases where the information genuinely isn't available (such as for automatically-generated code where things are assembled from many strings from many lines).

Upvotes: 1

Related Questions