Repository: neovim/neovim

Issue #13711: Errors in BufEnter autocmd swallowed when triggered by vim.cmd("buffer ...")
Created: 2021-01-07T19:18:25Z
nvim --version: NVIM v0.5.0-dev+1012-gfe1ebea33
vim -u DEFAULTS (version: ) behaves differently? Vim doesn't have vim.cmd or vim.api.
Operating system/version: N/A
Terminal name/version: N/A
$TERM: N/A

Steps to reproduce using nvim -u NORC
Create the following bug.lua file.
function setup_bug()
  vim.cmd [[autocmd BufEnter * lua print(0); error("error swallowed"); print(1)]]
  -- vim.cmd [[autocmd BufEnter * throw 'error swallowed']]
end
function show_bug()
  local buf = vim.api.nvim_create_buf(true, false)
  vim.api.nvim_buf_set_lines(buf, 0, -1, true, {"some text"})
  vim.cmd("buffer " .. buf)
  -- vim.cmd("doautocmd BufEnter")
end
Then run this command.
nvim -u NORC -c 'luafile bug.lua' -c 'lua setup_bug(); show_bug()'

Actual behaviour
A new unnamed buffer is created with the text "some text", 0 is printed, and then the new buffer is switched to.
If you comment out line 8 and uncomment line 9 (i.e. replace buffer with doautocmd), then the error is correctly displayed. lua error("error swallowed") and throw 'error swallowed' as the autocmd both have the same behavior.
Expected behaviour
A new unnamed buffer is created with the text "some text" and the following is printed (or similar)
0
Error detected while processing command line:
E5108: Error executing lua bug.lua:8: Vim(lua):E5108: Error executing lua [string ":lua"]:1: error swallowed

Duplicate issue #13726: `BufEnter` is not executed with `:lua vim.cmd('edit /')`.
Created: 2021-01-09T04:48:27Z
nvim --version:

NVIM v0.5.0-dev+1012-gfe1ebea33
Build type: RelWithDebInfo
LuaJIT 2.1.0-beta3
Compilation: /usr/bin/cc -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 -O2 -g -Og -g -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wmissing-prototypes -Wvla -fstack-protector-strong -fno-common -fdiagnostics-color=always -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -DNVIM_MSGPACK_HAS_FLOAT32 -DNVIM_UNIBI_HAS_VAR_FROM -DMIN_LOG_LEVEL=3 -I/home/runner/work/neovim/neovim/build/config -I/home/runner/work/neovim/neovim/src -I/home/runner/work/neovim/neovim/.deps/usr/include -I/usr/include -I/home/runner/work/neovim/neovim/build/src/nvim/auto -I/home/runner/work/neovim/neovim/build/include
Compiled by runner@fv-az207-568

Features: +acl +iconv +tui
See ":help feature-compile"

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/home/runner/work/neovim/neovim/build/nvim.AppDir/usr/share/nvim"

Run :checkhealth for more info



vim -u DEFAULTS (version: ) behaves differently?


Operating system/version:


DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.5 LTS"

Terminal name/version: wsltty 3.3.0
$TERM: xterm-256color

Steps to reproduce using nvim -u NORC
nvim -u NORC
# Alternative for shell-related problems:
# env -i TERM=ansi-256color "$(which nvim)"


:lua vim.cmd('edit /')

Actual behaviour
It seems that BufEnter is not executed when a file path with a slash ending is executed by the :edit command in lua's vim.cmd() function.
But with :edit /, BufEnter will be executed correctly.
It would be great if vim.cmd('edit /') would execute BufEnter, although this can be handled by doing the following
vim.cmd('edit /')
vim.cmd('doautocmd BufEnter')
Expected behaviour
I want BufEnter to be executed when :lua vim.cmd('edit /') is executed.

Closing comment (2022-03-25T02:23:23Z): This is not a duplicate of #13711. #17846 fixes this one, but not #13711.