rmagatti / auto-session

A small automated session manager for Neovim
MIT License
1.2k stars 49 forks source link

[BUG] when changing current folder, old session file gets cleared #205

Closed nnako closed 1 year ago

nnako commented 1 year ago

Describe the bug Within a development session (with some buffers and tabs open, as well as a freshly saved session file done e.g. by SaveSession), changing the current folder to something else using cd <new-folder-path> re-initializes the session file of the former session.

To Reproduce Steps to reproduce the behavior:

  1. open nvim
  2. move into favorite (or test) folder
  3. open some tabs and buffers
  4. create session by executing SaveSession -> a session file is created within the respective session folder
  5. leave the current folder by e.g. cd .. -> a message is shown in the nvim status line saying "... buffers were deleted" -> the former session file has been re-initialized (see e.g. file size as hint)

Expected behavior When changing from one current folder to another, I would expect all session files to stay untouched. A write of a session file should only happen on the respective vim autocommand (default would be "nvim leave"). So, only on nvim leave (close nvim), the most recent session file should be updated if configured so.

Baseline (please complete the following information):

rmagatti commented 1 year ago

What does your setup call look like?

rmagatti commented 1 year ago

You are probably not setting cwd_change_handling like the readme mentions

nnako commented 1 year ago

Hi,

I have been and I am still setting cwd_change_handling in my plugin configuraion, like:

  use {
    'rmagatti/auto-session',
    config = function()
      require("auto-session").setup {
        log_level = "error",
        auto_session_suppress_dirs = {
            "~/",
            "~/Projects",
            "~/Downloads",
            "/"
        },
        cwd_change_handling = {
          restore_upcoming_session = true,   -- already the default, no need to specify like this, only here as an example
          pre_cwd_changed_hook = nil,        -- already the default, no need to specify like this, only here as an example
          -- post_cwd_changed_hook = function() -- example refreshing the lualine status line _after_ the cwd changes
          --   require("lualine").refresh()     -- refresh lualine so the new session name is displayed in the status bar
          -- end,
        },
      }
    end
  }

As I am not using lualine, yet, I commented those lines out in order to prevent possible errors. But, anyway, the readme says that those active arguments for cwd_change_handling would be default. They should not make any difference in behavior, right?

rmagatti commented 1 year ago

Yes, correct. The behaviour is a bit weird with cwd_change_handling but the setup function you got there is correct and should work, that said, Windows is not really "officially" supported, as in I don't use Windows myself. It's very possible this is a Windows-specific issue, PRs are welcome if you'd like to dig deeper into it though!

nnako commented 1 year ago

I just configured "debug" mode for auto-session and recorded the outputs while doing the things I mentioned in the messages above. maybe by reading the debug messages, you could give me a hint where to start digging into the code? I would love to help by providing a PR, but as my time is very limited, I would like to ask for your support understanding your code:

After the start of neovim without specifying a specific folder, I see this when issuing :messages:

debug:  ==== Pager mode 
debug:  sessions_dir, session_file
debug:  ==== Using session DIR
debug:  ==== Session Name:
debug:  ==== is_readable 
info:  Session restored from C:\Users\user\AppData\Local\nvim-data/sessions/C++\%PROGS\%Neovim__v0_9_0_dev\%bin.vim

here, everything looks fine to me. the old session was correctly loaded (and the filetype colors are fine). then, after issuing the command cd c:\Users\user\_NEXTCLOUD\_TOOLS\_Python\project1, I recognize that the currently loaded session file has been cleared again. This path is a valid path but without a defined session, yet. which should not be any problem. and I see this within the messages:

