Hi Luigi,

Any errors in a "texlua" script only print an error message, not a full
traceback. Demonstration:

    $ cat ./texlua-traceback.lua
    #!/usr/bin/env texlua
    local function test_function()
        error("Unhandled error")
    end

    test_function()

    $ texlua ./texlua-traceback.lua
    ./texlua-traceback.lua:3: Unhandled error

This is because if a texlua script fails, "luainit.c" calls
"lua_traceback()" without printing the message that it produces. I've
attached a patch that changes it so that a full traceback message is
printed, meaning that running the above example now produces the
following output:

    $ texlua ./texlua-traceback.lua
    ./texlua-traceback.lua:3: Unhandled error
    stack traceback:
            [C]: in function 'error'
            ./texlua-traceback.lua:3: in local 'test_function'
            ./texlua-traceback.lua:6: in main chunk

Thanks,
-- Max
From 2996f54aae619f70bcce182717bfd87163330f0e Mon Sep 17 00:00:00 2001
From: Max Chernoff <[email protected]>
Date: Mon, 1 Dec 2025 21:52:47 -0700
Subject: [PATCH] Fix error handling in "texlua" mode

Before, the code just called "lua_traceback()", which doesn't itself
print any messages. This commit modifies the code to push the traceback
function onto the stack and use it as the message handler for
"lua_pcall()", so that any errors in a "texlua" script will print a full
traceback instead of just a single-line message.
---
 source/texk/web2c/luatexdir/lua/luainit.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/source/texk/web2c/luatexdir/lua/luainit.c b/source/texk/web2c/luatexdir/lua/luainit.c
index 0f1ed8ca9..9f99cf582 100644
--- a/source/texk/web2c/luatexdir/lua/luainit.c
+++ b/source/texk/web2c/luatexdir/lua/luainit.c
@@ -1127,9 +1127,10 @@ void lua_initialize(int ac, char **av)
                 exit(1);
             }
             init_tex_table(Luas);
-            if (lua_pcall(Luas, 0, 0, 0)) {
+            lua_pushcfunction(Luas, lua_traceback);
+            lua_insert(Luas, -2);
+            if (lua_pcall(Luas, 0, 0, -2)) {
                 fprintf(stdout, "%s\n", lua_tostring(Luas, -1));
-                lua_traceback(Luas);
              /*tex lua_close(Luas); */
                 exit(1);
             } else {
@@ -1138,6 +1139,7 @@ void lua_initialize(int ac, char **av)
                 /*tex lua_close(Luas); */
                 exit(0);
             }
+            lua_remove(Luas, -1);
         }
         /*tex a normal tex run */
         init_tex_table(Luas);
@@ -1149,11 +1151,13 @@ void lua_initialize(int ac, char **av)
             fprintf(stdout, "%s\n", lua_tostring(Luas, -1));
             exit(1);
         }
-        if (lua_pcall(Luas, 0, 0, 0)) {
+        lua_pushcfunction(Luas, lua_traceback);
+        lua_insert(Luas, -2);
+        if (lua_pcall(Luas, 0, 0, -2)) {
             fprintf(stdout, "%s\n", lua_tostring(Luas, -1));
-            lua_traceback(Luas);
             exit(1);
         }
+        lua_remove(Luas, -1);
         if (!input_name) {
             get_lua_string("texconfig", "jobname", &input_name);
         }
-- 
2.52.0

_______________________________________________
dev-luatex mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to