olimorris / persisted.nvim

💾 Simple session management for Neovim with git branching, autoloading and Telescope support
MIT License
437 stars 26 forks source link

[Bug]: Session overwrite when switch via telescope? #62

Closed tmpm697 closed 1 year ago

tmpm697 commented 1 year ago

Your Persisted.nvim config

return { "olimorris/persisted.nvim", lazy = false,

config = function()
    require("persisted").setup({
        save_dir = vim.fn.expand(vim.fn.stdpath("data") .. "/sessions/"), -- directory where session files are saved
        silent = false, -- silent nvim message when sourcing session file
        use_git_branch = false, -- create session files based on the branch of the git enabled repository
        autosave = true, -- automatically save session files when exiting Neovim
        should_autosave = nil, -- function to determine if a session should be autosaved
        autoload = true, -- automatically load the session for the cwd on Neovim startup
        on_autoload_no_session = nil, -- function to run when `autoload = true` but there is no session to load
        follow_cwd = true, -- change session file name to match current working directory if it changes
        allowed_dirs = nil, -- table of dirs that the plugin will auto-save and auto-load from
        ignored_dirs = nil, -- table of dirs that are ignored when auto-saving and auto-loading
        telescope = { -- options for the telescope extension
            reset_prompt_after_deletion = true, -- whether to reset prompt after session deleted
        },
    })

    require("telescope").load_extension("persisted") -- To load the telescope extension
end,

}

autocmd.lua

autocmd({ "User" }, {
    pattern = "PersistedTelescopeLoadPre",
    group = augroup,
    callback = function()
        vim.schedule(function()
            pcall(vim.cmd, "SessionSave")
            pcall(vim.cmd, "silent %bd!")
        end)
    end,
})

Error messages

No response

Describe the bug

Reproduce the bug

Steps:
1. cd /path/to/folder1
2. nvim
3. open some buffers
4. :bd!
5. nvim #sesison restored correctly
6. :bd!

9. cd /path/to/folder2
10. nvim
11. open some  buffers
12. :bd! #trigger autosave session
13. nvim #restored correctly
14. :bd!
15. nvim
16. :Telescope persisted 
17. <select session of folder1>
18. <switched successfully to session folder1>
19. :bd!
20. nvim #still under /path/to/folder2
21. <now nvim auto restore to session of folder1, :pwd show that it's folder1's path>

Expected here that when under folder1 or folder2, start nvim will restore session of each. when switch via telescope, all current buffers of current session will be tried to del if not having un-saved/modifed files and then switch cwd and restore the selecting session from telescope. Do not overwrite current session next session (currently it did)

NVIM v0.9.0-dev-1260+ga7b537c7 latest persisted.nvim

Final checks

tmpm697 commented 1 year ago

If i remove pcall(vim.cmd, "SessionSave"), it seems to work but i.e: you had sesison of folder1 with two buffers buf1,buf2 then nvim under folder1 will restore two buffers as expected, then you open buf3 and call :Telescope persisted to switch to another session --> now session folder1 still only have two buffers --> show be 3.

My setup works before and current idk from neovim or persisted, it stop working or become junky.

olimorris commented 1 year ago

@tmpm697 can you use the minimal.lua file in the initial bug report and share it so I can try and recreate it.

It's like searching for a needle in a haystack without it.

olimorris commented 1 year ago

Will reopen when you can recreate with a minimal.lua file

tmpm697 commented 1 year ago

just tested with only two plugins telescope.nvim and persisted.nvim the issue still happnes, overlap/overwrite/mess up saved sessions.

prepare:

  1. clone persisted.nvim/, plenary.nvim/, telescope.nvim/ to ~/.local/share/nvim/site/pack/default/start
  2. vi init.lua

minimal init.lua:

-- other require() for mapping, options and autocmd
autocmd({ "User" }, {
    pattern = "PersistedTelescopeLoadPre",
    group = augroup,
    callback = function()
        vim.schedule(function()
            pcall(vim.cmd, "silent %bd!")
        end)
    end,
})

-- require for telescope.nvim

require("persisted").setup({
  save_dir = vim.fn.expand(vim.fn.stdpath("data") .. "/sessions/"), -- directory where session files are saved
  silent = true, -- silent nvim message when sourcing session file
  use_git_branch = false, -- create session files based on the branch of the git enabled repository
  autosave = true, -- automatically save session files when exiting Neovim
  autoload = true, -- automatically load the session for the cwd on Neovim startup
  on_autoload_no_session = nil, -- function to run when `autoload = true` but there is no session to load
  follow_cwd = true, -- change session file name to match current working directory if it changes
  allowed_dirs = nil, -- table of dirs that the plugin will auto-save and auto-load from
  ignored_dirs = { "/tmp" }, -- table of dirs that are ignored when auto-saving and auto-loading
  should_autosave = nill
  telescope = { -- options for the telescope extension
    reset_prompt_after_deletion = true, -- whether to reset prompt after session deleted
  },
})
require("telescope").load_extension("persisted") -- To load the telescope extension

