folke / persistence.nvim

💾 Simple session management for Neovim
Apache License 2.0
591 stars 26 forks source link

bug: BufRead/BufReadPost events are not trigered when restoring session #53

Closed roeybenarieh closed 2 weeks ago

roeybenarieh commented 3 months ago

Did you check docs and existing issues?

Neovim version (nvim -v)

NVIM v0.9.4

Operating system/version

Ubuntu 22.04.4 LTS

Describe the bug

When I enter Neovim and load a session, I can see a file that has been loaded, but the BufReadPost/BufRead event hasn't been triggered. This is a very annoying bug, I can't make any autocmd with this event just because of this.

Steps To Reproduce

  1. open a file for editing with ":e file.txt"
  2. save the session ":lua require('persistence').save()"
  3. quit ":qa!"
  4. enter nvim with the minimal configuration I provided "nvim -u repro.lua"
  5. restore session ":lua require('persistence').load()"
  6. I created an autocmd that prints when the BufReadPost event is triggered, but as you can see nothing is printed

Expected Behavior

Every time a session is loaded and a buffer is shown, the BufReadPost/BufRead event should occur

Repro

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "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", "https://github.com/folke/lazy.nvim.git", lazypath })
end
vim.opt.runtimepath:prepend(lazypath)

-- create an autocmd to print when BufReadPost is triggered
vim.api.nvim_create_autocmd({ "BufReadPost" }, {
  pattern = { "*" },
  callback = function()
    print("the event BufReadPost was triggered!")
  end,
})

-- install plugins
local plugins = {
  "folke/tokyonight.nvim",
  {
    "folke/persistence.nvim",
    lazy = false,
    opts = {},
  },
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
github-actions[bot] commented 3 weeks ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

roeybenarieh commented 2 weeks ago

The problem still exists.

folke commented 2 weeks ago

It IS being executed, however session loading is done inside silent!, so your print statement will be silenced.

Check with:

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "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", "https://github.com/folke/lazy.nvim.git", lazypath })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  "folke/tokyonight.nvim",
  {
    "folke/persistence.nvim",
    lazy = false,
    opts = {},
  },
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

local msg = {}
-- create an autocmd to print when BufReadPost is triggered
vim.api.nvim_create_autocmd("BufRead", {
  callback = function(ev)
    local name = vim.api.nvim_buf_get_name(ev.buf)
    msg[#msg + 1] = "BufRead triggered for " .. name
  end,
})

vim.cmd.colorscheme("tokyonight")
vim.o.cmdheight = 5

vim.defer_fn(function()
  require("persistence").load()
  vim.schedule(function()
    vim.print(msg)
  end)
end, 100)