gennaro-tedesco / nvim-possession

πŸ“Œ the no-nonsense session manager
MIT License
215 stars 7 forks source link

Breaks on terminal #32

Closed Seybo closed 10 months ago

Seybo commented 10 months ago

if a session is saved with terminal (tab in my case) opened it can't be loaded anymore

[Fzf-lua] fn_selected threw an error: ...e/nvim/lazy/nvim-possession/lua/nvim-possession/init.lua:73: /home/glaux/.local/share/
nvim/sessions/test, line 103: Vim(normal):Can't re-enter normal mode from terminal mode
stack traceback:
^I...laux/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/core.lua:179: in function <...laux/.local/share/nvim/lazy/fzf-lua/lua/fzf-
lua/core.lua:178>
^I[C]: in function 'source'
^I...e/nvim/lazy/nvim-possession/lua/nvim-possession/init.lua:73: in function 'action'
^I...x/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/actions.lua:72: in function 'act'
^I...laux/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/core.lua:93: in function 'fn_selected'
^I...laux/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/core.lua:177: in function <...laux/.local/share/nvim/lazy/fzf-lua/lua/fzf-
lua/core.lua:176>
^I[C]: in function 'xpcall'
^I...laux/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/core.lua:176: in function <...laux/.local/share/nvim/lazy/fzf-lua/lua/fzf-
lua/core.lua:170>

Ubuntu 22.04.3 LTS and Nvim:

NVIM v0.10.0-dev
Build type: RelWithDebInfo
LuaJIT 2.1.0-beta3
Run "nvim -V1 -v" for more info
Seybo commented 10 months ago

workaround to destroy terminal tabs before save

possession.setup({
    save_hook = function()
        -- close terminal tabs as they can't be restored and break plugin
        local tabpage_count = vim.fn.tabpagenr("$")                                 -- Get the number of tabpages
        for i = 1, tabpage_count do
            local win_ids = vim.api.nvim_tabpage_list_wins(i)                       -- Get window IDs in tabpage
            for _, win_id in ipairs(win_ids) do
                local bufnr = vim.api.nvim_win_get_buf(win_id)                      -- Get buffer number
                if vim.api.nvim_buf_get_option(bufnr, "buftype") == "terminal" then -- Check if buffer is terminal
                    vim.api.nvim_command(i .. "tabclose")                           -- Close the tabpage
                    return
                end
            end
        end
    end,
})
gennaro-tedesco commented 10 months ago

Good morning and thank you for bringing the issue to my attention!

if a session is saved with terminal (tab in my case) opened it can't be loaded anymore

Could I ask to better specify what this means? Do you mean a session where some buffers are terminal buffers? If so, are you using neovim native terminal or some other terminal plugins? I have tried saving the session after opening a native terminal buffer and it all works fine. Could you indicate the steps you are taking so that I can reproduce?

Seybo commented 10 months ago

Yeah. I have 5 tabs. In the last one i have neovim terminal opened (:term). It seems to save this session with no problems. But loading it brings the error that i showed, it opens all the saved tabs but the session is not actually loaded (which can be seen by trying to save it)

gennaro-tedesco commented 10 months ago

Yeah. I have 5 tabs

What are you neovim session options? For vim to recognise tabs in sessions you must explicitly specify them (see :h sessionoptions, you must activate tabpages, terminal).

Could you try to reproduce it with buffers only, to understand whether this is due to tab or terminal session options?

  1. open an empty buffer
  2. open a terminal buffer
  3. save the session
  4. reload the session

in my case the above works fine.

Seybo commented 10 months ago
  1. opent an empty buffer
  2. command :term in it
  3. save the session
  4. update the session => see the error
    
    saved in: /home/glaux/.local/share/nvim/sessions/test2
    overwrite session?

E5108: Error executing lua: /home/glaux/.config/nvim/lua/plugins.lua:111: Vim(tabclose):E784: Cannot close last tab page stack traceback: [C]: in function 'nvim_command' /home/glaux/.config/nvim/lua/plugins.lua:111: in function 'save_hook' ...e/nvim/lazy/nvim-possession/lua/nvim-possession/init.lua:56: in function 'update' /home/glaux/.config/nvim/lua/plugins.lua:125: in function </home/glaux/.config/nvim/lua/plugins.lua:124>

5. close vim
6. open vim
7. load previous session => see the error

[Fzf-lua] fn_selected threw an error: ...e/nvim/lazy/nvim-possession/lua/nvim-possession/init.lua:73: /home/glaux/.local/share/ nvim/sessions/test2, line 36: Vim(normal):Can't re-enter normal mode from terminal mode stack traceback: ^I...laux/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/core.lua:179: in function <...laux/.local/share/nvim/lazy/fzf-lua/lua/fzf- lua/core.lua:178> ^I[C]: in function 'source' ^I...e/nvim/lazy/nvim-possession/lua/nvim-possession/init.lua:73: in function 'action' ^I...x/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/actions.lua:72: in function 'act' ^I...laux/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/core.lua:93: in function 'fn_selected' ^I...laux/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/core.lua:177: in function <...laux/.local/share/nvim/lazy/fzf-lua/lua/fzf- lua/core.lua:176> ^I[C]: in function 'xpcall' ^I...laux/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/core.lua:176: in function <...laux/.local/share/nvim/lazy/fzf-lua/lua/fzf- lua/core.lua:170>

