From fb5622215ba945ff87514c0c255738d338a23aaa Mon Sep 17 00:00:00 2001 From: Carter Li Date: Thu, 24 Sep 2026 15:32:15 +0800 Subject: [PATCH] Common (Format): fixes lua error message trimming --- src/common/impl/format.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/common/impl/format.c b/src/common/impl/format.c index b00fb2130..450870c39 100644 --- a/src/common/impl/format.c +++ b/src/common/impl/format.c @@ -147,11 +147,26 @@ static inline bool formatArgSet(const FFformatarg* arg) { static void appendLuaError(FFstrbuf* buffer, const char* prefix, lua_State* L) { const char* err = lua_tolstring(L, -1, nullptr); if (err) { - const char* tmp = strchr(err, ':'); - if (tmp) { - err = tmp + 1; - while (*err == ' ') { - ++err; + // Drop the position Lua puts in front of an error and keep the message only. Both places + // that build one -- `luaG_addinfo` and `luaL_where` -- format it as `%s:%d: %s`, so a + // position is a colon, a line number, a colon and a space: + // general.preload.lua:1: expected near '=' -> expected near '=' + // [string ""]:1: x -> x + // Not every error has one, and cutting at the first colon mangled those. `luaL_loadfilex` + // reports `cannot open : `, where that colon is the one after the path, so + // the path -- the only part naming the problem -- was dropped and `No such file or + // directory` was left on its own. A colon followed by a line number does not identify a + // position either, because a path can contain one: `cannot open /tmp/x:12:y/nope.lua: ...` + // has to survive whole. The trailing space is what separates the two, and a real position + // always has it. + for (const char* colon = strchr(err, ':'); colon; colon = strchr(colon + 1, ':')) { + const char* line = colon + 1; + while (*line >= '0' && *line <= '9') { + ++line; + } + if (line > colon + 1 && line[0] == ':' && line[1] == ' ') { + err = line + 2; + break; } } }