debug:  DirChangedPre
debug:    cwd: C:\PROGS\Neovim__v0_9_0_dev\bin
debug:    target: c:\Users\user\_NEXTCLOUD\_TOOLS\_Python\project1\
debug:    changed window: false
debug:    scope: global
debug:  ==== is_allowed_dirs_enabled
debug:  bypass_save_by_filetype: false
debug:  ==== SaveSession
debug:  Session saved at C:\Users\user\AppData\Local\nvim-data/sessions/C++\%PROGS\%Neovim__v0_9_0_dev\%bin.vim
9 buffers deleted
debug:  DirChangedPre
debug:    cwd: C:\PROGS\Neovim__v0_9_0_dev\bin
debug:    target: c:\Users\user\_NEXTCLOUD\_TOOLS\_Python\project1\
debug:    changed window: false
debug:    scope: global
debug:  ==== is_allowed_dirs_enabled
debug:  bypass_save_by_filetype: false
debug:  ==== SaveSession
debug:  Session saved at C:\Users\user\AppData\Local\nvim-data/sessions/C++\%PROGS\%Neovim__v0_9_0_dev\%bin.vim
debug:  DirChangedPre
debug:    cwd: C:\PROGS\Neovim__v0_9_0_dev\bin
debug:    target: c:\Users\user\_NEXTCLOUD\_TOOLS\_Python\project1\
debug:    changed window: false
debug:    scope: global
debug:  ==== is_allowed_dirs_enabled
debug:  bypass_save_by_filetype: false
debug:  ==== SaveSession
debug:  Session saved at C:\Users\user\AppData\Local\nvim-data/sessions/C++\%PROGS\%Neovim__v0_9_0_dev\%bin.vim
debug:  DirChanged
debug:    cwd: c:\Users\user\_NEXTCLOUD\_TOOLS\_Python\project1
debug:    changed window: false
debug:    scope: global
debug:  sessions_dir, session_file
debug:  ==== Using session DIR
debug:  ==== Session Name:
debug:  ==== is_readable 
debug:  ==== is_readable 
debug:  File not readable, not restoring session
info:  Could not load session. A session file is likely missing for this cwd.c:\Users\user\_NEXTCLOUD\_TOOLS\_Python\project1
debug:  DirChanged
debug:    cwd: c:\Users\user\_NEXTCLOUD\_TOOLS\_Python\project1
debug:    changed window: false
debug:    scope: global
debug:  sessions_dir, session_file
debug:  ==== Using session DIR
debug:  ==== Session Name:
debug:  ==== is_readable 
debug:  ==== is_readable 
debug:  File not readable, not restoring session
info:  Could not load session. A session file is likely missing for this cwd.c:\Users\user\_NEXTCLOUD\_TOOLS\_Python\project1
debug:  DirChanged
debug:    cwd: c:\Users\user\_NEXTCLOUD\_TOOLS\_Python\project1
debug:    changed window: false
debug:    scope: global
debug:  sessions_dir, session_file
debug:  ==== Using session DIR
debug:  ==== Session Name:
debug:  ==== is_readable 
debug:  ==== is_readable 
debug:  File not readable, not restoring session
info:  Could not load session. A session file is likely missing for this cwd.c:\Users\user\_NEXTCLOUD\_TOOLS\_Python\project1
c:\Users\user\_NEXTCLOUD\_TOOLS\_Python\project1

any tips?

nnako commented 1 year ago

to me, it seems as if the 9 buffers deletes occurs before Session saved even though the messages tell otherwise. Maybe some operating system internal synchronization problem which could be solved by some technical means...

nnako commented 1 year ago

another oddity is that the DirChangePre event seems to be executed multiple times after issuing cd ... . This could be the reason for re-setting the session file in the 2nd event round...

chikko80 commented 1 year ago

to me, it seems as if the 9 buffers deletes occurs before Session saved even though the messages tell otherwise. Maybe some operating system internal synchronization problem which could be solved by some technical means...

I am facing the same problem under OSX. If i change the Session by cd into another project the new project gets restored correctly, if i change back into the old project, all the buffers are deleted. So it seems like the Buffers get deleted first and then the session is saved.

@rmagatti It seems like this is not only related to windows

chikko80 commented 1 year ago

@rmagatti

seems like there is a problem with:

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

if i remove the sessionoptions, the buffers don't get cleared

rmagatti commented 1 year ago

Session options are required for anything to be stored in the session at all so I wouldn't think it's related to that

rmagatti commented 1 year ago

That said, I've had a few issues in the past around saving localoptions, maybe try unsettling that from your sessionoptions. But also, to be clear, buffers should get cleared right after the session is saved so switching to another session is done cleanly without bleeding of buffers between sessions.

