Skip to content

[mini.sessions] List of detected sessions should be updated after loading a new session #2260

@necronyxon

Description

@necronyxon

Contributing guidelines

Module(s)

mini.sessions

Neovim version

0.11.x

Description

When using (MiniSessions.read), the list of detected sessions is not updated after loading the new session.
This causes the list to potentially contain invalid local items from the previous path.

Reproduction

1 - create a local session file (Session.vim)
2 - create a global session in another path using MiniSessions.write
3 - run :lua=require("mini.sessions").read("Session.vim")
4 - run :lua=require("mini.sessions").select()
5 - select the global session
6 - run :lua=require("mini.sessions").detected; it contains the detected sessions from the previous path


Here's a snippet of my configuration using lazy.nvim. I am validating if there's a local Session.vim before loading it because I don't want to load the previous session if a local one wasn't detected.

For this to work, I need to run MiniSessions.read before validating the detected sessions, otherwise the list is outdated.

  { "nvim-mini/mini.sessions", event = "BufReadPre",  opts = { autowrite = false },
    keys = {
      { "<leader>qr", function()
        local sessions = require("mini.sessions")
        pcall(sessions.read) -- update detected sessions list
        local detected = sessions.detected
        if detected and type(detected) == "table" then
          local local_session = detected["Session.vim"]
          if local_session and local_session.type == "local" then
            sessions.read("Session.vim")
            return
          end
        end
        vim.notify("No local session found on current path!", vim.log.levels.ERROR)
      end, desc = "[SESSION] Load Session.vim file from current dir" },
      { "<leader>qd", function() require("mini.sessions").delete() end, "[SESSION] Delete current session" },
      { "<leader>qw", function() require("mini.sessions").write() end, "[SESSION] Save current session" },
      { "<leader>qs", function() require("mini.sessions").select() end, "[SESSION] Select session" },
      { "<leader>qn", function()
        vim.ui.input( { prompt = "Enter session name: " },
          function (session_name)
            if session_name ~= nil and session_name ~= "" then require("mini.sessions").write(session_name)
            else vim.notify("Session name must not be empty!", vim.log.levels.ERROR) end
          end)
      end, "[SESSION] Create new session" },
    }
  }

Here's what happens if I comment out the pcall(sessions.read):

E5108: Error executing lua: (mini.sessions) "Session.vim" is not a name for detected session.
stack traceback:
        [C]: in function 'error'
        ...ocal/share/nvim/lazy/mini.sessions/lua/mini/sessions.lua:552: in function 'error'
        ...ocal/share/nvim/lazy/mini.sessions/lua/mini/sessions.lua:525: in function 'validate_detected'
        ...ocal/share/nvim/lazy/mini.sessions/lua/mini/sessions.lua:172: in function 'read'
        /Users/username/.config/nvim/lua/plugins/init.lua:47: in function </Users/username/.config/nvim/lua/plugins/init.lua:40>

Here's the output of MiniSessions.detected (without the pcall as well):

{
  ["Session.vim"] = {
    modify_time = 1770587145,
    name = "Session.vim",
    path = "/Users/username/My Drive/Session.vim",
    type = "local"
  },
  dotfiles = {
    modify_time = 1770588025,
    name = "dotfiles",
    path = "/Users/username/.local/share/nvim/session/dotfiles",
    type = "global"
  }
}

The Session.vim entry should not be there, as it does not exist in the new path.
The next time it works because MiniSessions.read updates the list. Here's the updated (and expected) list:

{
  dotfiles = {
    modify_time = 1770588025,
    name = "dotfiles",
    path = "/Users/username/.local/share/nvim/session/dotfiles",
    type = "global"
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions