Skip to content

fix databricks fs cp command #2901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions cmd/fs/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/fs"
"path"
"path/filepath"
"strings"

"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/cmdio"
Expand Down Expand Up @@ -60,6 +61,23 @@ func (c *copy) cpDirToDir(sourceDir, targetDir string) error {
}

func (c *copy) cpFileToDir(sourcePath, targetDir string) error {
// Remove trailing slash if present
targetDir = strings.TrimSuffix(targetDir, "/")

// Check if target directory exists
_, err := c.targetFiler.Stat(c.ctx, targetDir)
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the path has a trailing / but does not exist, we should error instead to match the semantics of the linux cp command. Could you please update this PR to do so?

if errors.Is(err, fs.ErrNotExist) {
// Create the directory if it doesn't exist
err = c.targetFiler.Mkdir(c.ctx, targetDir)
if err != nil {
return fmt.Errorf("failed to create target directory %s: %w", targetDir, err)
}
} else {
return err
}
}

fileName := filepath.Base(sourcePath)
targetPath := path.Join(targetDir, fileName)

Expand Down Expand Up @@ -196,6 +214,11 @@ func newCpCommand() *cobra.Command {
return c.cpFileToDir(sourcePath, targetPath)
}

// Check if target path ends with a slash, indicating it should be treated as a directory
if strings.HasSuffix(fullTargetPath, "/") || strings.HasSuffix(targetPath, "/") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if strings.HasSuffix(fullTargetPath, "/") || strings.HasSuffix(targetPath, "/") {
if strings.HasSuffix(fullTargetPath, "/") {

Checking fullTargetPath for a trailing slash should be enough. However, this also needs to work on Windows. In that case I'm not sure whether having this change means a breaking change for customers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect using filepath.ToSlash would probably do the trick.

return c.cpFileToDir(sourcePath, targetPath)
}

// case 3: source path is a file, and target path is a file
return c.cpFileToFile(sourcePath, targetPath)
}
Expand Down