Skip to content

debug.traceback failed on anonymous function #1665

Open

Description

@ChouUn

let [result] = string.gsub(trace, "(%S+)%.lua:(%d+)", (file, line) =>

When anonymous functions are included in TS code, stack.traceback printing doesn't work exactly as expected, such as in the form SOME_PATH\dist\main.ts:43: in function <SOME_PATH\dist\main.lua:65>.
This is because the "<" escapes as a non-whitespace character matching the %S, causing it to be considered part of the path.

On Windows, you can quickly fix this by changing the pattern to ([^%s<]+)%.lua:(%d+).
This isn't the most elegant fix, considering that "<" can be part of a filename on Unix-like systems.

Additional, my guess is that if there are spaces in the entire path, it may also cause unexpected behavior because of %S.

However, collecting the paths to build a regular expression can be quite risky.
Do you have any suggestions?

Activity

  1. Perryvw commented on Nov 2, 2025

    @Perryvw
    Member

    Which Lua runtime are you using? In my quick check I don't see the <> notation used in Lua 5.3 or 5.4.

    And do you have some example code that actually produces the problem? I've been using this to test:

    function bla()
      local f = function()
        error("hi")
      end
      f()
    end
    
    bla()
  2. ChouUn commented on Feb 27, 2026

    @ChouUn
    ContributorAuthor

    Hi @Perryvw, I've added a regression test for this in PR #1666.

    If you'd like a quick way to reproduce the issue, nested IIFEs work well — the anonymous function definitions show up as <file.lua:N> in debug.traceback(), and those lines do have sourcemap entries:

    (() => {
        (() => {
            print(debug.traceback());
        })();
    })();

    Hope that helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions