Skip to content

Commit 69f798b

Browse files
authored
fix(fs_actions): traverse up on non-existent destination (#1666)
1 parent e9ffdbf commit 69f798b

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

lua/neo-tree/sources/filesystem/init.lua

+19
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ M.follow = function(callback, force_show)
107107
end, 100, utils.debounce_strategy.CALL_LAST_ONLY)
108108
end
109109

110+
local fs_stat = (vim.uv or vim.loop).fs_stat
110111
---@param path string?
111112
---@param path_to_reveal string?
112113
---@param callback function?
@@ -123,6 +124,24 @@ M._navigate_internal = function(state, path, path_to_reveal, callback, async)
123124
path = manager.get_cwd(state)
124125
end
125126
path = utils.normalize_path(path)
127+
128+
-- if path doesn't exist, navigate upwards until it does
129+
local orig_path = path
130+
local backed_out = false
131+
while not fs_stat(path) do
132+
log.debug(("navigate_internal: path %s didn't exist, going up a directory"):format(path))
133+
backed_out = true
134+
local parent, _ = utils.split_path(path)
135+
if not parent then
136+
break
137+
end
138+
path = parent
139+
end
140+
141+
if backed_out then
142+
log.warn(("Root path %s doesn't exist, backing out to %s"):format(orig_path, path))
143+
end
144+
126145
if path ~= state.path then
127146
log.debug("navigate_internal: path changed from ", state.path, " to ", path)
128147
state.path = path

lua/neo-tree/sources/manager.lua

+11-9
Original file line numberDiff line numberDiff line change
@@ -322,22 +322,24 @@ local get_params_for_cwd = function(state)
322322
end
323323
end
324324

325+
---@return string
325326
M.get_cwd = function(state)
326327
local winid, tabnr = get_params_for_cwd(state)
327-
local success, cwd = false, ""
328328
if winid or tabnr then
329-
success, cwd = pcall(vim.fn.getcwd, winid, tabnr)
330-
end
331-
if success then
332-
return cwd
333-
else
334-
success, cwd = pcall(vim.fn.getcwd)
329+
local success, cwd = pcall(vim.fn.getcwd, winid, tabnr)
335330
if success then
336331
return cwd
337-
else
338-
return state.path
339332
end
340333
end
334+
335+
local success, cwd = pcall(vim.fn.getcwd)
336+
if success then
337+
return cwd
338+
end
339+
340+
local err = cwd
341+
log.debug(err)
342+
return state.path or ""
341343
end
342344

343345
M.set_cwd = function(state)

0 commit comments

Comments
 (0)