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

[BUG] auto-session seems to always display an error on home directory

Open dasupradyumna opened this issue 2 years ago • 14 comments

Describe Bug With auto-save and auto-load enabled, the plugin behaves as expected in all directories; automatically saving the session when NeoVim is closed and loading the session when reopened. But this expected behavior fails when the working directory is $HOME. NeoVim displays errors before startup is complete and disables autosave for that session.

Steps To Reproduce

  1. Enable the auto-save and auto-load options for auto-session.
  2. Open $HOME directory in NeoVim, do some work and close the session.
  3. Reopen $HOME directory again.
  4. The error (shown below in screenshot) should appear on the screen before NeoVim starts up fully.

Expected Behavior The session should load automatically without any errors displayed.

Screenshot issue_autosession_home_error

System Information

  • Session Options : buffers,curdir,folds,help,localoptions,options,tabpages,terminal,winsize
  • OS : Linux Tensei 5.15.0-58-generic #64~20.04.1-Ubuntu SMP Fri Jan 6 16:42:31 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
  • Neovim version :
NVIM v0.9.0-dev
Build type: RelWithDebInfo
LuaJIT 2.1.0-beta3
Compilation: /usr/bin/cc -g -O2 -fdebug-prefix-map=/build/neovim-oiekze/neovim-0.9.0~ubuntu1+git202301230018-e5a537c88-333b5866f=. -fstack-protector-strong -Wformat -Werror=format-security -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 -O2 -g  -Og -g -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wdouble-promotion -Wmissing-noreturn -Wmissing-format-attribute -Wmissing-prototypes -Wimplicit-fallthrough -Wvla -fno-common -fdiagnostics-color=auto -fstack-protector-strong -DNVIM_UNIBI_HAS_VAR_FROM -DNVIM_MSGPACK_HAS_FLOAT32 -DNVIM_TS_HAS_SET_MATCH_LIMIT -DNVIM_TS_HAS_SET_ALLOCATOR -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -DMIN_LOG_LEVEL=3 -I/usr/include/luajit-2.1 -I/usr/include -I/build/neovim-oiekze/neovim-0.9.0~ubuntu1+git202301230018-e5a537c88-333b5866f/.deps/usr/include -I/build/neovim-oiekze/neovim-0.9.0~ubuntu1+git202301230018-e5a537c88-333b5866f/build/src/nvim/auto -I/build/neovim-oiekze/neovim-0.9.0~ubuntu1+git202301230018-e5a537c88-333b5866f/build/include -I/build/neovim-oiekze/neovim-0.9.0~ubuntu1+git202301230018-e5a537c88-333b5866f/build/cmake.config -I/build/neovim-oiekze/neovim-0.9.0~ubuntu1+git202301230018-e5a537c88-333b5866f/src
Compiled by buildd@lcy02-amd64-048

Features: +acl +iconv +tui
See ":help feature-compile"

  system vimrc file: "$VIM/sysinit.vim"
 fall-back for $VIM: "/usr/share/nvim"

Run :checkhealth for more info

dasupradyumna avatar Jan 29 '23 13:01 dasupradyumna

It is unlikely this would have anything to do with what directory you're trying to load. By the look of it, you're trying to lazyload auto-session, this is not recommended. The link to your config is hitting 404, so I can't tell for sure that's the case.

rmagatti avatar Jan 31 '23 08:01 rmagatti

I get the same error. Here is my config https://github.com/Johannesklint/dotfiles/blob/main/nvim/.config/nvim/lua/primalivet/packer.lua#L66

Johannesklint avatar Feb 06 '23 13:02 Johannesklint

Hey, I'm sorry for the tardy response. Please check the link to my config now, it should work. And I double-checked my config as per your suggestion. I also use your "session-lens" plugin, but neither plugin is lazy-loaded.

dasupradyumna avatar Feb 06 '23 18:02 dasupradyumna

Hey @Johannesklint what happens if you remove ~/ from auto_session_suppress_dirs? Do you still get an error?

