Skip to content

Commit 79ef412

Browse files
authored
Merge pull request #413 from rmagatti/use-getcwd--1
fix: Fix session relying on `getcwd` without specifying global (-1)
2 parents d26acde + 4dcbb67 commit 79ef412

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

lua/auto-session/autocmds.lua

+5-5
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ local function purge_orphaned_sessions()
7979
local session_files = Lib.get_session_list(M.AutoSession.get_root_dir())
8080
for _, session in ipairs(session_files) do
8181
if
82-
not Lib.is_named_session(session.session_name)
83-
-- don't want any annotations (e.g. git branch)
84-
and vim.fn.isdirectory(session.display_name_component) == Lib._VIM_FALSE
82+
not Lib.is_named_session(session.session_name)
83+
-- don't want any annotations (e.g. git branch)
84+
and vim.fn.isdirectory(session.display_name_component) == Lib._VIM_FALSE
8585
then
8686
Lib.logger.debug("purge: " .. session.session_name)
8787
table.insert(orphaned_sessions, session.session_name)
@@ -205,7 +205,7 @@ local function setup_dirchanged_autocmds(AutoSession)
205205
callback = function()
206206
Lib.logger.debug "DirChangedPre"
207207
Lib.logger.debug {
208-
cwd = vim.fn.getcwd(),
208+
cwd = vim.fn.getcwd(-1, -1),
209209
target = vim.v.event.directory,
210210
["changed window"] = tostring(vim.v.event.changed_window),
211211
scope = vim.v.event.scope,
@@ -239,7 +239,7 @@ local function setup_dirchanged_autocmds(AutoSession)
239239
vim.api.nvim_create_autocmd("DirChanged", {
240240
callback = function()
241241
Lib.logger.debug "DirChanged"
242-
Lib.logger.debug(" cwd: " .. vim.fn.getcwd())
242+
Lib.logger.debug(" cwd: " .. vim.fn.getcwd(-1, -1))
243243
Lib.logger.debug(" changed window: " .. tostring(vim.v.event.changed_window))
244244
Lib.logger.debug(" scope: " .. vim.v.event.scope)
245245

lua/auto-session/init.lua

+6-6
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ local function suppress_session(session_dir)
173173

174174
-- If session_dir is set, use that otherwise use cwd
175175
-- session_dir will be set when loading a session from a directory at lauch (i.e. from argv)
176-
local cwd = session_dir or vim.fn.getcwd()
176+
local cwd = session_dir or vim.fn.getcwd(-1, -1)
177177

178178
if Lib.find_matching_directory(cwd, dirs) then
179179
Lib.logger.debug "suppress_session found a match, suppressing"
@@ -190,7 +190,7 @@ local function is_allowed_dir()
190190
end
191191

192192
local dirs = Config.allowed_dirs or {}
193-
local cwd = vim.fn.getcwd()
193+
local cwd = vim.fn.getcwd(-1, -1)
194194

195195
if Lib.find_matching_directory(cwd, dirs) then
196196
Lib.logger.debug "is_allowed_dir found a match, allowing"
@@ -209,7 +209,7 @@ end
209209
---@return string Returns the escaped version of the name with .vim appended.
210210
local function get_session_file_name(session_name, legacy)
211211
if not session_name or session_name == "" then
212-
session_name = vim.fn.getcwd()
212+
session_name = vim.fn.getcwd(-1, -1)
213213
Lib.logger.debug("get_session_file_name no session_name, using cwd: " .. session_name)
214214

215215
local git_branch_name = get_git_branch_name()
@@ -279,13 +279,13 @@ end
279279
---unless a session for the current working directory exists.
280280
---@return boolean True if a session exists for the cwd
281281
function AutoSession.session_exists_for_cwd()
282-
local session_file = get_session_file_name(vim.fn.getcwd())
282+
local session_file = get_session_file_name(vim.fn.getcwd(-1, -1))
283283
if vim.fn.filereadable(AutoSession.get_root_dir() .. session_file) ~= 0 then
284284
return true
285285
end
286286

287287
-- Check legacy sessions
288-
session_file = get_session_file_name(vim.fn.getcwd(), true)
288+
session_file = get_session_file_name(vim.fn.getcwd(-1, -1), true)
289289
return vim.fn.filereadable(AutoSession.get_root_dir() .. session_file) ~= 0
290290
end
291291

@@ -482,7 +482,7 @@ function AutoSession.auto_restore_session_at_vim_enter()
482482

483483
-- We failed to load a session for the other directory. Unless session name matches cwd, we don't
484484
-- want to enable autosaving since it might replace the session for the cwd
485-
if vim.fn.getcwd() ~= session_name then
485+
if vim.fn.getcwd(-1, -1) ~= session_name then
486486
Lib.logger.debug "Not enabling autosave because launch argument didn't load session and doesn't match cwd"
487487
Config.auto_save = false
488488
end

tests/get_cwd_spec.lua

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---@diagnostic disable: undefined-field
2+
local TL = require "tests/test_lib"
3+
4+
describe("cwd lookup", function()
5+
local as = require "auto-session"
6+
7+
require("auto-session").setup {}
8+
9+
TL.clearSessionFilesAndBuffers()
10+
11+
it("works when tcd is used", function()
12+
assert.equals(0, vim.fn.filereadable(TL.default_session_path))
13+
vim.cmd("e " .. TL.test_file)
14+
vim.cmd "tabnew"
15+
vim.cmd "tcd tests"
16+
17+
as.SaveSession()
18+
19+
vim.cmd "tabclose"
20+
21+
-- Make sure the session was still created for the global directory
22+
assert.equals(1, vim.fn.filereadable(TL.default_session_path))
23+
end)
24+
25+
TL.clearSessionFilesAndBuffers()
26+
27+
it("works when lcd is used", function()
28+
assert.equals(0, vim.fn.filereadable(TL.default_session_path))
29+
vim.cmd("e " .. TL.test_file)
30+
vim.cmd "tabnew"
31+
vim.cmd "lcd tests"
32+
33+
as.SaveSession()
34+
35+
vim.cmd "tabclose"
36+
37+
-- Make sure the session was still created for the global directory
38+
assert.equals(1, vim.fn.filereadable(TL.default_session_path))
39+
end)
40+
end)

0 commit comments

Comments
 (0)