auto-session
auto-session copied to clipboard
AutoSave ignores current loaded session when there's multiple local cwd's
Problem: After leaving Neovim when on a tab with a different local cwd than the restored session's name, session is saved as the cwd name, and not the current loaded session.
Expected: If a session is loaded (or has been restored), Autosave should save the loaded session name from v:this_session, regardless of the local cwd.
Reproduce:
- Setup with
auto_session_enable_last_session = false,
auto_session_enabled = true,
auto_save_enabled = true,
auto_restore_enabled = true,
- Open
nvim, open a few files and close Neovim to save the session. - Open
nvimagain, session should be restored. - Create a new tab,
:tabnewand:lcd ~/different/projectand open a file from the other project. - Without changing to a different tab, close Neovim.
- Session name saved is the last tab's cwd, which is unexpected, and ignores
v:this_sessionfor the current loaded session.
I see. This is an interesting one, the plugin currently doesn't keep track of the cwd itself but uses whatever is set at the time of session saving, which explains the issue but solving it might not be trivial.
I'll give it some more thought before actioning this. Thanks for the submission!
@rmagatti this issue started happening again after latest changes with cwd, even with cwd_change_handling = false.
@rmagatti any clue what could have broken this? This is my configuration:
local HOME = vim.fn.expand('$HOME')
require('auto-session').setup({
log_level = 'error',
auto_session_enable_last_session = false,
auto_session_enabled = true,
auto_save_enabled = true,
auto_restore_enabled = true,
auto_session_suppress_dirs = { '/etc', '/tmp', HOME, HOME .. '/code' },
pre_save_cmds = { 'lua require("interface").win.close_plugin_owned()' },
cwd_change_handling = false,
})
-- lua/interface.lua
-- Close plugin owned windows.
interface.win.close_plugin_owned = function()
-- Jump to preview window if current window is plugin owned.
if interface.win.is_plugin_owned(0) then
vim.cmd([[ wincmd p ]])
end
for _, win in ipairs(fn.getwininfo()) do
if interface.win.is_plugin_owned(win.bufnr) then
-- Delete plugin owned window buffers.
api.nvim_buf_delete(win.bufnr, {})
end
end
end
-- Detect if window is owned by plugin by checking buftype.
interface.win.is_plugin_owned = function(bufid)
local origin_type = api.nvim_buf_get_option(bufid, 'buftype')
if origin_type == '' or origin_type == 'help' then
return false
end
return true
end
Not quite yet. I'll have to do some digging, just haven't had the time lately unfortunately.
@rafi can you set the log_level to debug and paste here what it prints when you encounter the issue you're describing?
I think i have similar problem. I use cwd in monorepo but I don't want sessions saved under each package/service which is happening now. I need session saved only in the folder that I opened with, which is usually root of monorepo
@kuba-gaj, Auto Session uses the cwd to derive the session from. So if you're switching cwds within a monorepo the behaviour you're seeing makes sense given how the plugin currently works. That said I suppose there could be a setting to allow for storing what session was initially loaded in memory, and save only that session. This would have to be carefully coded though as to not break things like rmagatti/session-lens.
Re-produce:
Create two directories and start a session in a:
cd ~
mkdir tmp && cd $_
mkdir a b
touch a/a.txt b/b.txt
cd a
nvim
Now open a file, and create a new tab with different lcd and open a file and quit all:
:e a.txt
:tabnew
:lcd ../b
:e b.txt
:qall
Now run nvim again.
Expected: Open session with the same exact tabs, one whose lcd is a, and 2nd with lcd of b.
Actual: Nothing. The new session was created as %Users%foo%tmp%b.vim instead of a.
Specially when auto_restore_enabled is set to true, it makes a lot of sense to use v:this_session, or when creating the session for the first time, use the initial working-directory nvim was opened from..