stevearc / resession.nvim

A replacement for mksession with a better API
MIT License
175 stars 13 forks source link

bug: VimLeavePre is causing all buffers to be marked as unloaded #49

Closed serranomorante closed 5 months ago

serranomorante commented 5 months ago

Did you check the docs and existing issues?

Neovim version (nvim -v)

NVIM v0.10.0-dev-2064+g2f9ee9b6c

Operating system/version

arch, ubuntu

Describe the bug

Hi! All buffers from the session .json file are marked as loaded: false when exiting neovim by closing the terminal window instead of executing :qa!.

Is my understanding that at least 1 of the buffers from that .json file should be marked as loaded: true for everything to work correctly later on load, right? I believe that is the case because this is the root of a problem I'm having and which is kinda painful to fully replicate.

What is the severity of this bug?

tolerable (can work around it)

Steps To Reproduce

Using the provided repro, open neovim nightly with nvim --clean +'so repro.lua'. Put at least 2 files (of any type) under the same cwd as your repro.

  1. Open a new instance of your terminal emulator.
  2. Open neovim with nvim --clean +'so repro.lua'
  3. Open some files and exit neovim with :qa!
  4. Go to the "dirsession" folder (should be on ~...resession_issue/.repro/data/nvim/dirsession/_session_file.json) and inspect the .json file, at least 1 of the buffers is marked as loaded. This is good.
  5. Now repeat step 3 but this time exit neovim by closing your terminal emulator window (I tested this with Kitty and Konsole)
  6. Inspect the .json file again. Now all buffers are marked as unloaded. Bad (I think)

This doesn't seems to cause that much of a problem, until you have some complicated lazy-loading on your neovim config.

My workaround is using "UILeave" event instead of "VimLeavePre":

      vim.api.nvim_create_autocmd("UILeave", {
        desc = "Save a dir-specific session when you close Neovim",
        group = vim.api.nvim_create_augroup("resession_autosave_session", { clear = true }),
        callback = function()
          -- Only save the session if nvim was started with no args
          resession.save(vim.fn.getcwd(), { dir = "dirsession", notify = false })
        end,
      })

Expected Behavior

At least 1 buffer should be marked as loaded

Directory structure

. ├── file-1.md ├── file-2.md └── repro.lua

Repro

-- DO NOT change the paths
local root = vim.fn.fnamemodify("./.repro", ":p")
root = root:sub(-1) == "/" and root or root .. "/"

-- 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

--------------------------------------------------------------------------------

vim.g.mapleader = " "

--------------------------------------------------------------------------------

local plugins = {
  {
    "stevearc/resession.nvim",
    event = "VeryLazy",
    config = function(_, opts)
      local resession = require("resession")
      resession.setup(opts)

      vim.api.nvim_create_autocmd("VimLeavePre", {
        desc = "Save a dir-specific session when you close Neovim",
        group = vim.api.nvim_create_augroup("resession_autosave_session", { clear = true }),
        callback = function()
          -- Only save the session if nvim was started with no args
          resession.save(vim.fn.getcwd(), { dir = "dirsession", notify = false })
        end,
      })
    end,
  },
}

--------------------------------------------------------------------------------

local lazypath = root .. "/plugins/lazy.nvim"
---@diagnostic disable-next-line: undefined-field
if not (vim.uv or vim.loop).fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "https://github.com/folke/lazy.nvim.git",
    "--branch=stable",
    lazypath,
  })
end
vim.opt.runtimepath:prepend(lazypath)

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

Did you check the bug with a clean config?

serranomorante commented 5 months ago

Now I realized UILeave also don't work when exiting neovim as :qa!. What always work is just remembering to manually save the session before leaving. I also can't use the autosave option from resession because it uses VimLeavePre under the hook.

stevearc commented 5 months ago

Looks like when Neovim is killed all of the buffers are marked as unloaded during VimLeavePre. I added a hack to work around this