diff --git a/.nextchanges/cli/fs-complete-local-path.md b/.nextchanges/cli/fs-complete-local-path.md new file mode 100644 index 00000000000..d0fab2ace59 --- /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. ([#7679](https://github.com/databricks/cli/pull/7679)) 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..9e971dd0c79 --- /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: ./src/ + +>>> [CLI] __complete fs ls ./ +./src/ +dbfs:/ +:2 +Completion ended with directive: ShellCompDirectiveNoSpace +bash offers: ./src/ + +>>> [CLI] __complete fs ls ./src/ +./src/sub/ +dbfs:/ +:2 +Completion ended with directive: ShellCompDirectiveNoSpace +bash offers: ./src/sub/ + +>>> [CLI] __complete fs ls src//s +src//sub/ +dbfs:/ +:2 +Completion ended with directive: ShellCompDirectiveNoSpace +bash offers: src//sub/ diff --git a/acceptance/cmd/fs/complete-local-path/script b/acceptance/cmd/fs/complete-local-path/script new file mode 100644 index 00000000000..433dbfb46b7 --- /dev/null +++ b/acceptance/cmd/fs/complete-local-path/script @@ -0,0 +1,6 @@ +# 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 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/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 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) } diff --git a/libs/filer/completer/completer.go b/libs/filer/completer/completer.go index 0f7f8b71122..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" @@ -43,20 +42,28 @@ func (c *completer) SetIsLocalPath(i bool) { func (c *completer) CompletePath(p string) ([]string, cobra.ShellCompDirective, error) { trailingSeparator := "/" 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) } - // 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) + // 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, _ := 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; + // 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) @@ -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..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 { @@ -89,7 +92,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) } @@ -102,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) +}