Skip to content

Commit b75f8c6

Browse files
authored
Merge pull request #340 from cameronr/silent-source
Keep sourcing the session even if there's an error
2 parents a6e692f + cfe5279 commit b75f8c6

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ require("auto-session").setup {
112112
},
113113
args_allow_single_directory = true, -- boolean Follow normal sesion save/load logic if launched with a single directory as the only argument
114114
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
115+
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
115116
}
116117
```
117118

doc/auto-session.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ luaOnlyConf *luaOnlyConf*
2222
{cwd_change_handling?} (boolean|CwdChangeHandling)
2323
{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
2424
{close_unsupported_windows?} (boolean) Whether to close windows that aren't backed by a real file
25-
{silent_restore?} (boolean) Whether to restore sessions silently or not
25+
{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
2626
{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
2727
{args_allow_single_directory?} (boolean) Follow normal sesion save/load logic if launched with a single directory as the only argument
2828
Argv Handling

lua/auto-session/init.lua

+14-6
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ local defaultConf = {
7474
---@field cwd_change_handling? boolean|CwdChangeHandling
7575
---@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
7676
---@field close_unsupported_windows? boolean Whether to close windows that aren't backed by a real file
77-
---@field silent_restore? boolean Whether to restore sessions silently or not
77+
---@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
7878
---@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
7979
---Argv Handling
8080
---@field args_allow_single_directory? boolean Follow normal sesion save/load logic if launched with a single directory as the only argument
@@ -114,7 +114,7 @@ local luaOnlyConf = {
114114
control_filename = "session_control.json", -- File name of the session control file
115115
},
116116
},
117-
silent_restore = true,
117+
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
118118
}
119119

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

941941
if AutoSession.conf.silent_restore then
942-
cmd = "silent " .. cmd
942+
cmd = "silent! " .. cmd
943+
-- clear errors here so we can
944+
vim.v.errmsg = ""
943945
end
944946

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

964+
if AutoSession.conf.silent_restore and vim.v.errmsg and vim.v.errmsg ~= "" then
965+
-- we had an error while sourcing silently so surface it
966+
success = false
967+
result = vim.v.errmsg
968+
end
969+
962970
if not success then
963971
Lib.logger.error([[
964-
Error restoring session! The session might be corrupted.
965-
Disabling auto save. Please check for errors in your config. Error:
966-
]] .. result)
972+
Error restoring session, disabling auto save.
973+
Set silent_restore = false in the config for a more detailed error message.
974+
Error: ]] .. result)
967975
AutoSession.conf.auto_save_enabled = false
968976
return false
969977
end

0 commit comments

Comments
 (0)