Skip to content

fix debug.traceback failing on anonymous function (#1665)#1666

Open
ChouUn wants to merge 4 commits intoTypeScriptToLua:masterfrom
ChouUn:master
Open

fix debug.traceback failing on anonymous function (#1665)#1666
ChouUn wants to merge 4 commits intoTypeScriptToLua:masterfrom
ChouUn:master

Conversation

@ChouUn
Copy link

@ChouUn ChouUn commented Oct 28, 2025

more details in #1665

Copilot AI review requested due to automatic review settings October 28, 2025 23:02
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a bug in the source map traceback regex pattern to avoid incorrectly matching anonymous function stack entries. The regex is updated to exclude the < character from file path matching, preventing matches against patterns like in function <...> that appear in Lua stack traces.

  • Updated regex pattern from (%S+) to ([^%s<]+) to exclude < character
  • Added comprehensive comments explaining the fix and its limitations

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

…ypeScriptToLua#1665)

Nested IIFEs produce <file.lua:N> notation in debug.traceback().
The old pattern (%S+) captured the "<" prefix, causing sourcemap
lookup to fail for anonymous function definition locations.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
})();
`
.setLuaHeader(
`__TS__sourcemap = { ["main.lua"] = {["3"] = 7, ["4"] = 8, ["5"] = 9, ["6"] = 8} }\n` +
Copy link
Member

@Perryvw Perryvw Feb 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you overriding the source map here? Shouldn't tstl already produce the sourcemap due to the sourcmaptraceback below?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In our unit test harness we execute Lua via luaL_dostring, so the runtime traceback uses string chunk names ([string "..."]) rather than file-based paths. The traceback looks like:

stack traceback:
  [string "local function __TS__ArrayAt(self, relativeIn..."]:2222: in function 'debug.traceback'
  (...tail calls...)
  [string "..."]:3: in main chunk

Because of this, we don’t get frames like main.lua:5 or anonymous-function locations like <main.lua:4> from real execution, so the regression for #1665 can’t be exercised end-to-end without mocking.

That’s why the test overrides debug.traceback to return a file-based traceback containing <main.lua:N> entries, and also injects a matching __TS__sourcemap keyed by "main.lua" so the sourcemap lookup hits the same file name as the mocked traceback.

})();
`
.setLuaHeader(
`__TS__sourcemap = { ["main.lua"] = {["3"] = 7, ["4"] = 8, ["5"] = 9, ["6"] = 8} }\n` +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both line 4 and 6 map to line 8 here, which seems strange. If you are overriding the default traceback (which I would rather not do unless absolutely necessary), better pick some different numbers so we can assert that both main.lua:4 and \tmain.lua:6: are mapped to the correct values.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in d61f863:

  • The sourcemap mapping is now copied from the compiler's actual emit rather than hand-crafted, with a sanity check (getMainLuaCodeChunk) to ensure they stay in sync.
  • All mapped values are distinct, so regular (\tmain.ts:4:, \tmain.ts:3:) and anonymous (<main.ts:3>, <main.ts:2>) assertions each verify a unique mapping.
  • Added explicit expect(result).toContain("<main.ts:N>") assertions as requested.

ChouUn and others added 2 commits March 1, 2026 06:41
…ypeScriptToLua#1665)

Use distinct sourcemap values and explicit <main.ts:N> assertions
per reviewer request.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants