auto-session icon indicating copy to clipboard operation
auto-session copied to clipboard

AutoSave ignores current loaded session when there's multiple local cwd's

Open rafi opened this issue 4 years ago • 8 comments

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:

  1. Setup with
auto_session_enable_last_session = false,
auto_session_enabled = true,
auto_save_enabled = true,
auto_restore_enabled = true,
  1. Open nvim, open a few files and close Neovim to save the session.
  2. Open nvim again, session should be restored.
  3. Create a new tab, :tabnew and :lcd ~/different/project and open a file from the other project.
  4. Without changing to a different tab, close Neovim.
  5. Session name saved is the last tab's cwd, which is unexpected, and ignores v:this_session for the current loaded session.

rafi avatar Jul 11 '21 13:07 rafi

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 avatar Jul 12 '21 21:07 rmagatti

@rmagatti this issue started happening again after latest changes with cwd, even with cwd_change_handling = false.

rafi avatar Aug 11 '22 11:08 rafi

@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

rafi avatar Sep 15 '22 12:09 rafi

Not quite yet. I'll have to do some digging, just haven't had the time lately unfortunately.

rmagatti avatar Sep 15 '22 20:09 rmagatti

@rafi can you set the log_level to debug and paste here what it prints when you encounter the issue you're describing?

rmagatti avatar Sep 18 '22 08:09 rmagatti

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

punk-dev-robot avatar Sep 26 '22 12:09 punk-dev-robot

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

rmagatti avatar Sep 26 '22 15:09 rmagatti

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

rafi avatar Oct 02 '22 12:10 rafi