Seybo commented 10 months ago

What are you neovim session options? For vim to recognise tabs in sessions you must explicitly specify them (see :h sessionoptions, you must activate tabpages, terminal).

i don't don't have any special sessionoptions set. ~:tab new works out of the box, no?~ (UPD: misunderstood)

Seybo commented 10 months ago

this is my sessionoptions and it is set by default: sessionoptions=blank,buffers,curdir,folds,help,tabpages,winsize,terminal

Seybo commented 10 months ago

just tried it with an empty config, single plugin. And the error is there

init.lua

vim.g.mapleader = " " -- must be set prior plugins initialization
require "plugins"

plugins.lua

local lazypath = vim.fn.stdpath("data") .. "/lazy/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",
        "--branch=stable", -- latest stable release
        lazypath,
    })
end

vim.opt.rtp:prepend(lazypath)

local lazy = require "lazy"

local plugins = {
    { -- sessions management
        "gennaro-tedesco/nvim-possession",
        version = "*",
        dependencies = {
            "ibhagwan/fzf-lua",
        },
        config = true,
        init = function()
            local possession = require("nvim-possession")
            vim.keymap.set("n", "<leader>sl", function()
                possession.list()
            end)
            vim.keymap.set("n", "<leader>sn", function()
                possession.new()
            end)
            vim.keymap.set("n", "<leader>su", function()
                possession.update()
            end)
            vim.keymap.set("n", "<leader>sd", function()
                possession.delete()
            end)
        end,
    },
}
local options = {
    defaults = {
        lazy = false,
    },

    performance = {
        cache = {
            enabled = true,
        },

        rtp = {
            disabled_plugins = {
                "gzip",
                -- "netrwPlugin",
                "rplugin",
                "tarPlugin",
                "tohtml",
                "tutor",
                "zipPlugin",
            },
        },
    },

    ui = {
        size = { width = 0.9, height = 0.9 },
        border = { "┏", "━", "β”“", "┃", "β”›", "━", "β”—", "┃" },
    },

    checker = {
        -- automatically check for plugin updates
        enabled = false,
    },
}

lazy.setup(plugins, options)
gennaro-tedesco commented 10 months ago

I have used the aforementioned minimal init.lua and I have observed the following:

  1. save the session
  2. update the session => see the error

I do not see any error while doing so: what version of neovim are you on? P. S. you need not update the session after saving it, saving it suffices. Do you still see an error if just saving a session consisting of a normal buffer and a terminal buffer?

  1. load previous session => see the error

interestingly enough here I only see the error if the last buffer I operate on is the terminal buffer, namely if - after opening the terminal buffer - I switch back to any other normal buffer then no such error occurs. By looking at the stacktrace I suspect this is due to neovim trying to remember the last cursor position to be in terminal mode and being unable to re-enter normal mode upon loading the session: I have found a similar issue opened in core.

Can you confirm this behaviour, namely that if you switch back to a normal buffer and save the session then no error occurs?

Seybo commented 10 months ago

what version of neovim are you on?

it's in my initial post

NVIM v0.10.0-dev
Build type: RelWithDebInfo
LuaJIT 2.1.0-beta3

you need not update the session after saving it, saving it suffices. Do you still see an error if just saving a session consisting of a normal buffer and a terminal buffer?

yeah, i know i don't need to do that. It's just the quickest way to repro the issue. Saving a new session doesn't raise any error. Loading it raises error and updating it as well. Loading does load the tabs and everything (looks like) but the session itself seems to be not set (because when trying to update it there is a message something like no session)

Can you confirm this behaviour, namely that if you switch back to a normal buffer and save the session then no error occurs?

I'm not sure i follow. Are you suggesting to:

gennaro-tedesco commented 10 months ago

If that's what you mean, then yeah, no errors there. But no surprise as terminal buffer is not present in the saved session at all

the terminal buffer is present in the saved session, is it not? See screenshot below to reproduce:

https://github.com/gennaro-tedesco/nvim-possession/assets/15387611/c6687092-6c76-42ca-ab53-90e5e12085dd

Is the terminal buffer no longer present in your case?

Seybo commented 10 months ago

yeah, my bad, it is there, terminal is there and no errors are there. So yeah, the problem is only when terminal buffer is active

gennaro-tedesco commented 10 months ago

In which case I wouldn't really know what should be the intended behaviour (as it is a neovim problem, as per issue above): one solution could be to write a save_hook as you did, I wouldn't however make it default part of the plugin as it could generate unwanted behaviours (i. e. wiping off terminal buffers unwantedly).

Seybo commented 10 months ago

yeah, i guess i'm ok with my solution. Ideally would be to have a setting for this. Like auto_close_active_terminal_buffers = false (default). But that's not i big issue. Feel free to close it