Skip to content

Commit c7a105f

Browse files
authored
Improve stack trace when debugging required modules (#238)
Required modules run in their own threads in Lute, and there isn't an easy way to automatically "inherit" a stack trace across a thread boundary. To make it more user-friendly to debug where an issue is coming from in required modules, each module thread now stores its stack trace in its error message in the case of an error, appended to the actual error message. --- To demonstrate, I have five Luau files that require each other in a chain: `a.luau`, `b.luau`, `c.luau`, `d.luau`, and `e.luau`. At the end of the chain, `e.luau` attempts to (illegally) yield. Prior to this change, Lute outputted this: ``` module can not yield stacktrace: [C] function require a:1 ``` This is what Lute outputs now: ``` module can not yield [C] function yield ./e:1 [C] function require ./d:1 [C] function require ./c:1 [C] function require ./b:1 stacktrace: [C] function require a:1 ``` Fixes #131.
1 parent f9c9f9a commit c7a105f

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

runtime/src/require.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,22 +269,26 @@ static int load(lua_State* L, void* ctx, const char* chunkname, const char* cont
269269
const std::string prefix = "module " + std::string(chunknameView.substr(1)) + " must";
270270

271271
if (lua_gettop(ML) == 0)
272-
lua_pushstring(ML, (prefix + " return a value, if it has no return value, you should explicitly return `nil`").c_str());
272+
lua_pushstring(ML, (prefix + " return a value, if it has no return value, you should explicitly return `nil`\n").c_str());
273273
}
274274
else if (status == LUA_YIELD)
275275
{
276-
lua_pushstring(ML, "module can not yield");
276+
lua_pushstring(ML, "module can not yield\n");
277277
}
278278
else if (!lua_isstring(ML, -1))
279279
{
280-
lua_pushstring(ML, "unknown error while running module");
280+
lua_pushstring(ML, "unknown error while running module\n");
281281
}
282282
}
283283

284284
// add ML result to L stack
285285
lua_xmove(ML, L, 1);
286286
if (lua_isstring(L, -1))
287+
{
288+
lua_pushstring(L, lua_debugtrace(ML));
289+
lua_concat(L, 2);
287290
lua_error(L);
291+
}
288292

289293
// remove ML thread from L stack
290294
lua_remove(L, -2);

0 commit comments

Comments
 (0)