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

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

Open
rafi opened this issue Jul 11, 2021 · 8 comments
Open

Comments

@rafi
Copy link

rafi commented Jul 11, 2021

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.
@rmagatti
Copy link
Owner

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!

@rafi
Copy link
Author

rafi commented Aug 11, 2022

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

@rmagatti rmagatti reopened this Aug 12, 2022
@rafi
Copy link
Author

rafi commented Sep 15, 2022

@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

@rmagatti
Copy link
Owner

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

@rmagatti
Copy link
Owner

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

@punk-dev-robot
Copy link

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

@rmagatti
Copy link
Owner

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

@rafi
Copy link
Author

rafi commented Oct 2, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants