[Bug]: Autosave Not Triggered Without Existing Session File
tummetott opened this issue · 8 comments
Your minimal.lua
config
Copy paste of the above config, no configurations
local root = "/tmp/persisted"
-- Set stdpaths to use root dir
for _, name in ipairs({ "config", "data", "state", "cache" }) do
vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end
-- Bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
vim.fn.system({
"git",
"clone",
"--filter=blob:none",
"--single-branch",
"https://github.com/folke/lazy.nvim.git",
lazypath,
})
end
vim.opt.runtimepath:prepend(lazypath)
vim.opt.sessionoptions = "buffers,curdir,folds,globals,tabpages,winpos,winsize" -- Session options to store in the session
-- Install plugins
local plugins = {
{
"olimorris/persisted.nvim",
opts = {
-- Your custom config here
}
},
-- Put any other plugins here
}
require("lazy").setup(plugins, {
root = root .. "/plugins",
})
Error messages
no error message
Describe the bug
The autosave
feature doesn't function if no session file exists initially. To enable autosave for a directory, you need to manually create a sessions file by running :SessionSave
. Once this file is in place, autosave operates as expected.
However, when opening Neovim without a file and subsequently closing it, an empty session file is automatically generated, allowing autoload to function correctly thereafter.
Reproduce the bug
- Run:
nvim --clean -u minimal.lua minimal.lua
- Close nvim:
:q
- Open nvim:
nvim
- Run:
:SessionLoad
- No session is loaded
Final checks
- I have made sure this issue exists in the latest version of the plugin
- I have tested with the
minimal.lua
config file above and still get the issue - I have used
SessionSave
to save the session before restarting Neovim and usingSessionLoad
- I have made sure this is not a duplicate issue
Thanks for raising this and your detailed minimal.lua file. Can confirm I can recreate this so will take a look at this in the coming days.
@tummetott I've created a new branch, fix/auto-creating-sessions. Could you check it out and test when you get a moment?
The issue persists. I've tested your branch, deleted the session files, but they are not automatically created. Furthermore, even when simply running nvim
and then closing it, the session files are no longer generated.
I've misunderstood the initial issue then. Because using your minimal.lua
file, I could open up Neovim in a directory with no session file, execute :q
and see a session appear
Did you open neovim with an argument? Or just neovim with no file as argument? For the latter, the Session file is crated, for the former not.
Okay so just pushed a new feature to the branch. By default, no session should be created if autocreate = false
(which it is by default).
Ok with autocreate = true
, the session file is created. However now I get errors from time to time:
E5108: Error executing lua: ...hare/nvim/lazy/persistence.nvim/lua/persistence/init.lua:81: attempt to concatenate field 'dir' (a nil value)
stack traceback:
...hare/nvim/lazy/persistence.nvim/lua/persistence/init.lua:81: in function 'list'
...hare/nvim/lazy/persistence.nvim/lua/persistence/init.lua:19: in function 'get_last'
...hare/nvim/lazy/persistence.nvim/lua/persistence/init.lua:74: in function 'load'
In my opinion, introducing a new option called autocreate
might lead to confusion for users. The expected behavior, when setting autosave = true
, is that saving should occur automatically. However, with the proposed autocreate
setting, it implies that saving will only work when both autosave
and autocreate
are set to true.
A more intuitive approach could be to change the behavior as follows: when autosave = true
, session files are always created automatically because the user expects autosave to function seamlessly. If a different behavior is desired, such as saving only when session files were created before explicitly with :SessionSave
, the option value autosave = "known_dir"
(or another suitable name) could be introduced.
Following your steps to reproduce the bug, the issue comes down to this. If I remove this it means anytime someone opens up Neovim with an argument, it will automatically save the session...potentially overwriting an existing one.
Appreciate I've been dipping into this issue every couple of days and not given it some dedicated time but open to a PR which could resolve it.