diff --git a/.nextchanges/air/7644.md b/.nextchanges/air/7644.md new file mode 100644 index 00000000000..b3eaf02d050 --- /dev/null +++ b/.nextchanges/air/7644.md @@ -0,0 +1 @@ +* `air convert-to-dabs` now suggests the `--output-dir` that converts a working-tree workload whose `code_source.root_path` is outside the bundle directory, instead of suggesting `sync.paths`. ([#7644](https://github.com/databricks/cli/pull/7644)) diff --git a/acceptance/air/convert-to-dabs/output.txt b/acceptance/air/convert-to-dabs/output.txt index 707b03a2eec..72037334a3d 100644 --- a/acceptance/air/convert-to-dabs/output.txt +++ b/acceptance/air/convert-to-dabs/output.txt @@ -1,4 +1,9 @@ +=== code_source outside the bundle directory: the error suggests the code directory's parent as --output-dir + +>>> [CLI] air convert-to-dabs workloads/eval.yaml +Error: code_source root_path "../src" ("[TEST_TMP_DIR]/src") is not inside the bundle directory "workloads", and the bundle convert-to-dabs writes packages only code inside its directory; pass --output-dir [TEST_TMP_DIR] to write the bundle to the code directory's parent, or move the code inside the bundle directory + === convert an AIR run YAML into a DABs bundle (in place, next to the source) >>> [CLI] air convert-to-dabs train.yaml @@ -223,3 +228,8 @@ Workspace: Path: /Workspace/Users/[USERNAME]/.bundle/databricks-ai-v5-smoke-test/dev Validation OK! + +=== a parent that already holds a bundle is not suggested: converting there would replace it + +>>> [CLI] air convert-to-dabs workloads/eval.yaml +Error: code_source root_path "../src" ("[TEST_TMP_DIR]/src") is not inside the bundle directory "workloads", and the bundle convert-to-dabs writes packages only code inside its directory; "[TEST_TMP_DIR]" already holds a bundle (databricks.yml), and converting there would replace it; pass --output-dir with another directory that contains the code directory, or move the code inside the bundle directory diff --git a/acceptance/air/convert-to-dabs/script b/acceptance/air/convert-to-dabs/script index 5dea9e8ae3b..b0ae1894cd5 100644 --- a/acceptance/air/convert-to-dabs/script +++ b/acceptance/air/convert-to-dabs/script @@ -1,3 +1,7 @@ +title "code_source outside the bundle directory: the error suggests the code directory's parent as --output-dir" +# Runs first: the in-place conversion below writes databricks.yml into src's parent. +musterr trace $CLI air convert-to-dabs workloads/eval.yaml + title "convert an AIR run YAML into a DABs bundle (in place, next to the source)" trace $CLI air convert-to-dabs train.yaml @@ -31,3 +35,6 @@ trace grep -E 'environment_version|base_environment' databricks.yml title "the Databricks AI bundle validates" trace $CLI bundle validate + +title "a parent that already holds a bundle is not suggested: converting there would replace it" +musterr trace $CLI air convert-to-dabs workloads/eval.yaml diff --git a/acceptance/air/convert-to-dabs/test.toml b/acceptance/air/convert-to-dabs/test.toml index 956c8efc16e..bf2c71951f8 100644 --- a/acceptance/air/convert-to-dabs/test.toml +++ b/acceptance/air/convert-to-dabs/test.toml @@ -1,3 +1,8 @@ # convert-to-dabs writes the bundle in place (next to train.yaml + src/). These are # generated artifacts, not committed inputs, so exclude them from the repo-diff check. Ignore = ["databricks.yml", "generated_artifacts", "generated-b300", "generated-docker"] + +[[Repls]] +# Fix the quoted code directory path for Windows, where %q doubles its backslashes. +Old = 'TEST_TMP_DIR]\\\\src' +New = 'TEST_TMP_DIR]/src' diff --git a/acceptance/air/convert-to-dabs/workloads/eval.yaml b/acceptance/air/convert-to-dabs/workloads/eval.yaml new file mode 100644 index 00000000000..c7eabae7a93 --- /dev/null +++ b/acceptance/air/convert-to-dabs/workloads/eval.yaml @@ -0,0 +1,9 @@ +experiment_name: eval-smoke-test +command: python train.py --eval +compute: + accelerator_type: GPU_1xA10 + num_accelerators: 1 +code_source: + type: snapshot + snapshot: + root_path: ../src diff --git a/cmd/air/convert_to_dabs.go b/cmd/air/convert_to_dabs.go index 363bc07e4d7..7c9af269462 100644 --- a/cmd/air/convert_to_dabs.go +++ b/cmd/air/convert_to_dabs.go @@ -8,14 +8,17 @@ import ( "os" "path" "path/filepath" + "runtime" "strconv" "strings" + "github.com/databricks/cli/bundle/config" "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/diag" "github.com/databricks/cli/libs/dyn" "github.com/databricks/cli/libs/dyn/yamlsaver" + "github.com/databricks/cli/libs/shellquote" "github.com/spf13/cobra" ) @@ -264,11 +267,43 @@ func bundleCodeSourcePath(ctx context.Context, cfg *runConfig, configPath, bundl } rel, err := filepath.Rel(bundleAbs, root) if err != nil || !filepath.IsLocal(rel) { - return "", fmt.Errorf("code_source root_path %q is not inside the bundle directory %q; bundle deploy can only upload files under the bundle's sync root. Move the code inside the bundle, or set sync.paths in databricks.yml to a directory that contains both (note that widens what gets synced)", snap.RootPath, bundleDir) + return "", codeOutsideBundleError(snap, root, bundleDir) } return localBundlePath(filepath.ToSlash(rel)), nil } +// codeOutsideBundleError explains a code directory outside the bundle directory. For +// a working-tree snapshot it names the code directory's parent as an --output-dir, +// unless that parent already holds a bundle: sibling workloads that share a code +// directory would each be sent there and replace one another's bundle. As in +// codeArtifactFor, a git snapshot is not offered a parent, which may be outside the +// repository. +func codeOutsideBundleError(snap *snapshotSourceConfig, root, bundleDir string) error { + remedy := "move the code inside the bundle directory" + if snap.Git == nil { + parent := filepath.Dir(root) + if name := bundleFileIn(parent); name != "" { + remedy = fmt.Sprintf("%q already holds a bundle (%s), and converting there would replace it; pass --output-dir with another directory that contains the code directory, or %s", parent, name, remedy) + } else if quoted, ok := shellquote.Arg(runtime.GOOS, parent); ok { + remedy = fmt.Sprintf("pass --output-dir %s to write the bundle to the code directory's parent, or %s", quoted, remedy) + } else { + remedy = "pass --output-dir with the code directory's parent, or " + remedy + } + } + return fmt.Errorf("code_source root_path %q (%q) is not inside the bundle directory %q, and the bundle convert-to-dabs writes packages only code inside its directory; %s", snap.RootPath, root, bundleDir, remedy) +} + +// bundleFileIn returns the name of the bundle configuration file in dir, or "" when +// it has none. +func bundleFileIn(dir string) string { + for _, name := range config.FileNames { + if _, err := os.Stat(filepath.Join(dir, name)); err == nil { + return name + } + } + return "" +} + // nv builds a dyn.Value at "position" n: yamlsaver orders a map's keys by their // Location line, so assigning ascending n values fixes the emitted key order. // It routes through dyn.V so nested Go maps/slices are converted recursively, diff --git a/cmd/air/convert_to_dabs_test.go b/cmd/air/convert_to_dabs_test.go index 1ac0a4f8c3c..bc0d9d5e18c 100644 --- a/cmd/air/convert_to_dabs_test.go +++ b/cmd/air/convert_to_dabs_test.go @@ -1,13 +1,17 @@ package aircmd import ( + "fmt" "os" "path/filepath" + "runtime" "strings" "testing" + "github.com/databricks/cli/bundle/config" "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/dyn" + "github.com/databricks/cli/libs/shellquote" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.yaml.in/yaml/v3" @@ -729,3 +733,95 @@ func TestConvertToDabsRejectsUnsupported(t *testing.T) { require.ErrorContains(t, err, "usage_policy_name is not yet supported") }) } + +// rayLayoutConfig writes ray//workload.yaml under dir, snapshotting its parent +// (root_path: ..) to include the shared ray/ray_bootstrap.sh, and loads it. +func rayLayoutConfig(t *testing.T, dir, recipe string) (*runConfig, string) { + t.Helper() + recipeDir := filepath.Join(dir, "ray", recipe) + require.NoError(t, os.MkdirAll(recipeDir, 0o700)) + require.NoError(t, os.WriteFile(filepath.Join(dir, "ray", "ray_bootstrap.sh"), []byte("#!/bin/bash\n"), 0o600)) + cfg := "experiment_name: ray-" + recipe + "\ncommand: bash ray_bootstrap.sh\n" + + "compute: {accelerator_type: GPU_1xA10, num_accelerators: 2}\n" + + "code_source:\n root_path: ..\n include_paths: [ray_bootstrap.sh, " + recipe + "]\n" + path := filepath.Join(recipeDir, "workload.yaml") + require.NoError(t, os.WriteFile(path, []byte(cfg), 0o600)) + loaded, err := loadRunConfig(path) + require.NoError(t, err) + return loaded, path +} + +// A workload whose root_path is its parent directory gets an --output-dir that +// converts it, not advice to edit a databricks.yml that was never written. +func TestConvertToDabsCodeOutsideBundleSuggestsOutputDir(t *testing.T) { + dir := t.TempDir() + loaded, path := rayLayoutConfig(t, dir, "core") + + _, _, err := convertToDabs(t.Context(), loaded, path, filepath.Dir(path)) + require.ErrorContains(t, err, "not inside the bundle") + assert.Contains(t, err.Error(), fmt.Sprintf("(%q)", filepath.Join(dir, "ray"))) + quoted, ok := shellquote.Arg(runtime.GOOS, dir) + require.True(t, ok) + assert.Contains(t, err.Error(), "--output-dir "+quoted+" ") + assert.Contains(t, err.Error(), "or move the code inside the bundle directory") + assert.NotContains(t, err.Error(), "sync.paths") + + root, _, err := convertToDabs(t.Context(), loaded, path, dir) + require.NoError(t, err) + assert.Equal(t, ".", get(t, root, "artifacts."+codeSourceArtifactKey+".path").MustString()) + inc := get(t, root, "artifacts."+codeSourceArtifactKey+".include").MustSequence() + require.Len(t, inc, 2) + assert.Equal(t, "ray/ray_bootstrap.sh", inc[0].MustString()) + assert.Equal(t, "ray/core", inc[1].MustString()) +} + +// Sibling workloads that share a code directory would all be sent to its parent, and +// converting a second one there would replace the first bundle. Once the parent holds +// a bundle, under any of the names DABs accepts, it is not suggested. +func TestConvertToDabsCodeOutsideBundleSkipsParentWithBundle(t *testing.T) { + for _, name := range config.FileNames { + t.Run(name, func(t *testing.T) { + dir := t.TempDir() + rayLayoutConfig(t, dir, "core") + require.NoError(t, os.WriteFile(filepath.Join(dir, name), []byte("bundle:\n name: ray-core\n"), 0o600)) + loaded, path := rayLayoutConfig(t, dir, "data") + + _, _, err := convertToDabs(t.Context(), loaded, path, filepath.Dir(path)) + require.ErrorContains(t, err, "not inside the bundle") + assert.Contains(t, err.Error(), fmt.Sprintf("%q already holds a bundle (%s)", dir, name)) + assert.NotContains(t, err.Error(), "to write the bundle to the code directory's parent") + }) + } +} + +// The suggested directory is quoted for the user's shell, so a parent whose path +// has a space pastes as one argument. +func TestConvertToDabsCodeOutsideBundleQuotesOutputDir(t *testing.T) { + dir := filepath.Join(t.TempDir(), "my recipes") + loaded, path := rayLayoutConfig(t, dir, "core") + + _, _, err := convertToDabs(t.Context(), loaded, path, filepath.Dir(path)) + require.ErrorContains(t, err, "not inside the bundle") + quoted, ok := shellquote.Arg(runtime.GOOS, dir) + require.True(t, ok) + require.NotEqual(t, dir, quoted) + assert.Contains(t, err.Error(), "--output-dir "+quoted+" ") +} + +// A git-pinned snapshot is archived from the bundle root, and a parent of the code +// directory may be outside the repository, so no parent is offered. +func TestConvertToDabsGitPinOutsideBundleDoesNotSuggestOutputDir(t *testing.T) { + outside := t.TempDir() + require.NoError(t, os.MkdirAll(filepath.Join(outside, "src"), 0o700)) + cfg := "experiment_name: outside\ncommand: python train.py\n" + + "compute: {accelerator_type: GPU_1xH100, num_accelerators: 1}\n" + + "code_source:\n root_path: " + filepath.Join(outside, "src") + "\n git:\n commit: abc123\n" + path := writeConfigFile(t, "run.yaml", cfg) + loaded, err := loadRunConfig(path) + require.NoError(t, err) + + _, _, err = convertToDabs(t.Context(), loaded, path, filepath.Dir(path)) + require.ErrorContains(t, err, "not inside the bundle") + assert.Contains(t, err.Error(), "; move the code inside the bundle directory") + assert.NotContains(t, err.Error(), "--output-dir") +} diff --git a/cmd/aitools/recovery.go b/cmd/aitools/recovery.go index ebb02260ca3..ec52afb727a 100644 --- a/cmd/aitools/recovery.go +++ b/cmd/aitools/recovery.go @@ -13,7 +13,6 @@ import ( "slices" "strconv" "strings" - "unicode" "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/aitools/installer" @@ -53,7 +52,7 @@ func renderInstallCommand(goos string, inv installInvocation) (string, bool) { // the backslashes. filepath.ToSlash follows the host OS, not goos. path = strings.ReplaceAll(path, `\`, "/") } - path, ok := quoteArg(goos, path) + path, ok := shellquote.Arg(goos, path) if !ok { return "", false } @@ -65,7 +64,7 @@ func renderInstallCommand(goos string, inv installInvocation) (string, bool) { if inv.agents != "" { // Quoted like --skills: PowerShell reads a bare comma as the array // operator and would split the list into two arguments. - names, ok := quoteArg(goos, inv.agents) + names, ok := shellquote.Arg(goos, inv.agents) if !ok { return "", false } @@ -76,7 +75,7 @@ func renderInstallCommand(goos string, inv installInvocation) (string, bool) { } if inv.skills != "" { // Normalized as install parses it, so "a, b" pastes as one value. - skills, ok := quoteArg(goos, strings.Join(splitAndTrim(inv.skills), ",")) + skills, ok := shellquote.Arg(goos, strings.Join(splitAndTrim(inv.skills), ",")) if !ok { return "", false } @@ -91,21 +90,6 @@ func renderInstallCommand(goos string, inv installInvocation) (string, bool) { return strings.Join(args, " "), true } -// quoteArg quotes a flag value for the shell a user on goos pastes the printed -// command into, only when it needs it, so the common case stays readable: -// cmd.exe or PowerShell on Windows, a POSIX shell elsewhere. It reports false -// for a value no quoting keeps literal there, and for any value with a control -// character: a line break in a pasted command runs what follows it. -func quoteArg(goos, value string) (string, bool) { - if goos == "windows" { - return shellquote.WindowsArg(value) - } - if strings.ContainsFunc(value, unicode.IsControl) { - return "", false - } - return shellquote.BashArg(value), true -} - // commandText renders inv followed by extra flags, for printing after a colon. // When a value cannot be printed so that it pastes literally, it returns false // and a phrase asking for the user's own command with the flags in changed diff --git a/libs/shellquote/quote.go b/libs/shellquote/quote.go index fdc4a8bd5ef..434b9f97a45 100644 --- a/libs/shellquote/quote.go +++ b/libs/shellquote/quote.go @@ -77,3 +77,18 @@ func WindowsArg(s string) (string, bool) { trailing := len(s) - len(strings.TrimRight(s, `\`)) return `"` + s + strings.Repeat(`\`, trailing) + `"`, true } + +// Arg quotes a flag value for the shell a user on goos pastes a printed command +// into, only when it needs it, so the common case stays readable: cmd.exe or +// PowerShell on Windows, a POSIX shell elsewhere. It reports false for a value no +// quoting keeps literal there, and for any value with a control character: a line +// break in a pasted command runs what follows it. +func Arg(goos, value string) (string, bool) { + if goos == "windows" { + return WindowsArg(value) + } + if strings.ContainsFunc(value, unicode.IsControl) { + return "", false + } + return BashArg(value), true +} diff --git a/libs/shellquote/quote_test.go b/libs/shellquote/quote_test.go index 4ed53d3f508..0cfbfd12e6c 100644 --- a/libs/shellquote/quote_test.go +++ b/libs/shellquote/quote_test.go @@ -132,3 +132,16 @@ func TestWindowsArgRejectsWhatNoQuotingKeepsLiteral(t *testing.T) { }) } } + +func TestArg(t *testing.T) { + quoted, ok := Arg("linux", "/tmp/my dir") + assert.True(t, ok) + assert.Equal(t, "'/tmp/my dir'", quoted) + + quoted, ok = Arg("windows", `C:\my dir`) + assert.True(t, ok) + assert.Equal(t, `"C:\my dir"`, quoted) + + _, ok = Arg("linux", "/tmp/a\nb") + assert.False(t, ok) +}