Skip to content
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .nextchanges/air/7644.md
Original file line number Diff line number Diff line change
@@ -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))
10 changes: 10 additions & 0 deletions acceptance/air/convert-to-dabs/output.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
7 changes: 7 additions & 0 deletions acceptance/air/convert-to-dabs/script
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
5 changes: 5 additions & 0 deletions acceptance/air/convert-to-dabs/test.toml
Original file line number Diff line number Diff line change
@@ -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'
9 changes: 9 additions & 0 deletions acceptance/air/convert-to-dabs/workloads/eval.yaml
Original file line number Diff line number Diff line change
@@ -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
37 changes: 36 additions & 1 deletion cmd/air/convert_to_dabs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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,
Expand Down
96 changes: 96 additions & 0 deletions cmd/air/convert_to_dabs_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -729,3 +733,95 @@ func TestConvertToDabsRejectsUnsupported(t *testing.T) {
require.ErrorContains(t, err, "usage_policy_name is not yet supported")
})
}

// rayLayoutConfig writes ray/<recipe>/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")
}
22 changes: 3 additions & 19 deletions cmd/aitools/recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"slices"
"strconv"
"strings"
"unicode"

"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/aitools/installer"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
Expand Down
15 changes: 15 additions & 0 deletions libs/shellquote/quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
13 changes: 13 additions & 0 deletions libs/shellquote/quote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Loading