From 30e7f3512eeeed4492826449d4afad98e0029927 Mon Sep 17 00:00:00 2001 From: devangpratap <115096812+devangpratap@users.noreply.github.com> Date: Thu, 12 Nov 2026 16:40:55 -0500 Subject: [PATCH] Complete paths relative to the config directory correctly CompleteFiles resolved the prefix with filepath.Abs, which uses the process cwd, instead of the cwd it was given. For the completers that are relative to the config directory, like kitty --session and launch --watcher, a prefix such as sessions/ was looked up under the shell's cwd, so completing inside a sub-directory of the config directory gave nothing, or the contents of a same named directory in the shell's cwd. Resolve relative prefixes against the given cwd instead. --- tools/cli/files.go | 11 +++++------ tools/cli/files_test.go | 12 ++++++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/tools/cli/files.go b/tools/cli/files.go index 1b4b45409..13971ca26 100644 --- a/tools/cli/files.go +++ b/tools/cli/files.go @@ -16,13 +16,12 @@ import ( var _ = fmt.Print -func absolutize_path(path string) string { +func absolutize_path(path, cwd string) string { path = utils.Expanduser(path) - q, err := filepath.Abs(path) - if err == nil { - path = q + if !filepath.IsAbs(path) { + path = filepath.Join(cwd, path) } - return path + return filepath.Clean(path) } type FileEntry struct { @@ -39,7 +38,7 @@ func CompleteFiles(prefix string, callback func(*FileEntry), cwd string) error { return err } } - location := absolutize_path(prefix) + location := absolutize_path(prefix, cwd) base_dir := "" joinable_prefix := "" switch prefix { diff --git a/tools/cli/files_test.go b/tools/cli/files_test.go index 28601c841..33f7f2739 100644 --- a/tools/cli/files_test.go +++ b/tools/cli/files_test.go @@ -102,6 +102,18 @@ func TestCompleteFiles(t *testing.T) { } } + // paths are resolved relative to the cwd passed in, such as the config directory, not the process cwd + cdir := t.TempDir() + os.Mkdir(filepath.Join(cdir, "sessions"), 0700) + os.WriteFile(filepath.Join(cdir, "sessions", "work.kitty-session"), nil, 0600) + for _, prefix := range []string{"sessions/", "sessions/w"} { + actual := []string{} + CompleteFiles(prefix, func(entry *FileEntry) { actual = append(actual, entry.CompletionCandidate) }, cdir) + if !reflect.DeepEqual(actual, []string{"sessions/work.kitty-session"}) { + t.Fatalf("Did not complete %#v relative to the cwd argument: %#v", prefix, actual) + } + } + // symlinks to directories are directories os.Symlink(filepath.Join(tdir, "odir"), filepath.Join(tdir, "slink")) c := NewCompletions()