olimorris / persisted.nvim

💾 Simple session management for Neovim with git branching, autoloading and Telescope support
MIT License
418 stars 24 forks source link

[Bug]: SessionLoad crashes #120

Closed Musinux closed 2 months ago

Musinux commented 2 months ago

Your minimal.lua config

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 = {
      silent = true, -- silent nvim message when sourcing session file
      use_git_branch = true, -- create session files based on the branch of the git enabled repository
      autoload = true, -- automatically load the session for the cwd on Neovim startup
      follow_cwd = true, -- change session file name to match current working directory if it changes
      allowed_dirs = { '/home/musinux/projects/front', '/home/musinux/projects/front-client' }, -- table of dirs that the plugin will auto-save and auto-load from
      ignored_dirs = { '/home/musinux/projects/front/node_modules', '/home/musinux/projects/front-client/node_modules' }, -- table of dirs that are ignored when auto-saving and auto-loading
      telescope = { -- options for the telescope extension
        reset_prompt = true, -- whether to reset prompt after session deleted
      },
    }
  },
  -- Put any other plugins here
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

Error messages

E5108: Error executing lua ...persisted/plugins/persisted.nvim/lua/persisted/utils.lua:58: missing '[' after '%f' in pattern                                 
stack traceback:                                                                                                                                              
        [C]: in function 'gsub'                                                                                                                               
        ...persisted/plugins/persisted.nvim/lua/persisted/utils.lua:58: in function 'make_session_data'                                                       
        ...persisted/plugins/persisted.nvim/lua/persisted/utils.lua:133: in function 'load_session'                                                           
        .../persisted/plugins/persisted.nvim/lua/persisted/init.lua:215: in function 'load'                                                                   
        [string ":lua"]:1: in main chunk   

Describe the bug

I expect :SessionLoad to function properly and not fail

However the error message doesn't seem to prevent sessions to actually load

image

Reproduce the bug

  1. nvim --clean -u ./minimal.lua
  2. :SessionSave
  3. :SessionLoad -> this should trigger the error

Final notes: This bug has been there for multiple months for me, and I'm always following the latest dev release of nvim. I'm currently on NVIM v0.10.0-dev-2976+g208852126

Final checks

olimorris commented 2 months ago

I've tried this with your minimal.lua file and can't recreate it. Is there something missing from your config?

Musinux commented 2 months ago

Thanks for looking into it. I honestly don't know. Here's a session file that triggers the error consistenly. Maybe there's something in there that can help you?

The file is named: %home%musinux%projects%front-client@@branchprefix%the-branch-name.vim

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 ~/projects/front-client
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
argglobal
%argdel
argglobal
enew
setlocal fdm=syntax
setlocal fde=0
setlocal fmr={{{,}}}
setlocal fdi=#
setlocal fdl=20
setlocal fml=1
setlocal fdn=20
setlocal fen
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=20
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 :
olimorris commented 2 months ago

I have minimal knowledge of what a session file should look like unfortunately.

I wonder if the name is the problem: %home%musinux%projects%front-client@@branchprefix%the-branch-name.vim

Most of mine are named %Users%Oli%.dotfiles@@main.vim.

Musinux commented 2 months ago

The error comes from https://github.com/olimorris/persisted.nvim/blob/ca9900c31ee6e254a0ba7011ba49f48ebf4c8db2/lua/persisted/utils.lua#L57-L58

And indeed, my folder is [...]%front-client[...] so and I see this stackoverflow post so maybe there's a pattern recognition thing going on with it ?

olimorris commented 2 months ago

Could you share the full path to the session? Of course change any words for confidentiality reasons.

Musinux commented 2 months ago

Yes, here's one:

/home/louis-cherel/.local/share/nvim/sessions/%home%louis-cherel%projects%front@@louis%fix-analytics-export-null-values.vim

GabeDuarteM commented 2 months ago

I'm also having this same problem:

E5108: Error executing lua: vim/_editor.lua:0: nvim_exec2(): Vim(lua):E5108: Error executing lua ...l/share/nvim/lazy/persisted.nvim/lua/persisted/utils.lua:58: missing '[' after '%f' in pattern
stack traceback:
    [C]: in function 'gsub'
    ...l/share/nvim/lazy/persisted.nvim/lua/persisted/utils.lua:58: in function 'make_session_data'
    ...l/share/nvim/lazy/persisted.nvim/lua/persisted/utils.lua:133: in function 'load_session'
    ...al/share/nvim/lazy/persisted.nvim/lua/persisted/init.lua:215: in function 'load'
    [string ":lua"]:1: in main chunk
    [C]: in function 'nvim_exec2'
    vim/_editor.lua: in function 'cmd'
    ...re/nvim/lazy/dashboard-nvim/lua/dashboard/theme/doom.lua:24: in function <...re/nvim/lazy/dashboard-nvim/lua/dashboard/theme/doom.lua:20>
stack traceback:
    [C]: in function 'nvim_exec2'
    vim/_editor.lua: in function 'cmd'
    ...re/nvim/lazy/dashboard-nvim/lua/dashboard/theme/doom.lua:24: in function <...re/nvim/lazy/dashboard-nvim/lua/dashboard/theme/doom.lua:20>

This only happen on this specific folder... The session file is /home/gabe/.local/share/nvim/sessions/%home%gabe%[REDACTED]%[REDACTED]%[REDACTED]@@[REDACTED]%fix-[REDACTED].vim, and the file contents are:

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 [REDACTED]
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
argglobal
%argdel
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=20
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 :

On my session filename, I also have a %f on %fix (branch name is like something/fix-something), so I assume this is also the cause...

olimorris commented 2 months ago

Thanks for flagging.

Any support you both could offer for this would be great. I don't have capacity right now.

Musinux commented 2 months ago

Now that we have a good lead, I'll try to dig in the following weeks

olimorris commented 2 months ago

I think I've solved it...can you guys try this PR: https://github.com/olimorris/persisted.nvim/pull/123

Musinux commented 2 months ago

Thank you! That fixed it for me :)

Small note: I'd prefer not having my name in your test files, so if you can just twist your test string that would mean a lot to me

olimorris commented 2 months ago

Of course. Apologies I hadn't even spotted that.