diff --git a/tools/cli/files.go b/tools/cli/files.go index 72e256f2b..453f400af 100644 --- a/tools/cli/files.go +++ b/tools/cli/files.go @@ -65,7 +65,7 @@ func CompleteFiles(prefix string, callback func(*FileEntry), cwd string) error { joinable_prefix = prefix } else { idx := strings.LastIndex(prefix, utils.Sep) - if idx > 0 { + if idx > -1 { joinable_prefix = prefix[:idx+1] base_dir = filepath.Dir(location) } diff --git a/tools/cli/files_test.go b/tools/cli/files_test.go index a61268f98..3b2b1de87 100644 --- a/tools/cli/files_test.go +++ b/tools/cli/files_test.go @@ -89,6 +89,18 @@ func TestCompleteFiles(t *testing.T) { test_candidates("odir/f", "odir/four.txt") test_candidates("x") + // a prefix whose only separator is the leading one, such as /us + if entries, err := os.ReadDir(utils.Sep); err == nil && len(entries) > 0 { + name := entries[0].Name() + prefix := utils.Sep + name[:1] + found := false + CompleteFiles(prefix, func(entry *FileEntry) { + found = found || strings.TrimSuffix(entry.CompletionCandidate, utils.Sep) == utils.Sep+name + }, "") + if !found { + t.Fatalf("Did not get %#v as a completion candidate for prefix: %#v", utils.Sep+name, prefix) + } + } } func TestCompleteExecutables(t *testing.T) {