Skip to content

Commit

Permalink
Merge pull request #340 from cameronr/silent-source
Browse files Browse the repository at this point in the history
Keep sourcing the session even if there's an error
  • Loading branch information
cameronr authored Aug 5, 2024
2 parents a6e692f + cfe5279 commit b75f8c6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ require("auto-session").setup {
},
args_allow_single_directory = true, -- boolean Follow normal sesion save/load logic if launched with a single directory as the only argument
args_allow_files_auto_save = false, -- boolean|function Allow saving a session even when launched with a file argument (or multiple files/dirs). It does not load any existing session first. While you can just set this to true, you probably want to set it to a function that decides when to save a session when launched with file args. See documentation for more detail
silent_restore = true, -- Suppress extraneous messages and source the whole session, even if there's an error. Set to false to get the line number a restore error
}
```

Expand Down
2 changes: 1 addition & 1 deletion doc/auto-session.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ luaOnlyConf *luaOnlyConf*
{cwd_change_handling?} (boolean|CwdChangeHandling)
{bypass_session_save_file_types?} (table) List of file types to bypass auto save when the only buffer open is one of the file types listed
{close_unsupported_windows?} (boolean) Whether to close windows that aren't backed by a real file
{silent_restore?} (boolean) Whether to restore sessions silently or not
{silent_restore?} (boolean) Suppress extraneous messages and source the whole session, even if there's an error. Set to false to get the line number of a restore error
{log_level?} (string|integer) "debug", "info", "warn", "error" or vim.log.levels.DEBUG, vim.log.levels.INFO, vim.log.levels.WARN, vim.log.levels.ERROR
{args_allow_single_directory?} (boolean) Follow normal sesion save/load logic if launched with a single directory as the only argument
Argv Handling
Expand Down
20 changes: 14 additions & 6 deletions lua/auto-session/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ local defaultConf = {
---@field cwd_change_handling? boolean|CwdChangeHandling
---@field bypass_session_save_file_types? table List of file types to bypass auto save when the only buffer open is one of the file types listed
---@field close_unsupported_windows? boolean Whether to close windows that aren't backed by a real file
---@field silent_restore? boolean Whether to restore sessions silently or not
---@field silent_restore? boolean Suppress extraneous messages and source the whole session, even if there's an error. Set to false to get the line number of a restore error
---@field log_level? string|integer "debug", "info", "warn", "error" or vim.log.levels.DEBUG, vim.log.levels.INFO, vim.log.levels.WARN, vim.log.levels.ERROR
---Argv Handling
---@field args_allow_single_directory? boolean Follow normal sesion save/load logic if launched with a single directory as the only argument
Expand Down Expand Up @@ -114,7 +114,7 @@ local luaOnlyConf = {
control_filename = "session_control.json", -- File name of the session control file
},
},
silent_restore = true,
silent_restore = true, -- Suppress extraneous messages and source the whole session, even if there's an error. Set to false to get the line number of a restore error
}

-- Set default config on plugin load
Expand Down Expand Up @@ -939,7 +939,9 @@ function AutoSession.RestoreSessionFile(session_path, show_message)
local cmd = "source " .. vim_session_path

if AutoSession.conf.silent_restore then
cmd = "silent " .. cmd
cmd = "silent! " .. cmd
-- clear errors here so we can
vim.v.errmsg = ""
end

-- Set restore_in_progress here so we won't also try to save/load the session if
Expand All @@ -959,11 +961,17 @@ function AutoSession.RestoreSessionFile(session_path, show_message)
-- Clear any saved command line args since we don't need them anymore
launch_argv = nil

if AutoSession.conf.silent_restore and vim.v.errmsg and vim.v.errmsg ~= "" then
-- we had an error while sourcing silently so surface it
success = false
result = vim.v.errmsg
end

if not success then
Lib.logger.error([[
Error restoring session! The session might be corrupted.
Disabling auto save. Please check for errors in your config. Error:
]] .. result)
Error restoring session, disabling auto save.
Set silent_restore = false in the config for a more detailed error message.
Error: ]] .. result)
AutoSession.conf.auto_save_enabled = false
return false
end
Expand Down

0 comments on commit b75f8c6

Please sign in to comment.