From d845369295b3be51c1fa4679ac9a66b9d514c6aa Mon Sep 17 00:00:00 2001 From: Cameron Ring Date: Thu, 22 Aug 2024 14:58:16 -0700 Subject: [PATCH] fix: handle symlinks in allowed/suppressed dirs --- .gitignore | 1 + lua/auto-session/lib.lua | 32 +++++++++++++++++++++++++++----- tests/allowed_dirs_spec.lua | 23 +++++++++++++++++++++++ 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 77ddff8..50e4407 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ doc/tags tests/test_sessions tests/custom_sessions tests/test_git +tests/symlink-test diff --git a/lua/auto-session/lib.lua b/lua/auto-session/lib.lua index 732bf9c..643b2d4 100644 --- a/lua/auto-session/lib.lua +++ b/lua/auto-session/lib.lua @@ -491,12 +491,34 @@ end ---@param dirs table ---@param dirToFind string function Lib.find_matching_directory(dirToFind, dirs) - Lib.logger.debug("find_matching_directory", { dirToFind = dirToFind, dirs = dirs }) - for _, s in pairs(dirs) do - local expanded = Lib.expand(s) - -- Lib.logger.debug("find_matching_directory expanded: " .. s) + local dirsToCheck = {} + + -- resolve any symlinks and also check those + for _, dir in pairs(dirs) do + -- first expand it + local expanded_dir = Lib.expand(dir) + + -- resolve symlinks + local resolved_dir = vim.fn.resolve(expanded_dir) + + -- Lib.logger.debug("dir: " .. dir .. " expanded_dir: " .. expanded_dir .. " resolved_dir: " .. resolved_dir) + + -- add the base expanded dir first. in theory, we should only need + -- the resolved directory but other systems might behave differently so + -- safer to check both + table.insert(dirsToCheck, expanded_dir) + + -- add the resolved dir if it's different (e.g. a symlink) + if resolved_dir ~= expanded_dir then + table.insert(dirsToCheck, resolved_dir) + end + end + + Lib.logger.debug("find_matching_directory", { dirToFind = dirToFind, dirsToCheck = dirsToCheck }) + + for _, dir in pairs(dirsToCheck) do ---@diagnostic disable-next-line: param-type-mismatch - for path in string.gmatch(expanded, "[^\r\n]+") do + for path in string.gmatch(dir, "[^\r\n]+") do local simplified_path = vim.fn.simplify(path) local path_without_trailing_slashes = string.gsub(simplified_path, "/+$", "") diff --git a/tests/allowed_dirs_spec.lua b/tests/allowed_dirs_spec.lua index 4389801..293239e 100644 --- a/tests/allowed_dirs_spec.lua +++ b/tests/allowed_dirs_spec.lua @@ -7,10 +7,12 @@ describe("The allowed dirs config", function() local c = require "auto-session.config" as.setup { auto_session_allowed_dirs = { "/dummy" }, + -- log_level = "debug", } TL.clearSessionFilesAndBuffers() vim.cmd("e " .. TL.test_file) + local cwd = vim.fn.getcwd() it("doesn't save a session for a non-allowed dir", function() as.AutoSaveSession() @@ -46,4 +48,25 @@ describe("The allowed dirs config", function() -- Make sure the session was created assert.equals(1, vim.fn.filereadable(session_path)) end) + + if vim.fn.has "win32" == 0 then + it("saves a session for an allowed dir with a symlink", function() + TL.clearSessionFilesAndBuffers() + vim.cmd("cd " .. cwd) + + vim.cmd("e " .. TL.test_file) + c.allowed_dirs = { vim.fn.getcwd() .. "/tests/symlink-test" } + + vim.fn.system "ln -snf test_files tests/symlink-test" + vim.cmd "cd tests/symlink-test" + + local session_path = TL.makeSessionPath(vim.fn.getcwd()) + assert.equals(0, vim.fn.filereadable(session_path)) + + assert.True(as.AutoSaveSession()) + + -- Make sure the session was created + assert.equals(1, vim.fn.filereadable(session_path)) + end) + end end)