From b8ef32fac5e962a8c05e2a7a770f35a22007d502 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Sun, 15 Nov 2026 09:22:57 +0000 Subject: [PATCH 1/6] Add acceptance test for fs completion of local paths Records current behavior: completions for './sr', './' and 'src//s' do not start with the typed word, so bash offers nothing. Co-authored-by: Isaac --- .../cmd/fs/complete-local-path/out.test.toml | 2 ++ .../cmd/fs/complete-local-path/output.txt | 35 +++++++++++++++++++ acceptance/cmd/fs/complete-local-path/script | 11 ++++++ .../cmd/fs/complete-local-path/test.toml | 2 ++ 4 files changed, 50 insertions(+) create mode 100644 acceptance/cmd/fs/complete-local-path/out.test.toml create mode 100644 acceptance/cmd/fs/complete-local-path/output.txt create mode 100644 acceptance/cmd/fs/complete-local-path/script create mode 100644 acceptance/cmd/fs/complete-local-path/test.toml diff --git a/acceptance/cmd/fs/complete-local-path/out.test.toml b/acceptance/cmd/fs/complete-local-path/out.test.toml new file mode 100644 index 00000000000..2e1543aa802 --- /dev/null +++ b/acceptance/cmd/fs/complete-local-path/out.test.toml @@ -0,0 +1,2 @@ +Cloud = false +GOOS.windows = false diff --git a/acceptance/cmd/fs/complete-local-path/output.txt b/acceptance/cmd/fs/complete-local-path/output.txt new file mode 100644 index 00000000000..545fdc3f08d --- /dev/null +++ b/acceptance/cmd/fs/complete-local-path/output.txt @@ -0,0 +1,35 @@ + +>>> [CLI] __complete fs ls sr +src/ +dbfs:/ +:2 +Completion ended with directive: ShellCompDirectiveNoSpace +bash offers: src/ + +>>> [CLI] __complete fs ls ./sr +src/ +dbfs:/ +:2 +Completion ended with directive: ShellCompDirectiveNoSpace +bash offers: + +>>> [CLI] __complete fs ls ./ +src/ +dbfs:/ +:2 +Completion ended with directive: ShellCompDirectiveNoSpace +bash offers: + +>>> [CLI] __complete fs ls ./src/ +src/sub/ +dbfs:/ +:2 +Completion ended with directive: ShellCompDirectiveNoSpace +bash offers: + +>>> [CLI] __complete fs ls src//s +src/sub/ +dbfs:/ +:2 +Completion ended with directive: ShellCompDirectiveNoSpace +bash offers: diff --git a/acceptance/cmd/fs/complete-local-path/script b/acceptance/cmd/fs/complete-local-path/script new file mode 100644 index 00000000000..efc8f651c06 --- /dev/null +++ b/acceptance/cmd/fs/complete-local-path/script @@ -0,0 +1,11 @@ +mkdir -p src/sub +touch src/file.txt + +# Shells only offer completions that start with the word being completed +# (bash filters them with compgen), so each completion must extend the input. +for word in sr ./sr ./ ./src/ src//s; do + trace $CLI __complete fs ls "$word" + echo "bash offers:" $($CLI __complete fs ls "$word" 2>/dev/null | grep -v '^:' | bash -c 'compgen -W "$(cat)" -- "$1"' _ "$word") +done + +rm -rf src diff --git a/acceptance/cmd/fs/complete-local-path/test.toml b/acceptance/cmd/fs/complete-local-path/test.toml new file mode 100644 index 00000000000..b89cebe3b91 --- /dev/null +++ b/acceptance/cmd/fs/complete-local-path/test.toml @@ -0,0 +1,2 @@ +# Local paths use the OS separator, so completions differ on Windows. +GOOS.windows = false From 30e999363a27f2bd40f396f5a8957e170d3b2fa8 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Sun, 15 Nov 2026 09:26:21 +0000 Subject: [PATCH 2/6] Keep the typed directory in fs path completions Completions joined the directory and entry name with filepath.Join, which cleaned the path: './sr' completed to 'src/'. Shells only offer completions that start with the word being completed, so bash offered nothing for local paths starting with './' or containing repeated separators. Keep the directory part of the input as typed. Co-authored-by: Isaac --- .nextchanges/cli/fs-complete-local-path.md | 1 + acceptance/cmd/fs/complete-local-path/output.txt | 16 ++++++++-------- libs/filer/completer/completer.go | 10 ++++++++-- libs/filer/completer/completer_test.go | 3 ++- 4 files changed, 19 insertions(+), 11 deletions(-) create mode 100644 .nextchanges/cli/fs-complete-local-path.md diff --git a/.nextchanges/cli/fs-complete-local-path.md b/.nextchanges/cli/fs-complete-local-path.md new file mode 100644 index 00000000000..e417afa52a5 --- /dev/null +++ b/.nextchanges/cli/fs-complete-local-path.md @@ -0,0 +1 @@ +* Fix shell completion in `databricks fs` commands for local paths typed with a leading `./` or repeated separators, which previously offered no suggestions. diff --git a/acceptance/cmd/fs/complete-local-path/output.txt b/acceptance/cmd/fs/complete-local-path/output.txt index 545fdc3f08d..9e971dd0c79 100644 --- a/acceptance/cmd/fs/complete-local-path/output.txt +++ b/acceptance/cmd/fs/complete-local-path/output.txt @@ -7,29 +7,29 @@ Completion ended with directive: ShellCompDirectiveNoSpace bash offers: src/ >>> [CLI] __complete fs ls ./sr -src/ +./src/ dbfs:/ :2 Completion ended with directive: ShellCompDirectiveNoSpace -bash offers: +bash offers: ./src/ >>> [CLI] __complete fs ls ./ -src/ +./src/ dbfs:/ :2 Completion ended with directive: ShellCompDirectiveNoSpace -bash offers: +bash offers: ./src/ >>> [CLI] __complete fs ls ./src/ -src/sub/ +./src/sub/ dbfs:/ :2 Completion ended with directive: ShellCompDirectiveNoSpace -bash offers: +bash offers: ./src/sub/ >>> [CLI] __complete fs ls src//s -src/sub/ +src//sub/ dbfs:/ :2 Completion ended with directive: ShellCompDirectiveNoSpace -bash offers: +bash offers: src//sub/ diff --git a/libs/filer/completer/completer.go b/libs/filer/completer/completer.go index 0f7f8b71122..df7bccd2c7f 100644 --- a/libs/filer/completer/completer.go +++ b/libs/filer/completer/completer.go @@ -42,14 +42,21 @@ func (c *completer) SetIsLocalPath(i bool) { func (c *completer) CompletePath(p string) ([]string, cobra.ShellCompDirective, error) { trailingSeparator := "/" + separators := "/" joinFunc := path.Join // Use filepath functions if we are in a local path. if c.isLocalPath { joinFunc = filepath.Join trailingSeparator = string(filepath.Separator) + separators += trailingSeparator } + // Shells only offer completions that start with the word being completed, + // so keep the directory part of p as typed. Joining it with the entry name + // would clean it and turn "./sr" into "src", which the shell then discards. + typedDir := p[:strings.LastIndexAny(p, separators)+1] + // If the user is TAB-ing their way through a path and the // path ends in a trailing slash, we should list nested directories. // If the path is incomplete, however, then we should list adjacent @@ -70,8 +77,7 @@ func (c *completer) CompletePath(p string) ([]string, cobra.ShellCompDirective, continue } - // Join directory path and entry name - completion := joinFunc(dirPath, entry.Name()) + completion := typedDir + entry.Name() // Prepend prefix if it has been set if c.prefix != "" { diff --git a/libs/filer/completer/completer_test.go b/libs/filer/completer/completer_test.go index 7ee3ae0b64c..622a2dee82a 100644 --- a/libs/filer/completer/completer_test.go +++ b/libs/filer/completer/completer_test.go @@ -89,7 +89,8 @@ func TestFilerCompleterWindowsSeparator(t *testing.T) { completer.SetIsLocalPath(true) completions, directive, err := completer.CompletePath("dir/") - assert.Equal(t, []string{"dir\\dirA\\", "dir\\dirB\\", "dbfs:/"}, completions) + // The typed "dir/" is kept so the completions still extend it. + assert.Equal(t, []string{"dir/dirA\\", "dir/dirB\\", "dbfs:/"}, completions) assert.Equal(t, cobra.ShellCompDirectiveNoSpace, directive) assert.NoError(t, err) } From 00550188547367926b3a9852fbccf99f3dcf7c32 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Mon, 16 Nov 2026 10:30:12 +0000 Subject: [PATCH 3/6] Add PR link to changelog fragment Co-authored-by: Isaac --- .nextchanges/cli/fs-complete-local-path.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.nextchanges/cli/fs-complete-local-path.md b/.nextchanges/cli/fs-complete-local-path.md index e417afa52a5..d0fab2ace59 100644 --- a/.nextchanges/cli/fs-complete-local-path.md +++ b/.nextchanges/cli/fs-complete-local-path.md @@ -1 +1 @@ -* Fix shell completion in `databricks fs` commands for local paths typed with a leading `./` or repeated separators, which previously offered no suggestions. +* Fix shell completion in `databricks fs` commands for local paths typed with a leading `./` or repeated separators, which previously offered no suggestions. ([#7679](https://github.com/databricks/cli/pull/7679)) From b05d0f6deede46d654b0d0648ac8091fa70f7ae2 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Mon, 16 Nov 2026 13:24:07 +0000 Subject: [PATCH 4/6] Update the Windows fs completion test for the kept typed separator Co-authored-by: Isaac --- cmd/fs/helpers_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/fs/helpers_test.go b/cmd/fs/helpers_test.go index dc54914ee0c..6cd812dd229 100644 --- a/cmd/fs/helpers_test.go +++ b/cmd/fs/helpers_test.go @@ -154,7 +154,8 @@ func TestGetValidArgsFunctionLocalCompletionWindows(t *testing.T) { v, cmd, _ := setupTest(t) completions, directive := v.Validate(cmd, []string{}, "dir/") - assert.Equal(t, []string{"dir\\dirA\\", "dir\\dirB\\", "dir\\fileA", "dbfs:/"}, completions) + // The typed "dir/" is kept so the shell's prefix match still accepts the candidates. + assert.Equal(t, []string{"dir/dirA\\", "dir/dirB\\", "dir/fileA", "dbfs:/"}, completions) assert.Equal(t, cobra.ShellCompDirectiveNoSpace, directive) } From 264e7080e09422fd154ab65950698df188b7139d Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Mon, 16 Nov 2026 13:52:08 +0000 Subject: [PATCH 5/6] List the directory the typed prefix names in fs path completion path.Dir only splits on '/', so on Windows ".\src\s" listed the current directory and paired its entries with the typed ".\src\", offering paths that don't exist and suppressing PowerShell's own file completion. Derive the listed directory from the same split as the typed prefix. Co-authored-by: Isaac --- libs/filer/completer/completer.go | 14 +++++++------- libs/filer/completer/completer_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/libs/filer/completer/completer.go b/libs/filer/completer/completer.go index df7bccd2c7f..a42fa590f5b 100644 --- a/libs/filer/completer/completer.go +++ b/libs/filer/completer/completer.go @@ -57,13 +57,13 @@ func (c *completer) CompletePath(p string) ([]string, cobra.ShellCompDirective, // would clean it and turn "./sr" into "src", which the shell then discards. typedDir := p[:strings.LastIndexAny(p, separators)+1] - // If the user is TAB-ing their way through a path and the - // path ends in a trailing slash, we should list nested directories. - // If the path is incomplete, however, then we should list adjacent - // directories. - dirPath := p - if !strings.HasSuffix(p, trailingSeparator) { - dirPath = path.Dir(p) + // List the directory part of p: nested entries after a trailing separator, + // adjacent ones for a partial name. Split it the same way as typedDir; + // path.Dir only splits on '/', so on Windows it would list the current + // directory for ".\src\s" and pair its entries with the typed ".\src\". + dirPath := typedDir + if dirPath == "" { + dirPath = "." } entries, err := c.filer.ReadDir(c.ctx, dirPath) diff --git a/libs/filer/completer/completer_test.go b/libs/filer/completer/completer_test.go index 622a2dee82a..11a36f27a43 100644 --- a/libs/filer/completer/completer_test.go +++ b/libs/filer/completer/completer_test.go @@ -1,6 +1,8 @@ package completer import ( + "os" + "path/filepath" "runtime" "testing" @@ -10,6 +12,7 @@ import ( "github.com/databricks/databricks-sdk-go/experimental/mocks" "github.com/spf13/cobra" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func setupCompleter(t *testing.T, onlyDirs bool) *completer { @@ -103,3 +106,25 @@ func TestFilerCompleterNoCompletions(t *testing.T) { assert.Equal(t, cobra.ShellCompDirectiveError, directive) assert.Error(t, err) } + +// TestFilerCompleterLocalPartialName uses the OS separator, so on Windows it covers +// "dir\\di": the listed directory must come from the same split as the typed prefix. +func TestFilerCompleterLocalPartialName(t *testing.T) { + root := t.TempDir() + require.NoError(t, os.MkdirAll(filepath.Join(root, "dir", "dirA"), 0o755)) + require.NoError(t, os.MkdirAll(filepath.Join(root, "dir", "dirB"), 0o755)) + require.NoError(t, os.WriteFile(filepath.Join(root, "dir", "fileA"), nil, 0o644)) + require.NoError(t, os.MkdirAll(filepath.Join(root, "dirC"), 0o755)) + t.Chdir(root) + + localFiler, err := filer.NewLocalClient("") + require.NoError(t, err) + c := New(t.Context(), localFiler, true) + c.SetIsLocalPath(true) + + sep := string(filepath.Separator) + completions, directive, err := c.CompletePath("dir" + sep + "di") + require.NoError(t, err) + assert.Equal(t, []string{"dir" + sep + "dirA" + sep, "dir" + sep + "dirB" + sep, "dbfs:/"}, completions) + assert.Equal(t, cobra.ShellCompDirectiveNoSpace, directive) +} From 3be995713b3ae9842312ef3db369724c502309d3 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Tue, 17 Nov 2026 12:20:58 +0000 Subject: [PATCH 6/6] Use filepath.Split for the typed directory in fs path completion filepath.Split already returns the directory part with its separator and, on Windows, splits on both '/' and '\'. Also commit the test's input files instead of creating them in the script. Co-authored-by: Isaac --- acceptance/cmd/fs/complete-local-path/script | 5 ----- acceptance/cmd/fs/complete-local-path/src/file.txt | 0 acceptance/cmd/fs/complete-local-path/src/sub/.gitkeep | 0 libs/filer/completer/completer.go | 10 +++++----- 4 files changed, 5 insertions(+), 10 deletions(-) create mode 100644 acceptance/cmd/fs/complete-local-path/src/file.txt create mode 100644 acceptance/cmd/fs/complete-local-path/src/sub/.gitkeep diff --git a/acceptance/cmd/fs/complete-local-path/script b/acceptance/cmd/fs/complete-local-path/script index efc8f651c06..433dbfb46b7 100644 --- a/acceptance/cmd/fs/complete-local-path/script +++ b/acceptance/cmd/fs/complete-local-path/script @@ -1,11 +1,6 @@ -mkdir -p src/sub -touch src/file.txt - # Shells only offer completions that start with the word being completed # (bash filters them with compgen), so each completion must extend the input. for word in sr ./sr ./ ./src/ src//s; do trace $CLI __complete fs ls "$word" echo "bash offers:" $($CLI __complete fs ls "$word" 2>/dev/null | grep -v '^:' | bash -c 'compgen -W "$(cat)" -- "$1"' _ "$word") done - -rm -rf src diff --git a/acceptance/cmd/fs/complete-local-path/src/file.txt b/acceptance/cmd/fs/complete-local-path/src/file.txt new file mode 100644 index 00000000000..e69de29bb2d diff --git a/acceptance/cmd/fs/complete-local-path/src/sub/.gitkeep b/acceptance/cmd/fs/complete-local-path/src/sub/.gitkeep new file mode 100644 index 00000000000..e69de29bb2d diff --git a/libs/filer/completer/completer.go b/libs/filer/completer/completer.go index a42fa590f5b..ec3b8db5f40 100644 --- a/libs/filer/completer/completer.go +++ b/libs/filer/completer/completer.go @@ -4,7 +4,6 @@ import ( "context" "path" "path/filepath" - "strings" "github.com/databricks/cli/libs/filer" "github.com/spf13/cobra" @@ -42,20 +41,21 @@ func (c *completer) SetIsLocalPath(i bool) { func (c *completer) CompletePath(p string) ([]string, cobra.ShellCompDirective, error) { trailingSeparator := "/" - separators := "/" joinFunc := path.Join + splitFunc := path.Split - // Use filepath functions if we are in a local path. + // Use filepath functions if we are in a local path. On Windows, + // filepath.Split splits on both '/' and '\'. if c.isLocalPath { joinFunc = filepath.Join + splitFunc = filepath.Split trailingSeparator = string(filepath.Separator) - separators += trailingSeparator } // Shells only offer completions that start with the word being completed, // so keep the directory part of p as typed. Joining it with the entry name // would clean it and turn "./sr" into "src", which the shell then discards. - typedDir := p[:strings.LastIndexAny(p, separators)+1] + typedDir, _ := splitFunc(p) // List the directory part of p: nested entries after a trailing separator, // adjacent ones for a partial name. Split it the same way as typedDir;