From 67575e1736cbbb67f68493894f88b148b1ffdd8c Mon Sep 17 00:00:00 2001 From: mathias4833 <67101597+mathias4833@users.noreply.github.com> Date: Sat, 15 Mar 2025 21:06:38 +0100 Subject: [PATCH] fix(api): prevent tilde expansion in path suggestions (fixes #9990) (#9992) ### Purpose Path autocompletion wasn't working when using `~` as a shortcut for the home directory. The issue occurred because the tilde was expanded to /home/user, which caused the suggestion to no longer match the input (thus preventing the autocompletion from appearing in the suggestion list). To fix this, I replaced the custom `parentAndBase` function, which handled path splitting in a more complex way, with `filepath.Split` from the standard `path/filepath` package. This prevents tilde expansion while keeping the expected behavior for path splitting. ### Testing The issue has been tested manually on Linux. ### Screenshots ![screenshot](https://github.com/user-attachments/assets/49dd96e2-6d75-4476-946d-0dfb2ac474ef) --- lib/api/api.go | 23 +---------------------- 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/lib/api/api.go b/lib/api/api.go index b5be612e1..8d07546b9 100644 --- a/lib/api/api.go +++ b/lib/api/api.go @@ -1764,7 +1764,7 @@ func browse(fsType fs.FilesystemType, current string) []string { return browseRoots(fsType) } - parent, base := parentAndBase(current) + parent, base := filepath.Split(current) ffs := fs.NewFilesystem(fsType, parent) files := browseFiles(ffs, base) for i := range files { @@ -1800,27 +1800,6 @@ func browseRoots(fsType fs.FilesystemType) []string { return nil } -// parentAndBase returns the parent directory and the remaining base of the -// path. The base may be empty if the path ends with a path separator. -func parentAndBase(current string) (string, string) { - search, _ := fs.ExpandTilde(current) - pathSeparator := string(fs.PathSeparator) - - if strings.HasSuffix(current, pathSeparator) && !strings.HasSuffix(search, pathSeparator) { - search = search + pathSeparator - } - searchDir := filepath.Dir(search) - - // The searchFile should be the last component of search, or empty if it - // ends with a path separator - var searchFile string - if !strings.HasSuffix(search, pathSeparator) { - searchFile = filepath.Base(search) - } - - return searchDir, searchFile -} - func browseFiles(ffs fs.Filesystem, search string) []string { subdirectories, _ := ffs.DirNames(".") pathSeparator := string(fs.PathSeparator)