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()