ITADN

Incorrect line in stacktrace when try to get it by prepareStackTrace

#783Openalbion9919 创建于 2026-03-25
bugpending triage
A
albion9919commented
### Acknowledgements - [x] I read the documentation and searched existing issues to avoid duplicates - [x] I understand this is a **bug tracker** and anything other than a proven bug will be closed - [x] I understand this is a free project and relies on community contributions - [x] I read and understood the [Contribution guide](https://github.com/privatenumber/tsx/blob/master/CONTRIBUTING.md) ### Minimal reproduction URL https://github.com/albion9919 ### Problem & expected behavior (under 200 words) I am tring to get line for my logger. A minimal code: test.ts ```ts function getLine() { let line; const _prepareStackTrace = Error.prepareStackTrace; Error.prepareStackTrace = (_, stack) => { console.log(stack.map(s => s.toString())) line = stack.filter(s => !s.getFileName()?.endsWith(import.meta.url))[0]?.toString(); }; const e = new Error(); e.stack; Error.prepareStackTrace = _prepareStackTrace; return line } const t =1; const t2 = 2; console.log(getLine()) throw Error('test') ``` This code returnes me incorrect line 1. I've tried to add --enable-source-maps, inlineSourceMap/inlineSources, process.setEnableSourceMap. An Interesting detail - error returns correct line. Repro: ```bash cli % tsx test.ts [ 'getLine (file:///.../test.ts:1:358)', 'file:///...i/test.ts:1:490', 'ModuleJob.run (node:internal/modules/esm/module_job:222:25)', 'async ModuleLoader.import (node:internal/modules/esm/loader:316:24)', 'async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:123:5)' ] ModuleJob.run (node:internal/modules/esm/module_job:222:25) .../test.ts:19 throw Error('test') ^ Error: test at <anonymous> (.../test.ts:19:7) at ModuleJob.run (node:internal/modules/esm/module_job:222:25) at async ModuleLoader.import (node:internal/modules/esm/loader:316:24) at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:123:5) Node.js v20.16.0 cli % node --enable-source-maps --import tsx test.ts [ 'getLine (file:///.../test.ts:1:358)', 'file:///.../test.ts:1:490', 'ModuleJob.run (node:internal/modules/esm/module_job:222:25)', 'async ModuleLoader.import (node:internal/modules/esm/loader:316:24)', 'async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:123:5)' ] ModuleJob.run (node:internal/modules/esm/module_job:222:25) test.ts:19 throw Error('test') ^ Error: test at <anonymous> (.../test.ts:19:7) at ModuleJob.run (node:internal/modules/esm/module_job:222:25) at async ModuleLoader.import (node:internal/modules/esm/loader:316:24) at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:123:5) Node.js v20.16.0 ``` Maybe I did something wrong ? ### Bugs are expected to be fixed by those affected by it - [ ] I'm interested in working on this issue ### Compensating engineering work will speed up resolution and support the project - [ ] I'm willing to offer $10 for financial support
0 条评论