PS: I can't reproduce it here.

chikko80 commented 1 year ago

Okay very strange, even if i remove the localoptions i still get the same behavior, every time i switch to another session and go back to the previous all buffers are gone. Like it first removes the buffers and afterwards it saves the session

rmagatti commented 1 year ago

Do you have a video or gif of the problem you can show?

ghost commented 1 year ago

i had the same issue, cwd_change_handling = nil works for me.

my current config:

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

require("auto-session").setup({
    log_level = "error",
    auto_session_create_enabled = false,
    auto_restore_enabled = true,
    auto_save_enabled = true,
    cwd_change_handling = nil,
    session_lens = {
        load_on_setup = true,
        theme_conf = { border = true, winblend = 0 },
    },
})
vim.keymap.set("n", "<leader>pS", require("auto-session.session-lens").search_session, {
    noremap = true,
})

require("telescope").load_extension("session-lens")
rmagatti commented 1 year ago

@levioneyh is correct, there's some handling in the code that checks whether cwd_change_handing has been passed in at all and that triggers some different behaviour currently

nnako commented 1 year ago

Hi folks, hi @rmagatti ,

here is a (hopefully) descriptive screencast about the issue how it appears on my local Windows computer. Please disregard all the speaking mistakes and the missing structure. I would have put in much more time if I wanted to avoid all this ;-) .

Hope it helps clarifying the issue and finally finding a solution for people on Windows systems.

https://github.com/rmagatti/auto-session/assets/5845602/c4430bd0-d099-4840-b22b-32610b85401b

Please see the post up in this thread where I had the "debug" logging level switched on. It would resemble the inner operations of the plugin.

Thanks.

nnako commented 1 year ago

@levioneyh wrote:

i had the same issue, cwd_change_handling = nil works for me.

yes, that "works" in the sense that (at least in my case) it switches off any automatic save / restore operations on change dir. but thus, I have to do everything manually (using :RestoreSession and :SaveSession after / before changing dicrectories), again. what should I use the plugin for, except for storing all my sessions in one default place ;-) ?

ghost commented 1 year ago

@nnako If you want to change between sessions you can use the session_lens extension, it changes the session, the cwd and does not delete the previous session (unless you don't want to use the extension). If you just want to change directories with :cd without the "n buffers were deleted" problem, cwd_change_handling = nil works. :RestoreSession is not needed in any of these cases. :SessionSave doesn't seem to be a problem to me, I just want some sessions and have them loaded when running nvim, be able to move between them and not have the Session.vim in the same directory as my other files. (and that's what I use the plugin for).

nnako commented 1 year ago

@levioneyh thanks again. yes, I will have a look at session_lens and check with my preferred workflow. But, generally, it is a pitty to not be able to use the nice auto-session plugin on Windows systems in a way it was intended to be used. Even worse, I can not even pull the current version of it as then, the plugin does not work at all (for me at least). Hope that session_lens plays nicely with older versions of auto-session. We will see...

nnako commented 1 year ago

Hi folks. Sorry for all the confusion. I just saw that somehow, my local git repo of auto-session got messed up during all the jumping back and forth within the commit tree. In the end, I still e.g. had to use the command :RestoreSession and not the "new" command :SessionRestore to operate on stored sessions. And also, the session keys still looked and worked like this within the session folder:

c++%Users%pkohm%_NEXTCLOUD%_TOOLS%_Python%LIB__freeplane.vim

instead of this:

c++-Users-pkohm-_NEXTCLOUD-_TOOLS-_Python-LIB__freeplane.vim

SOLUTION

So, in the end, I removed my local auto-session repo and cloned it again, manually. The plugin manager is in sync with the current state.

CONCLUSION

Now, the file structure seems the same as seen on the project GitHub site, again. And there are NO WARNINGS and NO ERRORS when starting neovim, anymore. Using the parameter cwd_change_handling = nil switches off the auto buffer save / restore, still. But when using the provided commands :SessionRestore and :SessionSave, after changing directories works fine. Even the file types are restored correctly :-) .

I will close this issue, now, as it I consider it "solved" for me.

Thanks to all of you for your support.