and default telescope.nvim setup (if you also need it, please let me know)

steps:

  1. cd to folderA (can have path like /path/to/folderA) and open some files via :e filenames
  2. quit (:wq) to trigger auto save --> new session created and saved to ~/.local/share/nvim/sessions/
  3. cd to folderB and open some files
  4. quit to trigger auto save
  5. exit nvim
  6. cd to folderA
  7. switch to folderB using telescope
  8. quit to trigger auto save ---> Now saved session file from folderB will overwrite saved session file from folderA, switch between session A and B now only see session B

both follow_cwd = true or follow_cwd = false give same result

expected: with above settings, switch session will clean up current buffers and restore switching session, switch back and forth does not messup saved sessions.

I did not observe this issue until recent refactor that introduce: https://github.com/olimorris/persisted.nvim#events--callbacks @olimorris

tmpm697 commented 1 year ago

@olimorris can you re-open this?

olimorris commented 1 year ago

This is my hook:

  {
    name = "PersistedHooks",
    {
      "User",
      function(session)
        require("persisted").save()

        -- Delete all of the open buffers
        vim.api.nvim_input("<ESC>:%bd!<CR>")

        -- Don't start saving the session yet
        require("persisted").stop()
      end,
      opts = { pattern = "PersistedTelescopeLoadPre" },
    },
  },

That seems to do everything you're asking. It's in a different format as I use the legendary.nvim plugin

tmpm697 commented 1 year ago

Thanks, I'll play around and give it a try.

tmpm697 commented 1 year ago

yes, the only need to change to make this work is:

require("persisted").stop()

have no idea but if not this cause so much trouble for me. overlapping session make it unusable for a quit of time.

thanks.

tmpm697 commented 1 year ago

but for example, when i switch to a session and start to open some buffers there like:

  1. at folderA (sessionA)
  2. switch to folderB (sessionB)
  3. start to open some buffers in this sessionB
  4. switch to folderC (sessionC)
  5. now switch back to folderB (sessionB) ---> opened buffers at step 3 did not registered to sessionB (they're not saved to sessionB)

How to have buffers in step 3 in sessionB between switch?

should I open new issue?

olimorris commented 1 year ago

You'll need to manually save the session before you switch. In your steps above I can't see where you're saving it

tmpm697 commented 1 year ago

You'll need to manually save the session before you switch. In your steps above I can't see where you're saving it

can i autosave it when i switch session at step 4? manually save session is not ideal for me.

if yes, what should this can be changed to?

      function(session)
        require("persisted").save()
        vim.api.nvim_input("<ESC>:%bd!<CR>")
        require("persisted").stop()
      end,

EDIT: afiak if autosave look for current cwd to save session, the target session will be overwrite by current one --> this is the #1 issue

olimorris commented 1 year ago

Autosave will only work when you exit Neovim.

But I've just pushed a commit through as you were right it won't actually save the correct session. If you try this:

function(session)
  require("persisted").save({ session = session.data.file_path })

  -- Delete all of the open buffers
  vim.api.nvim_input("<ESC>:%bd!<CR>")

  -- Don't start saving the session yet
  require("persisted").stop()
end,

It should now work.

tmpm697 commented 1 year ago

Does not work for me, use 06946ed commit and

function(session)
  require("persisted").save({ session = session.data.file_path })

  -- Delete all of the open buffers
  vim.api.nvim_input("<ESC>:%bd!<CR>")

  -- Don't start saving the session yet
  require("persisted").stop()
end,

clean up all sessions, switch from sessionB to sessionA will overwrite sessionA by sessionB.

olimorris commented 1 year ago

Think I've solved it. If you update the plugin and try require("persisted").save({ session = vim.g.persisted_loaded_session })

After a session loads, the global variable gets updated with the path to that session allowing you to manually save it.

tmpm697 commented 1 year ago

Think I've solved it. If you update the plugin and try require("persisted").save({ session = vim.g.persisted_loaded_session })

After a session loads, the global variable gets updated with the path to that session allowing you to manually save it.

thank you!, this save me a ton, probably others.