stevearc / oil.nvim

Neovim file explorer: edit your filesystem like a buffer
MIT License
3.84k stars 110 forks source link

bug: entering a buffer from oil silences any prints done by BufEnter autocmds #307

Closed spflaumer closed 5 months ago

spflaumer commented 8 months ago

Did you check the docs and existing issues?

Neovim version (nvim -v)

NVIM v0.9.5

Operating system/version

Void Linux x86_64 (musl)

Describe the bug

When opening a file from oil, no BufEnter autocmds are triggered. any prints by autocmds are silenced. Using netrw yields the exact opposite result, consistently triggering the BufEnter autocmds, prints from BufEnter autocmds. The same applies to other plugins (telescope.nvim) and the :edit command (even when oil is loaded). Trying to circumvent this issue by creating a custom mapping for the select action, using a callback that triggers the select action and then manually the BufEnter event, doesn't help; nothing from BufEnter autocmds is printed.

What is the severity of this bug?

minor (annoyance)

Steps To Reproduce

  1. nvim -u repro.lua
  2. :Oil
  3. open any file

Expected Behavior

All BufEnter autocmds are ran

Directory structure

./foo.txt ./repro.lua

Repro

-- save as repro.lua
-- run with nvim -u repro.lua
-- DO NOT change the paths
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "runtime", "cache" }) do
    vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({
        "git",
        "clone",
        "--filter=blob:none",
        "--single-branch",
        "https://github.com/folke/lazy.nvim.git",
        lazypath,
    })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
    "folke/tokyonight.nvim",
    {
        "stevearc/oil.nvim",
        opts = {
            default_file_explorer = true,
            columns = { "mtime", "permissions" },

            skip_confirm_for_simple_edits = true,
            use_default_keymaps = true,

            view_options = {
                show_hidden = true,
                is_always_hidden = function(name, _)
                    return name == ".git"
                        or name == "."
                        or name == ".."
                        or string.match(name, "cache")
                end
            }
        }
    }
}

require("lazy").setup(plugins, {
    root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")

-- test autocmd
vim.api.nvim_create_autocmd("BufEnter", {
    callback = function(ev)
        print("Triggered:", ev.event, ev.file)
    end
})

Did you check the bug with a clean config?

spflaumer commented 8 months ago

It seems that the issue is of an entirely different kind... as it turns out, something silences all prints. Writing something to a global variable does in fact alter it's contents on BufEnter. Reading the code for all BufEnter autocmds created by oil didn't really reveal why

stevearc commented 6 months ago

I have encountered this problem before (missing prints in autocmds), though not in relation to oil specifically. I wish I had made more of a note of it, but I think I just worked around it and moved on. This is almost certainly either a bug or a weird interaction in Neovim itself. The thing to do here would be to try to get a more minimal repro (without using oil) and file it upstream on Neovim.

cmygray commented 5 months ago

May I know more about the workaround? There is an issue where the autocmd BufNewFile hook does not work when creating a new file in oil buffer, causing the inability to use skeleton files.

Found one from closed issue, never mind. https://github.com/stevearc/oil.nvim/issues/49#issuecomment-1407567002

stevearc commented 5 months ago

Dug into it more, and found a fix in #348