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

Auto save enabled doesn't work #275

Closed
shourov-rahman opened this issue Mar 12, 2024 · 19 comments
Closed

Auto save enabled doesn't work #275

shourov-rahman opened this issue Mar 12, 2024 · 19 comments

Comments

@shourov-rahman
Copy link

I want to save session buffer automatically before exit nvim with :wqa. I enable auto_save_enabled = true. But It doesn't work. Here is my configuration.

return {
	"rmagatti/auto-session",
	config = function()
		local auto_session = require("auto-session")

		auto_session.setup({
			auto_save_enabled = true,
			auto_restore_enabled = false,
			auto_session_suppress_dirs = { "~/", "~/Dev/", "~/Downloads", "~/Documents", "~/Desktop/" },
		})

		local keymap = vim.keymap

		keymap.set("n", "<leader>wr", "<cmd>SessionRestore<CR>", { desc = "Restore session for cwd" }) -- restore last workspace session for current directory
		keymap.set("n", "<leader>ws", "<cmd>SessionSave<CR>", { desc = "Save session for auto session root dir" }) -- save workspace session for current working directory
	end,
}
@rmagatti
Copy link
Owner

Can you still reproduce if you don't pass these two options at startup?

auto_save_enabled = true,
auto_restore_enabled = false,

@andrevmatos
Copy link

I also noticed auto save stops working on some sessions.
I have cwd_change_handling = false;, and cd to subdirs on certain tabs. I'd like the whole session (all tabs) to still be tracked according to the original cwd session (be it from restore, or cwd of explicit :SessionSave), but I think changing cwd on certain tabs makes auto-session try to track session by these new cwds, and fail to do so, leaving a non-updated session in place for next restore.

@rmagatti
Copy link
Owner

rmagatti commented Apr 1, 2024

cd changes the cwd for the whole Neovim instance. :h cd. You can probably try :h lcd or :h tcd instead. Auto session runs getcwd() so whatever that returns is what it'll try to operate on.

@andrevmatos
Copy link

I usually change cwd through NeoTree, which probably do one of the others, as I can see different :pwd on each tab. Nonetheless, this still seems to mess with auto-save (a previously/explicitly saved session).

@steakhutzeee
Copy link

steakhutzeee commented Apr 7, 2024

I noticed that :SessionSave saves the session file and I can restore it automatically with nvim.
If I do not issue the command manually, the session file is created for the correct directory, but there is no mention to the edited files in it, so nothing is restored automatically with nvim.

My config:

local M = {
  "rmagatti/auto-session",
}

function M.config() 
  require("auto-session").setup {
    log_level = "error",
    --auto_session_suppress_dirs = { "~/", "~/Projects", "~/Downloads", "/"},
    auto_save_enabled = true,
    auto_restore_enabled = true,
  }
end

vim.o.sessionoptions="blank,buffers,curdir,folds,help,tabpages,winsize,winpos,terminal,localoptions"

return M

set sessionoptions? returns blank,buffers,curdir,folds,help,tabpages,winsize,winpos,terminal,localoptions

@rmagatti
Copy link
Owner

Can you still reproduce if you don't pass these two options at startup?

auto_save_enabled = true,
auto_restore_enabled = false,

My question is the same still, what happens if you don't pass these options at all? (they are already the default)
I suspect something might be wrong with the handling of when these are set manually.

@steakhutzeee
Copy link

I'm actually using https://github.com/Shatur/neovim-session-manager

Actually from the docs I see that the 2 values have nil as default value. That's why I set the options up in the first place.

@wonrax
Copy link

wonrax commented May 7, 2024

Can you still reproduce if you don't pass these two options at startup?

auto_save_enabled = true,
auto_restore_enabled = false,

My question is the same still, what happens if you don't pass these options at all? (they are already the default) I suspect something might be wrong with the handling of when these are set manually.

It still won't autosave with the two options unset, here's my config:

return {
  {
    'rmagatti/auto-session',
    config = function()
      vim.o.sessionoptions = 'blank,buffers,curdir,folds,help,tabpages,winsize,winpos,terminal,localoptions'

      require('auto-session').setup {
        log_level = 'info',
        auto_session_enable_last_session = true,
        auto_session_root_dir = vim.fn.stdpath 'data' .. '/sessions/',
        auto_session_enabled = true,
        -- auto_save_enabled = true,
        -- auto_restore_enabled = false,
        auto_session_suppress_dirs = nil,
        auto_session_use_git_branch = nil,
        -- the configs below are lua only
        bypass_session_save_file_types = nil,
      }
    end,
    lazy = false,
  },
}

@rmagatti
Copy link
Owner

@wonrax can you set log_level=vim.log.levels.DEBUG and paste in the results of starting neovim stopping/starting when you expect a session to be loaded or saved automatically?

@wonrax
Copy link

wonrax commented May 15, 2024

I don't know how to pause to get the messages when doing neovim quit all, but session autoload works fine when I have manually created a save before. It just won't autosave on neovim quit.

@andrevmatos
Copy link

Yes, I see getcwd usage is quite widespread in the code, but that can be undesired in a scenario where user tcd and lcd in different directories per tab (sometimes, without a tab remaining in the original directory), and getcwd always return the current cwd instance (local, tab or global).
IMO the expected approach would be that only global cwd is used to pick current session to load or save, even if user tcd'ed to different directories in all tabs.

@cameronr
Copy link
Collaborator

cameronr commented Jul 4, 2024

@wonrax you can run the autosaving logic without exiting using this command

:lua require('auto-session').AutoSaveSession()

Then you should be able to check the messages via :messages (or :Notifications if you use something like nvim-notify)

@sakithb
Copy link

sakithb commented Aug 22, 2024

I had the same issue. My projects folder is symlinked from /mnt/projects to ~/projects, and I had auto_session_allowed_dirs set to { "~/projects/*" }. Changing it to { "/mnt/projects/*" } worked for me.

@cameronr
Copy link
Collaborator

@sakithb Ah, interesting. I think we can fix it so symlinks will match. I'll take a look.

Is anyone else in this thread still having problems with auto-saving? The code has changed a lot since the original issue so I want to see if anyone is still having trouble.

@DUOLabs333
Copy link

I just tried out this library and I have the same problem --- launching nvim creates the session, but autosave/autoreload will not work for that directory unless I run :SessionSave at least once in the directory (I have to repeat this process for every directory I want to create a session for).

@DUOLabs333
Copy link

DUOLabs333 commented Aug 26, 2024

Never mind, I saw what the problem was --- the problem was that I was testing the plugin by doing nvim file.txt; nvim. However, according to the README, the whole auto-session machinery is disabled when a file is specified as an argument, so of course nothing got saved. Doing (nvim, :edit file.txt, :qa!, nvim) works as expected.

This also explains why :SessionSave was seemingly necessary to "kickstart" the autosave process --- without it, the only session that would be saved would be the empty session, so loading the session would make it appear that the plugin itself wasn't working correctly.

In short: PEBCAK.

@cameronr
Copy link
Collaborator

@sakithb

I had the same issue. My projects folder is symlinked from /mnt/projects to ~/projects, and I had auto_session_allowed_dirs set to { "~/projects/*" }. Changing it to { "/mnt/projects/*" } worked for me.

The code to support symlinks is now pushed so this should now work. If you test it, let me know!

@rmagatti
Copy link
Owner

Closing. Can re-open if needed

@sakithb
Copy link

sakithb commented Aug 30, 2024

It works now!

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

8 participants