diff options
author | Gregory Anders <8965202+gpanders@users.noreply.github.com> | 2021-11-06 08:26:10 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-06 08:26:10 -0600 |
commit | 03b805aee617f67eb7f33a54822bc76c23a2c5f5 (patch) | |
tree | fc27127d462fe91159eb99c5a724c7b10e459f1b | |
parent | 1fdbd29dfa6366f8346693d0bf67f4f782ab0f32 (diff) | |
download | rneovim-03b805aee617f67eb7f33a54822bc76c23a2c5f5.tar.gz rneovim-03b805aee617f67eb7f33a54822bc76c23a2c5f5.tar.bz2 rneovim-03b805aee617f67eb7f33a54822bc76c23a2c5f5.zip |
feat(lua): enable stack traces in error output (#16228)
-rw-r--r-- | runtime/lua/vim/shared.lua | 2 | ||||
-rw-r--r-- | src/nvim/lua/executor.c | 58 | ||||
-rw-r--r-- | test/functional/lua/api_spec.lua | 33 | ||||
-rw-r--r-- | test/functional/lua/commands_spec.lua | 27 | ||||
-rw-r--r-- | test/functional/lua/loop_spec.lua | 8 | ||||
-rw-r--r-- | test/functional/lua/luaeval_spec.lua | 7 | ||||
-rw-r--r-- | test/functional/lua/overrides_spec.lua | 12 | ||||
-rw-r--r-- | test/functional/lua/vim_spec.lua | 139 | ||||
-rw-r--r-- | test/functional/plugin/health_spec.lua | 6 | ||||
-rw-r--r-- | test/functional/plugin/lsp_spec.lua | 10 | ||||
-rw-r--r-- | test/functional/terminal/tui_spec.lua | 14 | ||||
-rw-r--r-- | test/functional/ui/messages_spec.lua | 43 | ||||
-rw-r--r-- | test/helpers.lua | 10 |
13 files changed, 174 insertions, 195 deletions
diff --git a/runtime/lua/vim/shared.lua b/runtime/lua/vim/shared.lua index b57b7ad4ad..6e40b6ca52 100644 --- a/runtime/lua/vim/shared.lua +++ b/runtime/lua/vim/shared.lua @@ -605,7 +605,7 @@ do function vim.validate(opt) local ok, err_msg = is_valid(opt) if not ok then - error(debug.traceback(err_msg, 2), 2) + error(err_msg, 2) end end end diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 6120deb77a..b27b1ae7a8 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -85,6 +85,27 @@ static void nlua_error(lua_State *const lstate, const char *const msg) lua_pop(lstate, 1); } +/// Like lua_pcall, but use debug.traceback as errfunc. +/// +/// @param lstate Lua interpreter state +/// @param[in] nargs Number of arguments expected by the function being called. +/// @param[in] nresults Number of results the function returns. +static int nlua_pcall(lua_State *lstate, int nargs, int nresults) +{ + lua_getglobal(lstate, "debug"); + lua_getfield(lstate, -1, "traceback"); + lua_remove(lstate, -2); + lua_insert(lstate, -2 - nargs); + int status = lua_pcall(lstate, nargs, nresults, -2 - nargs); + if (status) { + lua_remove(lstate, -2); + } else { + lua_remove(lstate, -1 - nresults); + } + return status; +} + + /// Gets the version of the current Nvim build. /// /// @param lstate Lua interpreter state. @@ -115,7 +136,7 @@ static int nlua_luv_cfpcall(lua_State *lstate, int nargs, int nresult, int flags in_fast_callback++; int top = lua_gettop(lstate); - int status = lua_pcall(lstate, nargs, nresult, 0); + int status = nlua_pcall(lstate, nargs, nresult); if (status) { if (status == LUA_ERRMEM && !(flags & LUVF_CALLBACK_NOEXIT)) { // consider out of memory errors unrecoverable, just like xmalloc() @@ -146,7 +167,7 @@ static void nlua_schedule_event(void **argv) lua_State *const lstate = global_lstate; nlua_pushref(lstate, cb); nlua_unref(lstate, cb); - if (lua_pcall(lstate, 0, 0, 0)) { + if (nlua_pcall(lstate, 0, 0)) { nlua_error(lstate, _("Error executing vim.schedule lua callback: %.*s")); } } @@ -183,7 +204,7 @@ static void dummy_timer_close_cb(TimeWatcher *tw, void *data) static bool nlua_wait_condition(lua_State *lstate, int *status, bool *callback_result) { lua_pushvalue(lstate, 2); - *status = lua_pcall(lstate, 0, 1, 0); + *status = nlua_pcall(lstate, 0, 1); if (*status) { return true; // break on error, but keep error on stack } @@ -385,7 +406,7 @@ static int nlua_state_init(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL { const char *code = (char *)&shared_module[0]; if (luaL_loadbuffer(lstate, code, strlen(code), "@vim/shared.lua") - || lua_pcall(lstate, 0, 0, 0)) { + || nlua_pcall(lstate, 0, 0)) { nlua_error(lstate, _("E5106: Error while creating shared module: %.*s")); return 1; } @@ -397,7 +418,7 @@ static int nlua_state_init(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL const char *code = (char *)&inspect_module[0]; if (luaL_loadbuffer(lstate, code, strlen(code), "@vim/inspect.lua") - || lua_pcall(lstate, 0, 1, 0)) { + || nlua_pcall(lstate, 0, 1)) { nlua_error(lstate, _("E5106: Error while creating inspect module: %.*s")); return 1; } @@ -406,7 +427,7 @@ static int nlua_state_init(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL code = (char *)&lua_F_module[0]; if (luaL_loadbuffer(lstate, code, strlen(code), "@vim/F.lua") - || lua_pcall(lstate, 0, 1, 0)) { + || nlua_pcall(lstate, 0, 1)) { nlua_error(lstate, _("E5106: Error while creating vim.F module: %.*s")); return 1; } @@ -419,7 +440,7 @@ static int nlua_state_init(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL { const char *code = (char *)&vim_module[0]; if (luaL_loadbuffer(lstate, code, strlen(code), "@vim.lua") - || lua_pcall(lstate, 0, 0, 0)) { + || nlua_pcall(lstate, 0, 0)) { nlua_error(lstate, _("E5106: Error while creating vim module: %.*s")); return 1; } @@ -431,7 +452,7 @@ static int nlua_state_init(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL const char *code = (char *)&lua_meta_module[0]; if (luaL_loadbuffer(lstate, code, strlen(code), "@vim/_meta.lua") - || lua_pcall(lstate, 0, 1, 0)) { + || nlua_pcall(lstate, 0, 1)) { nlua_error(lstate, _("E5106: Error while creating vim._meta module: %.*s")); return 1; } @@ -549,6 +570,7 @@ static int nlua_print(lua_State *const lstate) for (; curargidx <= nargs; curargidx++) { lua_pushvalue(lstate, -1); // tostring lua_pushvalue(lstate, curargidx); // arg + // Do not use nlua_pcall here to avoid duplicate stack trace information if (lua_pcall(lstate, 1, 1, 0)) { errmsg = lua_tolstring(lstate, -1, &errmsg_len); goto nlua_print_error; @@ -616,7 +638,7 @@ static int nlua_debug(lua_State *lstate) if (luaL_loadbuffer(lstate, (const char *)input.vval.v_string, STRLEN(input.vval.v_string), "=(debug command)")) { nlua_error(lstate, _("E5115: Error while loading debug string: %.*s")); - } else if (lua_pcall(lstate, 0, 0, 0)) { + } else if (nlua_pcall(lstate, 0, 0)) { nlua_error(lstate, _("E5116: Error while calling debug string: %.*s")); } tv_clear(&input); @@ -909,7 +931,7 @@ static void nlua_typval_exec(const char *lcmd, size_t lcmd_len, const char *name PUSH_ALL_TYPVALS(lstate, args, argcount, special); - if (lua_pcall(lstate, argcount, ret_tv ? 1 : 0, 0)) { + if (nlua_pcall(lstate, argcount, ret_tv ? 1 : 0)) { nlua_error(lstate, _("E5108: Error executing lua %.*s")); return; } @@ -964,7 +986,7 @@ int typval_exec_lua_callable(lua_State *lstate, LuaCallable lua_cb, int argcount PUSH_ALL_TYPVALS(lstate, argvars, argcount, false); - if (lua_pcall(lstate, argcount, 1, 0)) { + if (nlua_pcall(lstate, argcount, 1)) { nlua_print(lstate); return ERROR_OTHER; } @@ -999,7 +1021,7 @@ Object nlua_exec(const String str, const Array args, Error *err) nlua_push_Object(lstate, args.items[i], false); } - if (lua_pcall(lstate, (int)args.size, 1, 0)) { + if (nlua_pcall(lstate, (int)args.size, 1)) { size_t len; const char *errstr = lua_tolstring(lstate, -1, &len); api_set_error(err, kErrorTypeException, @@ -1032,7 +1054,7 @@ Object nlua_call_ref(LuaRef ref, const char *name, Array args, bool retval, Erro nlua_push_Object(lstate, args.items[i], false); } - if (lua_pcall(lstate, nargs, retval ? 1 : 0, 0)) { + if (nlua_pcall(lstate, nargs, retval ? 1 : 0)) { // if err is passed, the caller will deal with the error. if (err) { size_t len; @@ -1126,7 +1148,7 @@ void ex_luado(exarg_T *const eap) if (lcmd_len >= IOSIZE) { xfree(lcmd); } - if (lua_pcall(lstate, 0, 1, 0)) { + if (nlua_pcall(lstate, 0, 1)) { nlua_error(lstate, _("E5110: Error executing lua: %.*s")); return; } @@ -1138,7 +1160,7 @@ void ex_luado(exarg_T *const eap) const char *old_line = (const char *)ml_get_buf(curbuf, l, false); lua_pushstring(lstate, old_line); lua_pushnumber(lstate, (lua_Number)l); - if (lua_pcall(lstate, 2, 1, 0)) { + if (nlua_pcall(lstate, 2, 1)) { nlua_error(lstate, _("E5111: Error calling lua: %.*s")); break; } @@ -1189,7 +1211,7 @@ bool nlua_exec_file(const char *path) return false; } - if (lua_pcall(lstate, 0, 0, 0)) { + if (nlua_pcall(lstate, 0, 0)) { nlua_error(lstate, _("E5113: Error while calling lua chunk: %.*s")); return false; } @@ -1241,7 +1263,7 @@ int nlua_expand_pat(expand_T *xp, char_u *pat, int *num_results, char_u ***resul // [ vim, vim._on_key, buf ] lua_pushlstring(lstate, (const char *)pat, STRLEN(pat)); - if (lua_pcall(lstate, 1, 2, 0) != 0) { + if (nlua_pcall(lstate, 1, 2) != 0) { nlua_error(lstate, _("Error executing vim._expand_pat: %.*s")); return FAIL; @@ -1394,7 +1416,7 @@ void nlua_execute_on_key(int c) // [ vim, vim._on_key, buf ] lua_pushlstring(lstate, (const char *)buf, buf_len); - if (lua_pcall(lstate, 1, 0, 0)) { + if (nlua_pcall(lstate, 1, 0)) { nlua_error(lstate, _("Error executing vim.on_key Lua callback: %.*s")); } diff --git a/test/functional/lua/api_spec.lua b/test/functional/lua/api_spec.lua index 8551c3d2a0..81e00bba6d 100644 --- a/test/functional/lua/api_spec.lua +++ b/test/functional/lua/api_spec.lua @@ -2,6 +2,7 @@ local helpers = require('test.functional.helpers')(after_each) local exc_exec = helpers.exc_exec +local remove_trace = helpers.remove_trace local funcs = helpers.funcs local clear = helpers.clear local eval = helpers.eval @@ -159,44 +160,44 @@ describe('luaeval(vim.api.…)', function() it('errors out correctly when working with API', function() -- Conversion errors eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Cannot convert given lua type', - exc_exec([[call luaeval("vim.api.nvim__id(vim.api.nvim__id)")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim__id(vim.api.nvim__id)")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Cannot convert given lua table', - exc_exec([[call luaeval("vim.api.nvim__id({1, foo=42})")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim__id({1, foo=42})")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Cannot convert given lua type', - exc_exec([[call luaeval("vim.api.nvim__id({42, vim.api.nvim__id})")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim__id({42, vim.api.nvim__id})")]]))) -- Errors in number of arguments eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Expected 1 argument', - exc_exec([[call luaeval("vim.api.nvim__id()")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim__id()")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Expected 1 argument', - exc_exec([[call luaeval("vim.api.nvim__id(1, 2)")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim__id(1, 2)")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Expected 2 arguments', - exc_exec([[call luaeval("vim.api.nvim_set_var(1, 2, 3)")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim_set_var(1, 2, 3)")]]))) -- Error in argument types eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Expected lua string', - exc_exec([[call luaeval("vim.api.nvim_set_var(1, 2)")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim_set_var(1, 2)")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Expected lua number', - exc_exec([[call luaeval("vim.api.nvim_buf_get_lines(0, 'test', 1, false)")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim_buf_get_lines(0, 'test', 1, false)")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Number is not integral', - exc_exec([[call luaeval("vim.api.nvim_buf_get_lines(0, 1.5, 1, false)")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim_buf_get_lines(0, 1.5, 1, false)")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Expected lua table', - exc_exec([[call luaeval("vim.api.nvim__id_float('test')")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim__id_float('test')")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Unexpected type', - exc_exec([[call luaeval("vim.api.nvim__id_float({[vim.type_idx]=vim.types.dictionary})")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim__id_float({[vim.type_idx]=vim.types.dictionary})")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Expected lua table', - exc_exec([[call luaeval("vim.api.nvim__id_array(1)")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim__id_array(1)")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Unexpected type', - exc_exec([[call luaeval("vim.api.nvim__id_array({[vim.type_idx]=vim.types.dictionary})")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim__id_array({[vim.type_idx]=vim.types.dictionary})")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Expected lua table', - exc_exec([[call luaeval("vim.api.nvim__id_dictionary(1)")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim__id_dictionary(1)")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Unexpected type', - exc_exec([[call luaeval("vim.api.nvim__id_dictionary({[vim.type_idx]=vim.types.array})")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim__id_dictionary({[vim.type_idx]=vim.types.array})")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: Expected lua table', - exc_exec([[call luaeval("vim.api.nvim_set_keymap('', '', '', '')")]])) + remove_trace(exc_exec([[call luaeval("vim.api.nvim_set_keymap('', '', '', '')")]]))) -- TODO: check for errors with Tabpage argument -- TODO: check for errors with Window argument diff --git a/test/functional/lua/commands_spec.lua b/test/functional/lua/commands_spec.lua index 2e0d0ea899..9b9ba531b0 100644 --- a/test/functional/lua/commands_spec.lua +++ b/test/functional/lua/commands_spec.lua @@ -17,6 +17,7 @@ local pcall_err = helpers.pcall_err local write_file = helpers.write_file local exec_capture = helpers.exec_capture local curbufmeths = helpers.curbufmeths +local remove_trace = helpers.remove_trace before_each(clear) @@ -46,9 +47,9 @@ describe(':lua command', function() eq([[Vim(lua):E5107: Error loading lua [string ":lua"]:0: unexpected symbol near ')']], pcall_err(command, 'lua ()')) eq([[Vim(lua):E5108: Error executing lua [string ":lua"]:1: TEST]], - exc_exec('lua error("TEST")')) + remove_trace(exc_exec('lua error("TEST")'))) eq([[Vim(lua):E5108: Error executing lua [string ":lua"]:1: Invalid buffer id: -10]], - exc_exec('lua vim.api.nvim_buf_set_lines(-10, 1, 1, false, {"TEST"})')) + remove_trace(exc_exec('lua vim.api.nvim_buf_set_lines(-10, 1, 1, false, {"TEST"})'))) eq({''}, curbufmeths.get_lines(0, 100, false)) end) it('works with NULL errors', function() @@ -95,15 +96,15 @@ describe(':lua command', function() feed(':lua error("fail\\nmuch error\\nsuch details")<cr>') screen:expect{grid=[[ - | - {1:~ }| - {1:~ }| - {1:~ }| {2: }| {3:E5108: Error executing lua [string ":lua}| {3:"]:1: fail} | {3:much error} | {3:such details} | + {3:stack traceback:} | + {3: [C]: in function 'error'} | + {3: [string ":lua"]:1: in main chunk}| + | {4:Press ENTER or type command to continue}^ | ]]} feed('<cr>') @@ -119,24 +120,24 @@ describe(':lua command', function() {1:~ }| | ]]} - eq('E5108: Error executing lua [string ":lua"]:1: fail\nmuch error\nsuch details', eval('v:errmsg')) + eq('E5108: Error executing lua [string ":lua"]:1: fail\nmuch error\nsuch details', remove_trace(eval('v:errmsg'))) local status, err = pcall(command,'lua error("some error\\nin a\\nAPI command")') local expected = 'Vim(lua):E5108: Error executing lua [string ":lua"]:1: some error\nin a\nAPI command' eq(false, status) - eq(expected, string.sub(err, -string.len(expected))) + eq(expected, string.sub(remove_trace(err), -string.len(expected))) feed(':messages<cr>') screen:expect{grid=[[ - | - {1:~ }| - {1:~ }| - {1:~ }| {2: }| {3:E5108: Error executing lua [string ":lua}| {3:"]:1: fail} | {3:much error} | {3:such details} | + {3:stack traceback:} | + {3: [C]: in function 'error'} | + {3: [string ":lua"]:1: in main chunk}| + | {4:Press ENTER or type command to continue}^ | ]]} end) @@ -219,7 +220,7 @@ describe(':luafile', function() exc_exec('luafile ' .. fname)) write_file(fname, 'vimm.api.nvim_buf_set_lines(1, 1, 2, false, {"ETTS"})') eq(("Vim(luafile):E5113: Error while calling lua chunk: %s:1: attempt to index global 'vimm' (a nil value)"):format(fname), - exc_exec('luafile ' .. fname)) + remove_trace(exc_exec('luafile ' .. fname))) end) it('works with NULL errors', function() write_file(fname, 'error(nil)') diff --git a/test/functional/lua/loop_spec.lua b/test/functional/lua/loop_spec.lua index 992d1666f6..7f3787d7bf 100644 --- a/test/functional/lua/loop_spec.lua +++ b/test/functional/lua/loop_spec.lua @@ -84,14 +84,14 @@ describe('vim.loop', function() screen:expect([[ | - {1:~ }| - {1:~ }| - {1:~ }| - {1:~ }| {2: }| {3:Error executing luv callback:} | {3:[string "<nvim>"]:5: E5560: nvim_set_var must not }| {3:be called in a lua loop callback} | + {3:stack traceback:} | + {3: [C]: in function 'nvim_set_var'} | + {3: [string "<nvim>"]:5: in function <[string }| + {3:"<nvim>"]:2>} | {4:Press ENTER or type command to continue}^ | ]]) feed('<cr>') diff --git a/test/functional/lua/luaeval_spec.lua b/test/functional/lua/luaeval_spec.lua index ab76e71a12..c543dd1995 100644 --- a/test/functional/lua/luaeval_spec.lua +++ b/test/functional/lua/luaeval_spec.lua @@ -4,6 +4,7 @@ local Screen = require('test.functional.ui.screen') local pcall_err = helpers.pcall_err local exc_exec = helpers.exc_exec +local remove_trace = helpers.remove_trace local exec_lua = helpers.exec_lua local command = helpers.command local meths = helpers.meths @@ -424,11 +425,11 @@ describe('luaeval()', function() it('errors out correctly when doing incorrect things in lua', function() -- Conversion errors eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: attempt to call field \'xxx_nonexistent_key_xxx\' (a nil value)', - exc_exec([[call luaeval("vim.xxx_nonexistent_key_xxx()")]])) + remove_trace(exc_exec([[call luaeval("vim.xxx_nonexistent_key_xxx()")]]))) eq('Vim(call):E5108: Error executing lua [string "luaeval()"]:1: ERROR', - exc_exec([[call luaeval("error('ERROR')")]])) + remove_trace(exc_exec([[call luaeval("error('ERROR')")]]))) eq('Vim(call):E5108: Error executing lua [NULL]', - exc_exec([[call luaeval("error(nil)")]])) + remove_trace(exc_exec([[call luaeval("error(nil)")]]))) end) it('does not leak memory when called with too long line', diff --git a/test/functional/lua/overrides_spec.lua b/test/functional/lua/overrides_spec.lua index 636479b81f..b0712ff366 100644 --- a/test/functional/lua/overrides_spec.lua +++ b/test/functional/lua/overrides_spec.lua @@ -161,15 +161,15 @@ describe('debug.debug', function() {0:~ }| {0:~ }| {0:~ }| - {0:~ }| - {0:~ }| - {0:~ }| nil | lua_debug> print("TEST") | TEST | | {E:E5108: Error executing lua [string ":lua"]:5: attempt}| {E: to perform arithmetic on local 'a' (a nil value)} | + {E:stack traceback:} | + {E: [string ":lua"]:5: in function 'Test'} | + {E: [string ":lua"]:1: in main chunk} | Interrupt: {cr:Press ENTER or type command to continue}^ | ]]} feed('<C-l>:lua Test()\n') @@ -197,13 +197,13 @@ describe('debug.debug', function() {0:~ }| {0:~ }| {0:~ }| - {0:~ }| - {0:~ }| - {0:~ }| nil | lua_debug> | {E:E5108: Error executing lua [string ":lua"]:5: attempt}| {E: to perform arithmetic on local 'a' (a nil value)} | + {E:stack traceback:} | + {E: [string ":lua"]:5: in function 'Test'} | + {E: [string ":lua"]:1: in main chunk} | {cr:Press ENTER or type command to continue}^ | ]]} end) diff --git a/test/functional/lua/vim_spec.lua b/test/functional/lua/vim_spec.lua index 5f903e7d5f..3832d27a22 100644 --- a/test/functional/lua/vim_spec.lua +++ b/test/functional/lua/vim_spec.lua @@ -4,7 +4,6 @@ local Screen = require('test.functional.ui.screen') local funcs = helpers.funcs local meths = helpers.meths -local dedent = helpers.dedent local command = helpers.command local insert = helpers.insert local clear = helpers.clear @@ -19,6 +18,7 @@ local source = helpers.source local NIL = helpers.NIL local retry = helpers.retry local next_msg = helpers.next_msg +local remove_trace = helpers.remove_trace before_each(clear) @@ -121,11 +121,6 @@ describe('lua stdlib', function() eq(1, funcs.luaeval('vim.stricmp("\\0C\\0", "\\0B\\0")')) end) - -- for brevity, match only the error header (not the traceback) - local function pcall_header(...) - return string.gsub(string.gsub(pcall_err(exec_lua, ...), '[\r\n].*', ''), '^Error executing lua: ', '') - end - it('vim.startswith', function() eq(true, funcs.luaeval('vim.startswith("123", "1")')) eq(true, funcs.luaeval('vim.startswith("123", "")')) @@ -136,8 +131,10 @@ describe('lua stdlib', function() eq(false, funcs.luaeval('vim.startswith("123", "2")')) eq(false, funcs.luaeval('vim.startswith("123", "1234")')) - eq("vim/shared.lua:0: prefix: expected string, got nil", pcall_header 'return vim.startswith("123", nil)') - eq("vim/shared.lua:0: s: expected string, got nil", pcall_header 'return vim.startswith(nil, "123")') + eq("Error executing lua: vim/shared.lua:0: prefix: expected string, got nil", + pcall_err(exec_lua, 'return vim.startswith("123", nil)')) + eq("Error executing lua: vim/shared.lua:0: s: expected string, got nil", + pcall_err(exec_lua, 'return vim.startswith(nil, "123")')) end) it('vim.endswith', function() @@ -150,8 +147,10 @@ describe('lua stdlib', function() eq(false, funcs.luaeval('vim.endswith("123", "2")')) eq(false, funcs.luaeval('vim.endswith("123", "1234")')) - eq("vim/shared.lua:0: suffix: expected string, got nil", pcall_header 'return vim.endswith("123", nil)') - eq("vim/shared.lua:0: s: expected string, got nil", pcall_header 'return vim.endswith(nil, "123")') + eq("Error executing lua: vim/shared.lua:0: suffix: expected string, got nil", + pcall_err(exec_lua, 'return vim.endswith("123", nil)')) + eq("Error executing lua: vim/shared.lua:0: s: expected string, got nil", + pcall_err(exec_lua, 'return vim.endswith(nil, "123")')) end) it("vim.str_utfindex/str_byteindex", function() @@ -233,7 +232,7 @@ describe('lua stdlib', function() ]]) feed("<cr>") - eq('Error executing vim.schedule lua callback: [string "<nvim>"]:2: big failure\nvery async', eval("v:errmsg")) + eq('Error executing vim.schedule lua callback: [string "<nvim>"]:2: big failure\nvery async', remove_trace(eval("v:errmsg"))) local screen = Screen.new(60,5) screen:set_default_attr_ids({ @@ -259,10 +258,10 @@ describe('lua stdlib', function() end) ]]) screen:expect{grid=[[ - | - {2: }| - {3:Error executing vim.schedule lua callback: [string "<nvim>"]}| - {3::2: Vim(echo):E115: Missing quote: 'err} | + {3:stack traceback:} | + {3: [C]: in function 'nvim_command'} | + {3: [string "<nvim>"]:2: in function <[string "<nvim>"]:}| + {3:1>} | {4:Press ENTER or type command to continue}^ | ]]} end) @@ -306,22 +305,11 @@ describe('lua stdlib', function() -- Validates args. eq(true, pcall(split, 'string', 'string')) - eq(dedent([[ - Error executing lua: vim/shared.lua:0: s: expected string, got number - stack traceback: - vim/shared.lua:0: in function 'gsplit' - vim/shared.lua:0: in function <vim/shared.lua:0>]]), + eq('Error executing lua: vim/shared.lua:0: s: expected string, got number', pcall_err(split, 1, 'string')) - eq(dedent([[ - Error executing lua: vim/shared.lua:0: sep: expected string, got number - stack traceback: - vim/shared.lua:0: in function 'gsplit' - vim/shared.lua:0: in function <vim/shared.lua:0>]]), + eq('Error executing lua: vim/shared.lua:0: sep: expected string, got number', pcall_err(split, 'string', 1)) - eq(dedent([[ - Error executing lua: vim/shared.lua:0: kwargs: expected table, got number - stack traceback: - vim/shared.lua:0: in function <vim/shared.lua:0>]]), + eq('Error executing lua: vim/shared.lua:0: kwargs: expected table, got number', pcall_err(split, 'string', 'string', 1)) end) @@ -342,10 +330,7 @@ describe('lua stdlib', function() end -- Validates args. - eq(dedent([[ - Error executing lua: vim/shared.lua:0: s: expected string, got number - stack traceback: - vim/shared.lua:0: in function <vim/shared.lua:0>]]), + eq('Error executing lua: vim/shared.lua:0: s: expected string, got number', pcall_err(trim, 2)) end) @@ -424,10 +409,7 @@ describe('lua stdlib', function() eq('foo%%%-bar', exec_lua([[return vim.pesc(vim.pesc('foo-bar'))]])) -- Validates args. - eq(dedent([[ - Error executing lua: vim/shared.lua:0: s: expected string, got number - stack traceback: - vim/shared.lua:0: in function <vim/shared.lua:0>]]), + eq('Error executing lua: vim/shared.lua:0: s: expected string, got number', pcall_err(exec_lua, [[return vim.pesc(2)]])) end) @@ -710,10 +692,7 @@ describe('lua stdlib', function() it('vim.list_extend', function() eq({1,2,3}, exec_lua [[ return vim.list_extend({1}, {2,3}) ]]) - eq(dedent([[ - Error executing lua: vim/shared.lua:0: src: expected table, got nil - stack traceback: - vim/shared.lua:0: in function <vim/shared.lua:0>]]), + eq('Error executing lua: vim/shared.lua:0: src: expected table, got nil', pcall_err(exec_lua, [[ return vim.list_extend({1}, nil) ]])) eq({1,2}, exec_lua [[ return vim.list_extend({1}, {2;a=1}) ]]) eq(true, exec_lua [[ local a = {1} return vim.list_extend(a, {2;a=1}) == a ]]) @@ -845,12 +824,12 @@ describe('lua stdlib', function() end) ]]) screen:expect{grid=[[ - foo | - {1:~ }| - {2: }| - {3:Error executing luv callback:} | {3:[string "<nvim>"]:6: E5560: rpcrequest must not be}| {3: called in a lua loop callback} | + {3:stack traceback:} | + {3: [C]: in function 'rpcrequest'} | + {3: [string "<nvim>"]:6: in function <[string }| + {3:"<nvim>"]:2>} | {4:Press ENTER or type command to continue}^ | ]]} feed('<cr>') @@ -914,76 +893,37 @@ describe('lua stdlib', function() exec_lua("vim.validate{arg1={{}, 't' }, arg2={ 'foo', 's' }}") exec_lua("vim.validate{arg1={2, function(a) return (a % 2) == 0 end, 'even number' }}") - eq(dedent([[ - Error executing lua: [string "<nvim>"]:0: opt[1]: expected table, got number - stack traceback: - [string "<nvim>"]:0: in main chunk]]), + eq('Error executing lua: [string "<nvim>"]:0: opt[1]: expected table, got number', pcall_err(exec_lua, "vim.validate{ 1, 'x' }")) - eq(dedent([[ - Error executing lua: [string "<nvim>"]:0: invalid type name: x - stack traceback: - [string "<nvim>"]:0: in main chunk]]), + eq('Error executing lua: [string "<nvim>"]:0: invalid type name: x', pcall_err(exec_lua, "vim.validate{ arg1={ 1, 'x' }}")) - eq(dedent([[ - Error executing lua: [string "<nvim>"]:0: invalid type name: 1 - stack traceback: - [string "<nvim>"]:0: in main chunk]]), + eq('Error executing lua: [string "<nvim>"]:0: invalid type name: 1', pcall_err(exec_lua, "vim.validate{ arg1={ 1, 1 }}")) - eq(dedent([[ - Error executing lua: [string "<nvim>"]:0: invalid type name: nil - stack traceback: - [string "<nvim>"]:0: in main chunk]]), + eq('Error executing lua: [string "<nvim>"]:0: invalid type name: nil', pcall_err(exec_lua, "vim.validate{ arg1={ 1 }}")) -- Validated parameters are required by default. - eq(dedent([[ - Error executing lua: [string "<nvim>"]:0: arg1: expected string, got nil - stack traceback: - [string "<nvim>"]:0: in main chunk]]), + eq('Error executing lua: [string "<nvim>"]:0: arg1: expected string, got nil', pcall_err(exec_lua, "vim.validate{ arg1={ nil, 's' }}")) -- Explicitly required. - eq(dedent([[ - Error executing lua: [string "<nvim>"]:0: arg1: expected string, got nil - stack traceback: - [string "<nvim>"]:0: in main chunk]]), + eq('Error executing lua: [string "<nvim>"]:0: arg1: expected string, got nil', pcall_err(exec_lua, "vim.validate{ arg1={ nil, 's', false }}")) - eq(dedent([[ - Error executing lua: [string "<nvim>"]:0: arg1: expected table, got number - stack traceback: - [string "<nvim>"]:0: in main chunk]]), + eq('Error executing lua: [string "<nvim>"]:0: arg1: expected table, got number', pcall_err(exec_lua, "vim.validate{arg1={1, 't'}}")) - eq(dedent([[ - Error executing lua: [string "<nvim>"]:0: arg2: expected string, got number - stack traceback: - [string "<nvim>"]:0: in main chunk]]), + eq('Error executing lua: [string "<nvim>"]:0: arg2: expected string, got number', pcall_err(exec_lua, "vim.validate{arg1={{}, 't'}, arg2={1, 's'}}")) - eq(dedent([[ - Error executing lua: [string "<nvim>"]:0: arg2: expected string, got nil - stack traceback: - [string "<nvim>"]:0: in main chunk]]), + eq('Error executing lua: [string "<nvim>"]:0: arg2: expected string, got nil', pcall_err(exec_lua, "vim.validate{arg1={{}, 't'}, arg2={nil, 's'}}")) - eq(dedent([[ - Error executing lua: [string "<nvim>"]:0: arg2: expected string, got nil - stack traceback: - [string "<nvim>"]:0: in main chunk]]), + eq('Error executing lua: [string "<nvim>"]:0: arg2: expected string, got nil', pcall_err(exec_lua, "vim.validate{arg1={{}, 't'}, arg2={nil, 's'}}")) - eq(dedent([[ - Error executing lua: [string "<nvim>"]:0: arg1: expected even number, got 3 - stack traceback: - [string "<nvim>"]:0: in main chunk]]), + eq('Error executing lua: [string "<nvim>"]:0: arg1: expected even number, got 3', pcall_err(exec_lua, "vim.validate{arg1={3, function(a) return a == 1 end, 'even number'}}")) - eq(dedent([[ - Error executing lua: [string "<nvim>"]:0: arg1: expected ?, got 3 - stack traceback: - [string "<nvim>"]:0: in main chunk]]), + eq('Error executing lua: [string "<nvim>"]:0: arg1: expected ?, got 3', pcall_err(exec_lua, "vim.validate{arg1={3, function(a) return a == 1 end}}")) -- Pass an additional message back. - eq(dedent([[ - Error executing lua: [string "<nvim>"]:0: arg1: expected ?, got 3. Info: TEST_MSG - stack traceback: - [string "<nvim>"]:0: in main chunk]]), + eq('Error executing lua: [string "<nvim>"]:0: arg1: expected ?, got 3. Info: TEST_MSG', pcall_err(exec_lua, "vim.validate{arg1={3, function(a) return a == 1, 'TEST_MSG' end}}")) end) @@ -2133,9 +2073,10 @@ describe('lua stdlib', function() end) it('should not crash when callback errors', function() - eq({false, '[string "<nvim>"]:1: As Expected'}, exec_lua [[ + local result = exec_lua [[ return {pcall(function() vim.wait(1000, function() error("As Expected") end) end)} - ]]) + ]] + eq({false, '[string "<nvim>"]:1: As Expected'}, {result[1], remove_trace(result[2])}) end) it('if callback is passed, it must be a function', function() diff --git a/test/functional/plugin/health_spec.lua b/test/functional/plugin/health_spec.lua index b84e9d1533..b567b3e20c 100644 --- a/test/functional/plugin/health_spec.lua +++ b/test/functional/plugin/health_spec.lua @@ -134,7 +134,7 @@ describe('health.vim', function() command("checkhealth test_plug*") local buf_lines = helpers.curbuf('get_lines', 0, -1, true) -- avoid dealing with path separators - local received = table.concat(buf_lines, '\n', 1, #buf_lines - 2) + local received = table.concat(buf_lines, '\n', 1, #buf_lines - 5) local expected = helpers.dedent([[ test_plug: require("test_plug.health").check() @@ -175,10 +175,10 @@ describe('health.vim', function() it("gracefully handles broken lua healthcheck", function() command("checkhealth test_plug.submodule_failed") local buf_lines = helpers.curbuf('get_lines', 0, -1, true) - local received = table.concat(buf_lines, '\n', 1, #buf_lines - 2) + local received = table.concat(buf_lines, '\n', 1, #buf_lines - 5) -- avoid dealing with path separators local lua_err = "attempt to perform arithmetic on a nil value" - local last_line = buf_lines[#buf_lines - 1] + local last_line = buf_lines[#buf_lines - 4] assert(string.find(last_line, lua_err) ~= nil, "Lua error not present") local expected = global_helpers.dedent([[ diff --git a/test/functional/plugin/lsp_spec.lua b/test/functional/plugin/lsp_spec.lua index 37fba02e0e..c025e9f4ab 100644 --- a/test/functional/plugin/lsp_spec.lua +++ b/test/functional/plugin/lsp_spec.lua @@ -1153,15 +1153,9 @@ describe('LSP', function() end) it('should invalid cmd argument', function() - eq(dedent([[ - Error executing lua: .../lsp.lua:0: cmd: expected list, got nvim - stack traceback: - .../lsp.lua:0: in function <.../lsp.lua:0>]]), + eq('Error executing lua: .../lsp.lua:0: cmd: expected list, got nvim', pcall_err(_cmd_parts, 'nvim')) - eq(dedent([[ - Error executing lua: .../lsp.lua:0: cmd argument: expected string, got number - stack traceback: - .../lsp.lua:0: in function <.../lsp.lua:0>]]), + eq('Error executing lua: .../lsp.lua:0: cmd argument: expected string, got number', pcall_err(_cmd_parts, {'nvim', 1})) end) end) diff --git a/test/functional/terminal/tui_spec.lua b/test/functional/terminal/tui_spec.lua index f7520b14d4..6b9586b4de 100644 --- a/test/functional/terminal/tui_spec.lua +++ b/test/functional/terminal/tui_spec.lua @@ -494,6 +494,8 @@ describe('TUI', function() it('paste: recovers from vim.paste() failure', function() child_session:request('nvim_exec_lua', [[ _G.save_paste_fn = vim.paste + -- Stack traces for this test are non-deterministic, so disable them + _G.debug.traceback = function(msg) return msg end vim.paste = function(lines, phase) error("fake fail") end ]], {}) -- Prepare something for dot-repeat/redo. @@ -514,7 +516,7 @@ describe('TUI', function() foo | | {5: }| - {8:paste: Error executing lua: [string "<nvim>"]:2: f}| + {8:paste: Error executing lua: [string "<nvim>"]:4: f}| {8:ake fail} | {10:Press ENTER or type command to continue}{1: } | {3:-- TERMINAL --} | @@ -579,12 +581,16 @@ describe('TUI', function() it("paste: 'nomodifiable' buffer", function() child_session:request('nvim_command', 'set nomodifiable') + child_session:request('nvim_exec_lua', [[ + -- Stack traces for this test are non-deterministic, so disable them + _G.debug.traceback = function(msg) return msg end + ]], {}) feed_data('\027[200~fail 1\nfail 2\n\027[201~') screen:expect{grid=[[ | {4:~ }| {5: }| - {MATCH:paste: Error executing lua: vim.lua:%d+: Vim:E21: }| + {8:paste: Error executing lua: vim.lua:243: Vim:E21: }| {8:Cannot make changes, 'modifiable' is off} | {10:Press ENTER or type command to continue}{1: } | {3:-- TERMINAL --} | @@ -671,8 +677,8 @@ describe('TUI', function() item 2997 | item 2998 | item 2999 | - item 3000 en{1:d}d | - {5:[No Name] [+] 5999,13 Bot}| + item 3000 en{1:d} | + {5:[No Name] [+] 3000,13 Bot}| | {3:-- TERMINAL --} | ]]) diff --git a/test/functional/ui/messages_spec.lua b/test/functional/ui/messages_spec.lua index c238898244..f038348253 100644 --- a/test/functional/ui/messages_spec.lua +++ b/test/functional/ui/messages_spec.lua @@ -750,8 +750,13 @@ describe('ui/ext_messages', function() {1:~ }| {1:~ }| ]], messages={{ - content = {{'E5108: Error executing lua [string ":lua"]:1: such\nmultiline\nerror', 2}}, - kind = "lua_error" + content = {{[[E5108: Error executing lua [string ":lua"]:1: such +multiline +error +stack traceback: + [C]: in function 'error' + [string ":lua"]:1: in main chunk]], 2}}, + kind = "lua_error", }}} end) @@ -1048,12 +1053,12 @@ vimComment xxx match /\s"[^\-:.%#=*].*$/ms=s+1,lc=1 excludenl contains=@vim -- edge case: just covers statusline feed(':set colorcolumn=5 | lua error("x\\n\\nx")<cr>') screen:expect{grid=[[ - | - {1:~ }| - {3: }| {2:E5108: Error executing lua [string ":lua"]:1: x} | | {2:x} | + {2:stack traceback:} | + {2: [C]: in function 'error'} | + {2: [string ":lua"]:1: in main chunk} | {4:Press ENTER or type command to continue}^ | ]]} @@ -1071,24 +1076,24 @@ vimComment xxx match /\s"[^\-:.%#=*].*$/ms=s+1,lc=1 excludenl contains=@vim -- edge case: just covers lowest window line feed(':set colorcolumn=5 | lua error("x\\n\\n\\nx")<cr>') screen:expect{grid=[[ - | - {3: }| {2:E5108: Error executing lua [string ":lua"]:1: x} | | | {2:x} | - {4:Press ENTER or type command to continue}^ | + {2:stack traceback:} | + {2: [C]: in function 'error'} | + {4:-- More --}^ | ]]} feed('<cr>') screen:expect{grid=[[ | - {1:~ }| - {8:[No Name] }| - ^ {9: } | - {1:~ }| - {3:[No Name] }| | + {2:x} | + {2:stack traceback:} | + {2: [C]: in function 'error'} | + {2: [string ":lua"]:1: in main chunk} | + {4:Press ENTER or type command to continue}^ | ]]} end) end) @@ -1360,14 +1365,14 @@ aliquip ex ea commodo consequat.]]) ]]} feed('d') screen:expect{grid=[[ + {2:mpor} | {2:incididunt ut labore et dolore magn}| {2:a aliqua.} | {2:Ut enim ad minim veniam, quis nostr}| {2:ud xercitation} | {2:ullamco laboris nisi ut} | {2:aliquip ex ea commodo consequat.} | - {4:Press ENTER or type command to cont}| - {4:inue}^ | + {4:-- More --}^ | ]]} feed('u') screen:expect{grid=[[ @@ -1460,14 +1465,14 @@ aliquip ex ea commodo consequat.]]) ]]} feed('d') screen:expect{grid=[[ + {3:mpor}{5: }| {3:incididunt ut labore et dolore magn}| {3:a aliqua.}{5: }| {3:Ut enim ad minim veniam, quis nostr}| {3:ud xercitation}{5: }| {3:ullamco laboris nisi ut}{5: }| {3:aliquip ex ea commodo consequat.}{5: }| - {6:Press ENTER or type command to cont}| - {6:inue}{5:^ }| + {6:-- More --}{5:^ }| ]]} feed('u') screen:expect{grid=[[ @@ -1667,6 +1672,7 @@ aliquip ex ea commodo consequat.]]) -- wrapped at the new screen size. feed('<cr>') screen:expect{grid=[[ + {2:":lua"]:1: Lorem ipsum dolor s}| {2:et, consectetur} | {2:adipisicing elit, sed do eiusm}| {2:mpore} | @@ -1677,8 +1683,7 @@ aliquip ex ea commodo consequat.]]) {2:ullamco laboris nisi ut} | {2:aliquip ex ea commodo consequa}| {2:t.} | - {4:Press ENTER or type command to}| - {4: continue}^ | + {4:-- More --}^ | ]]} feed('q') diff --git a/test/helpers.lua b/test/helpers.lua index 9ac3904776..09b113c01d 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -155,12 +155,20 @@ function module.pcall_err_withfile(fn, ...) return errmsg end -function module.pcall_err(fn, ...) +function module.pcall_err_withtrace(fn, ...) local errmsg = module.pcall_err_withfile(fn, ...) return errmsg:gsub('.../helpers.lua:0: ', '') end +function module.pcall_err(...) + return module.remove_trace(module.pcall_err_withtrace(...)) +end + +function module.remove_trace(s) + return (s:gsub("\n%s*stack traceback:.*", "")) +end + -- initial_path: directory to recurse into -- re: include pattern (string) -- exc_re: exclude pattern(s) (string or table) |