Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
3 tasks done
serranomorante opened this issue Jan 13, 2024 · 2 comments
Closed
3 tasks done
Labels
bug Something isn't working P0 Highest priority, will receive attention

Comments

@serranomorante
Copy link

serranomorante commented Jan 13, 2024

Did you check the docs and existing issues?

  • I have read the docs
  • I have searched the 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?

  • I have confirmed that the bug reproduces with nvim -u repro.lua using the repro.lua file above.
@serranomorante serranomorante added the bug Something isn't working label Jan 13, 2024
@serranomorante
Copy link
Author

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 stevearc added the P0 Highest priority, will receive attention label Jan 21, 2024
@stevearc
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working P0 Highest priority, will receive attention
Projects
None yet
Development

No branches or pull requests

2 participants