denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
97.77k stars 5.38k forks source link

[jupyter-kernel] stack trace line numbers are wrong #20643

Open skybrian opened 1 year ago

skybrian commented 1 year ago

To reproduce:

console.log("1");
console.log("2");
console.log("3");
throw new Error("fail");

The output is a stack trace:

Stack trace:
Error: fail
    at <anonymous>:5:7

So this is off by 1.

But this gets worse for classes:

class Camera {
    constructor(public x: number, public y: number, public dir: number) {}

    get pos(): [number, number] {
        return [this.x, this.y];
    }

    get dirVector(): [number, number] {
        return [Math.cos(this.dir * Math.PI / 180), Math.sin(this.dir * Math.PI / 180)];
    }

    get left(): [number, number] {
        const half_width = params.camera_width / 2;
        const dir = (this.dir - half_width) * Math.PI / 180;
        return [Math.cos(dir), Math.sin(dir)];
    }

    get right(): [number, number] {
        const half_width = params.camera_width / 2;
        const dir = (this.dir + half_width) * Math.PI / 180;
        return [Math.cos(dir), Math.sin(dir)];
    }
}

throw new Error("fail");

The stack trace I get is:

Stack trace:
Error: fail
    at <anonymous>:40:7

The actual line number is 25. I suspect the JavaScript line number isn't getting run through a sourcemap to get the TypeScript line number?

(Also, this is more of a feature request, but it would be friendlier to reproduce the source code in the error message like Python does, since cells don't have line numbers turned on by default.)

nayeemrmn commented 1 year ago

This is also a problem with deno repl which the jupyter kernel uses. I don't think think there's a pre-existing tracking issue

bartlomieju commented 1 year ago

@nayeemrmn could you handle source mapping in REPL?