From add52d9cf7345e76a1293fb6725d916affa93e29 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 14 Nov 2026 08:29:17 +0530 Subject: [PATCH] Cleanup previous PR Normalise the cwd passed to CompleteFiles once, so that a relative cwd, such as a config directory from a relative XDG_CONFIG_HOME, no longer gets joined onto itself twice. This also removes the second resolution pass against cwd and the absolutize_path helper. Check for errors in the test setup and test relative, ../ and ~/ prefixes against a custom cwd as well as a relative cwd. --- tools/cli/files.go | 24 ++++++++++-------------- tools/cli/files_test.go | 34 ++++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/tools/cli/files.go b/tools/cli/files.go index 13971ca26..0fbb8996d 100644 --- a/tools/cli/files.go +++ b/tools/cli/files.go @@ -16,14 +16,6 @@ import ( var _ = fmt.Print -func absolutize_path(path, cwd string) string { - path = utils.Expanduser(path) - if !filepath.IsAbs(path) { - path = filepath.Join(cwd, path) - } - return filepath.Clean(path) -} - type FileEntry struct { Name, CompletionCandidate, Abspath string Mode os.FileMode @@ -37,16 +29,23 @@ func CompleteFiles(prefix string, callback func(*FileEntry), cwd string) error { if err != nil { return err } + } else { + // cwd may be relative, for example, when the config directory comes from a relative XDG_CONFIG_HOME + cwd = utils.Abspath(utils.Expanduser(cwd)) + } + // relative prefixes are resolved against cwd, not the process working directory + location := utils.Expanduser(prefix) + if !filepath.IsAbs(location) { + location = filepath.Join(cwd, location) } - location := absolutize_path(prefix, cwd) base_dir := "" joinable_prefix := "" switch prefix { case ".": - base_dir = "." + base_dir = cwd joinable_prefix = "" case "./": - base_dir = "." + base_dir = cwd joinable_prefix = "./" case "/": base_dir = "/" @@ -72,9 +71,6 @@ func CompleteFiles(prefix string, callback func(*FileEntry), cwd string) error { if base_dir == "" { base_dir = cwd } - if !strings.HasPrefix(base_dir, "~") && !filepath.IsAbs(base_dir) { - base_dir = filepath.Join(cwd, base_dir) - } // fmt.Printf("prefix=%#v base_dir=%#v joinable_prefix=%#v\n", prefix, base_dir, joinable_prefix) entries, err := os.ReadDir(base_dir) if err != nil { diff --git a/tools/cli/files_test.go b/tools/cli/files_test.go index 33f7f2739..77ec1c172 100644 --- a/tools/cli/files_test.go +++ b/tools/cli/files_test.go @@ -104,14 +104,36 @@ 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"} { + if err := os.Mkdir(filepath.Join(cdir, "sessions"), 0700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(cdir, "sessions", "work.kitty-session"), nil, 0600); err != nil { + t.Fatal(err) + } + test_with_cwd := func(prefix, cwd string, expected ...string) { + t.Helper() 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) + if err := CompleteFiles(prefix, func(entry *FileEntry) { actual = append(actual, entry.CompletionCandidate) }, cwd); err != nil { + t.Fatalf("Completing %#v relative to %#v failed: %s", prefix, cwd, err) } + if !reflect.DeepEqual(actual, expected) { + t.Fatalf("Did not complete %#v relative to the cwd argument %#v\nExpected: %#v\nActual: %#v", prefix, cwd, expected, actual) + } + } + test_with_cwd("sessions/", cdir, "sessions/work.kitty-session") + test_with_cwd("sessions/w", cdir, "sessions/work.kitty-session") + test_with_cwd("./sessions/w", cdir, "./sessions/work.kitty-session") + test_with_cwd("../sessions/w", filepath.Join(cdir, "sessions"), "../sessions/work.kitty-session") + t.Setenv("HOME", cdir) + test_with_cwd("~/sessions/w", tdir, "~/sessions/work.kitty-session") + // a relative cwd, such as a config directory from a relative XDG_CONFIG_HOME, is relative to the process cwd + if err := os.Chdir(filepath.Dir(cdir)); err != nil { + t.Fatal(err) + } + test_with_cwd("sessions/", filepath.Base(cdir), "sessions/work.kitty-session") + test_with_cwd("s", filepath.Base(cdir), "sessions/") + if err := os.Chdir(tdir); err != nil { + t.Fatal(err) } // symlinks to directories are directories