diff --git a/src/lualib/SourceMapTraceBack.ts b/src/lualib/SourceMapTraceBack.ts index 47c2c8739..777c220c1 100644 --- a/src/lualib/SourceMapTraceBack.ts +++ b/src/lualib/SourceMapTraceBack.ts @@ -49,7 +49,12 @@ export function __TS__SourceMapTraceBack(this: void, fileName: string, sourceMap return `${file}:${line}`; }; - let [result] = string.gsub(trace, "(%S+)%.lua:(%d+)", (file, line) => + // Rewrite stack trace frames from "{PATH}.lua:{LINE}" to "{PATH}.ts:{ORIGINAL_LINE}". + // https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1665 + // Avoid matching anonymous function stack entries like `in function <...>` + // by excluding `<` before the file path. + // TODO: This will still fail for paths containing spaces. + let [result] = string.gsub(trace, "([^%s<]+)%.lua:(%d+)", (file, line) => replacer(`${file}.lua`, `${file}.ts`, line) ); diff --git a/test/unit/error.spec.ts b/test/unit/error.spec.ts index 05399d492..af69d7e1d 100644 --- a/test/unit/error.spec.ts +++ b/test/unit/error.spec.ts @@ -345,6 +345,56 @@ test("still works without debug module", () => { }); }); +// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1665 +test("sourceMapTraceback maps anonymous function locations in .lua files (#1665)", () => { + // Nested IIFEs produce anonymous function notation in traceback. + // Old pattern (%S+)%.lua:(%d+) captures "", + // failing the sourcemap lookup. Fix: ([^%s<]+) excludes "<". + + // mapping is copied from the emitted Lua, not invented. + const mapping = `{["5"] = 1,["6"] = 2,["7"] = 3,["8"] = 4,["9"] = 3,["10"] = 2,["11"] = 1}`; + + // Test harness executes via luaL_dostring (chunk names are [string "..."]), so we mock a file-based traceback. + const fakeTraceback = [ + "stack traceback:", + "\tmain.lua:8: in function ", + "\tmain.lua:7: in function ", + "\t[C]: in ?", + ].join("\n"); + + // Inject sourcemap for "main.lua" and mock debug.traceback to return file-based frames. + const header = ` + __TS__sourcemap = { ["main.lua"] = ${mapping} }; + local __real_tb = debug.traceback + debug.traceback = function() return ${JSON.stringify(fakeTraceback)} end + `; + + const builder = util.testFunction` + return (() => { + return (() => { + return (debug.traceback as (this: void) => string)(); + })(); + })(); + ` + .setLuaHeader(header) + .setOptions({ sourceMapTraceback: true }); + + const lua = builder.getMainLuaCodeChunk(); + // Sanity check: emitted code registers the same mapping literal we inject above. + expect(lua).toContain(`__TS__SourceMapTraceBack(debug.getinfo(1).short_src, ${mapping});`); + + const result = builder.getLuaExecutionResult(); + expect(result).toEqual(expect.any(String)); + // Both `main.lua:N` and `` frames should be rewritten using the sourcemap. + expect(result).not.toContain("main.lua"); + // Regular line references + expect(result).toContain("\tmain.ts:4:"); + expect(result).toContain("\tmain.ts:3:"); + // Anonymous function references must keep <> format + expect(result).toContain(""); + expect(result).toContain(""); +}); + util.testEachVersion( "error stacktrace omits constructor and __TS_New", () => util.testFunction`