rmagatti avatar Feb 06 '23 22:02 rmagatti

Hey @Johannesklint what happens if you remove ~/ from auto_session_suppress_dirs? Do you still get an error?

I have tried that, still getting the same error unfortunately. When running :PackerSync I get this output: ✗ Failed to update rmagatti/auto-session

Johannesklint avatar Feb 07 '23 12:02 Johannesklint

Hey @Johannesklint what happens if you remove ~/ from auto_session_suppress_dirs? Do you still get an error?

I also added that to my config as a bandaid, after creating this issue. This prevents the error in my case, but I lose "auto-session" capabilities in the $HOME directory. :(

dasupradyumna avatar Feb 07 '23 22:02 dasupradyumna

Okay so I've looked a bit deeper into this, this SessionLoad call happens in Neovim itself. I can't really reproduce here but to confirm you can try:

  1. Deleting your current nome dir session, by running :DeleteSession after opening Neovim at the home dir
  2. Manually create a session with :mksess
  3. Close Neovim
  4. Open Neovim
  5. Try loading the session :source Session.vim

You should see the same error even without auto-session involvement. Screenshot 2023-02-07 at 11 13 33 PM

rmagatti avatar Feb 08 '23 06:02 rmagatti

After following your steps it now works...

Okay so I've looked a bit deeper into this, this SessionLoad call happens in Neovim itself. I can't really reproduce here but to confirm you can try:

  1. Deleting your current nome dir session, by running :DeleteSession after opening Neovim at the home dir
  2. Manually create a session with :mksess
  3. Close Neovim
  4. Open Neovim
  5. Try loading the session :source Session.vim

You should see the same error even without auto-session involvement. Screenshot 2023-02-07 at 11 13 33 PM

After following those steps it works! Thanks

Johannesklint avatar Feb 10 '23 09:02 Johannesklint

🤔 Those steps shouldn't have made anything work, there might be something else at play here

rmagatti avatar Feb 10 '23 17:02 rmagatti

That's what's weird. It created a file called Session.vim where tabs and such is stored in a cached session. I guess that's why it now works... 🤔

Johannesklint avatar Feb 11 '23 07:02 Johannesklint

I have the same problem. After set restore_upcoming_session = false, that error won't show up anymore. But somehow if I open a file from other project's directory (nvimtree will auto change cwd to that project root), it will close all the buffers from old project. from this commit i got error: 04ccdac802200ecc363b251cf922b2b022bb515c

    {
    "rmagatti/auto-session",
    commit = "04ccdac802200ecc363b251cf922b2b022bb515c",
    lazy = false,
    config = function()
      require("auto-session").setup({
        log_level = "debug",
        auto_session_enabled = true,
        auto_session_create_enabled = true,
        auto_save_enabled = true,
        auto_restore_enabled = true,
        auto_session_enable_last_session = true,
        auto_session_allowed_dirs = { "~/git/*" },
        cwd_change_handling = {
           restore_upcoming_session = false,
        },
      })
    end,
  },

But if I use this commit c8b2f4048f846387361bd04cc185bf1aa7d2e3d1 auto save last session and auto restore the last session working as expected.

    {
    "rmagatti/auto-session",
    commit = "c8b2f4048f846387361bd04cc185bf1aa7d2e3d1",
    lazy = false,
    config = function()
      require("auto-session").setup({
        log_level = "debug",
        auto_session_enabled = true,
        auto_session_create_enabled = true,
        auto_save_enabled = true,
        auto_restore_enabled = true,
        auto_session_enable_last_session = true,
        auto_session_allowed_dirs = { "~/git/*" },
        --cwd_change_handling = {
        --   restore_upcoming_session = false,
        --},
       pre_save_cmds = {
      function()
      -- close all floating windows, then wipeout unlisted buffers to prevent error after auto restoring session
      end
       }
      })
    end,
  }

System Information

  • Session Options : blank,buffers,curdir,folds,help,tabpages,winsize,winpos,terminal
  • OS : EndeavourOS Linux x86_64 6.1.12-arch1-1
  • LunarVim version : v0.9.0-dev-1014+g286777c33
Build type: RelWithDebInfo
LuaJIT 2.1.0-beta3
Compilation: /usr/bin/gcc-10 -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wdouble-promotion -Wmissing-noreturn -Wmissing-format-attribute -Wmissing-prototypes -Wno-unused-result -Wimplicit-fallthrough -Wvla -fno-common -fdiagnostics-color=always -fstack-protector-strong -DNVIM_MSGPACK_HAS_FLOAT32 -DNVIM_TS_HAS_SET_MATCH_LIMIT -DNVIM_TS_HAS_SET_ALLOCATOR -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -DMIN_LOG_LEVEL=3 -DNVIM_UNIBI_HAS_VAR_FROM -I/home/runner/work/neovim/neovim/.deps/usr/include/luajit-2.1 -I/usr/include -I/home/runner/work/neovim/neovim/.deps/usr/include -I/home/runner/work/neovim/neovim/.deps/usr/include -I/home/runner/work/neovim/neovim/.deps/usr/include -I/home/runner/work/neovim/neovim/.deps/usr/include -I/home/runner/work/neovim/neovim/build/src/nvim/auto -I/home/runner/work/neovim/neovim/build/include -I/home/runner/work/neovim/neovim/build/cmake.config -I/home/runner/work/neovim/neovim/src -I/home/runner/work/neovim/neovim/.deps/usr/include -I/home/runner/work/neovim/neovim/.deps/usr/include -I/home/runner/work/neovim/neovim/.deps/usr/include

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "/share/nvim"

Run :checkhealth for more info

My config

boydaihungst avatar Feb 23 '23 12:02 boydaihungst

@boydaihungst, can you paste the debug logs here?

rmagatti avatar Feb 27 '23 05:02 rmagatti

Of cause.

  1. First I removed all auto saved session file.
  2. Then open nvim with cmd: lvim
  3. Then I open nvim-tree to open random file in /home/huyhoang/git/vue-enterprise-boilerplate project root folder
  4. It won't open at first time, I have to open it again.
  5. After that, open nvim-tree again, then move back to folder /home/huyhoang/git/nuxt-app
  6. Open any file in nuxt-app , all buffer deleted, include the one from nuxt-app.
--- Autocommands ---
debug:  ==== Pager mode 
debug:  sessions_dir, session_file
debug:  ==== Using session DIR
debug:  ==== Session Name:
debug:  ==== is_readable 
debug:  ==== is_readable 
debug:  DirChangedPre
debug:    cwd: /home/huyhoang/git/vue-enterprise-boilerplate
debug:    target: /home/huyhoang
debug:    changed window: true
debug:    scope: global
continuing in nvim_exec() called at FileType Autocommands for "*":0
debug:  DirChangedPre
debug:    cwd: /home/huyhoang
debug:    target: /home/huyhoang/git/vue-enterprise-boilerplate
debug:    changed window: false
debug:    scope: global
debug:  ==== is_allowed_dirs_enabled
debug:  bypass_save_by_filetype: false
debug:  ==== SaveSession
debug:  Session saved at /home/huyhoang/.local/share/nvim/sessions/\%home\%huyhoang.vim
3 buffers deleted
debug:  DirChangedPre
debug:    cwd: /home/huyhoang/git
debug:    target: /home/huyhoang/git/vue-enterprise-boilerplate
debug:    changed window: true
debug:    scope: global
debug:  DirChangedPre
debug:    cwd: /home/huyhoang/git/vue-enterprise-boilerplate
debug:    target: /home/huyhoang/git/nuxt-app
debug:    changed window: false
debug:    scope: global
debug:  ==== is_allowed_dirs_enabled
debug:  bypass_save_by_filetype: false
debug:  ==== SaveSession
debug:  Session saved at /home/huyhoang/.local/share/nvim/sessions/\%home\%huyhoang\%git\%vue-enterprise-boilerplate.vim
4 buffers deleted

debug:  ==== SaveSession
info:  Session saved at /home/huyhoang/.local/share/nvim/sessions/\%home\%huyhoang\%git\%nuxt-app.vim
debug:  ==== is_allowed_dirs_enabled
debug:  bypass_save_by_filetype: false
debug:  ==== SaveSession
debug:  Session saved at /home/huyhoang/.local/share/nvim/sessions/\%home\%huyhoang\%git\%nuxt-app.vim

Here are nvimtree config, in case you needs:

nvimtree.setup.view.adaptive_size                  = false
nvimtree.setup.view.side                           = "left"
nvimtree.setup.renderer.icons.show.git             = false
nvimtree.setup.renderer.full_name                  = false
nvimtree.setup.view.number                         = true
nvimtree.setup.view.relativenumber                 = true
nvimtree.setup.renderer.indent_markers.enable      = true
nvimtree.setup.hijack_netrw                        = false
nvimtree.setup.disable_netrw                       = false
nvimtree.setup.filters.custom                      = {}

The rest from lunarvim nvimtree.lua

boydaihungst avatar Feb 27 '23 12:02 boydaihungst

Just met the issue. It's wired, because it occurs only in my Mac Mini, Windows and Linux with the same issue and same neovim version never meet the issue. The error is

Disabling auto save. Please check for errors in your config. Error:
vim/_editor.lua:421: nvim_exec2()../Users/winterreise/.local/share/nvim/sessions/%Users%winterreise%leetcode-rust_master.vim, line 51: Vim(unlet):E108: 无此变量:"SessionLoad"

And the session vim is:

let SessionLoad = 1
let s:so_save = &g:so | let s:siso_save = &g:siso | setg so=0 siso=0 | setl so=-1 siso=-1
let v:this_session=expand("<sfile>:p")
let GuiLoaded =  1
let NvimTreeSetup =  1
let ScopeState = "{\"last_tab\":1,\"cache\":[[\"\\/Users\\/winterreise\\/leetcode-rust\\/src\\/solution\\/s1441_build_an_array_with_stack_operations.rs\",\"\"]]}"
let GuiWindowFullScreen =  0
let GuiWindowMaximized =  0
let GuiWindowFrameless =  0
let GuiWindowId =  140523127280704
let GuiLinespace =  0
let GuiFont = "DejaVuSansM Nerd Font:h14"
let NvimTreeRequired =  1
silent only
silent tabonly
if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''
  let s:wipebuf = bufnr('%')
endif
let s:shortmess_save = &shortmess
if &shortmess =~ 'A'
  set shortmess=aoOA
else
  set shortmess=aoO
endif
badd +116 ~/leetcode-rust/src/solution/s1441_build_an_array_with_stack_operations.rs
argglobal
%argdel
edit ~/leetcode-rust/src/solution/s1441_build_an_array_with_stack_operations.rs
argglobal
let s:l = 116 - ((115 * winheight(0) + 46) / 93)
if s:l < 1 | let s:l = 1 | endif
keepjumps exe s:l
normal! zt
keepjumps 116
normal! 030|
tabnext 1
if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0 && getbufvar(s:wipebuf, '&buftype') isnot# 'terminal'
  silent exe 'bwipe ' . s:wipebuf
endif
unlet! s:wipebuf
set winheight=1 winwidth=1
let &shortmess = s:shortmess_save
let s:sx = expand("<sfile>:p:r")."x.vim"
if filereadable(s:sx)
  exe "source " . fnameescape(s:sx)
endif
let &g:so = s:so_save | let &g:siso = s:siso_save
set hlsearch
nohlsearch
doautoall SessionLoadPost
unlet SessionLoad
" vim: set ft=vim :

tan-wei avatar Dec 31 '23 05:12 tan-wei

The No such variable: "SessionLoad" error is usually caused when a session gets loaded while in the process of already loading a session. Consider a normal session file (with curdir in sessionoptions):

let SessionLoad = 1
let s:so_save = &g:so | let s:siso_save = &g:siso | setg so=0 siso=0 | setl so=-1 siso=-1
let v:this_session=expand("<sfile>:p")
silent only
silent tabonly
cd ~/tmp/a
...
let &g:so = s:so_save | let &g:siso = s:siso_save
set hlsearch
nohlsearch
doautoall SessionLoadPost
unlet SessionLoad
" vim: set ft=vim :

The cd ~/tmp/a call will change the directory, which, if cwd_change_handling = true will trigger an event that could trigger loading the session again. That will let SessionLoad = 1 again but that's a nop, then it will cd ~/tmp/a again but that's also a nop because we're already in that directory, and finally it will unlet SessionLoad which clears it. Then the original load will start processing again and when it gets to unlet SessionLoad it generates an error because SessionLoadwas already cleared by the session load triggered by the cd ~/tmp/a

I thought I'd fixed this bug in PR #315 but I now realize that my fix only covers some cases (auto restored sessions and :AutoSession search and :Telescope session-lens) but it doesn't cover :SessionRestore. The real fix is to move the restore_in_progress flags into RestoreSession. I'll put together a PR.

cameronr avatar Jul 14 '24 22:07 cameronr

@3rd what broke for you in https://github.com/rmagatti/auto-session/commit/7186e6148e8de6fb58403cf6596a92dfc1a94f09?

rmagatti avatar Jul 16 '24 07:07 rmagatti

Hey, it breaks here: https://github.com/rmagatti/auto-session/blob/08c279882d4117a3e6ade1a014f7cf4af7c34fec/lua/auto-session/init.lua#L718-L721 table.concat outputs a string, json.decode(string[1]) errors

I think I got the wrong commit / the changes trigger that path, saw that part was changed a while ago. Thanks for the plugin, it's great ^_^

3rd avatar Jul 16 '24 08:07 3rd

@3rd Can you share the specific error you're getting?

And is it possible for you to post your session_control.json file (by default in vim.fn.stdpath "data" .. "/auto_session/")?

cameronr avatar Jul 16 '24 15:07 cameronr

Hey, of course!

Error:

Error detected while processing VimEnter Autocommands for "*":
Error executing lua callback: ...l/share/nvim/lazy/auto-session/lua/auto-session/init.lua:721: Expected value but found unexpected end of string at character 2
stack traceback:
        [C]: in function 'decode'
        ...l/share/nvim/lazy/auto-session/lua/auto-session/init.lua:721: in function 'write_to_session_control_json'
        ...l/share/nvim/lazy/auto-session/lua/auto-session/init.lua:898: in function 'restore'
        ...l/share/nvim/lazy/auto-session/lua/auto-session/init.lua:940: in function 'AutoRestoreSession'
        ...l/share/nvim/lazy/auto-session/lua/auto-session/init.lua:811: in function 'auto_restore_session_at_vim_enter'
        ...l/share/nvim/lazy/auto-session/lua/auto-session/init.lua:1187: in function <...l/share/nvim/lazy/auto-session/lua/auto-session/init.lua:1161>

session_control.json

{"current":"\/home\/rabbit\/.cache\/nvim\/session\/%home%rabbit%brain%config.vim","alternate":"\/home\/rabbit\/.cache\/nvim\/session\/%home%rabbit%brain%core%ui%cli.vim"}

3rd avatar Jul 16 '24 16:07 3rd

I'll have a PR for this later today. Looks like it was introduced in #322

cameronr avatar Jul 16 '24 22:07 cameronr

@3rd If you're up for it, I'd love to have someone test out the fix for this issue. If so, change your config as follows:

-- 'rmagatti/auto-session', 'cameronr/auto-session',

restart nvim, then bring up Lazy and find auto-session and update it (it should show up at the top saying there's an origin mismatch). then restart nvim and let me know if it fixes it.

cameronr avatar Jul 17 '24 06:07 cameronr

It does work great with your changes, thank you!

3rd avatar Jul 17 '24 07:07 3rd