diff --git a/.nextchanges/air/convert-replace-workload-bundle.md b/.nextchanges/air/convert-replace-workload-bundle.md new file mode 100644 index 00000000000..885b9da4218 --- /dev/null +++ b/.nextchanges/air/convert-replace-workload-bundle.md @@ -0,0 +1 @@ +* Fix the guidance from `air convert-to-dabs` when the suggested output directory already contains the workload’s bundle. ([#7835](https://github.com/databricks/cli/pull/7835)) diff --git a/cmd/air/convert_to_dabs.go b/cmd/air/convert_to_dabs.go index fdb7ad51f76..f5f96122eec 100644 --- a/cmd/air/convert_to_dabs.go +++ b/cmd/air/convert_to_dabs.go @@ -267,7 +267,7 @@ func bundleCodeSourcePath(ctx context.Context, cfg *runConfig, configPath, bundl } rel, err := filepath.Rel(bundleAbs, root) if err != nil || !filepath.IsLocal(rel) { - return "", codeOutsideBundleError(snap, root, bundleDir) + return "", codeOutsideBundleError(snap, root, bundleDir, cfg.ExperimentName) } return localBundlePath(filepath.ToSlash(rel)), nil } @@ -278,12 +278,15 @@ func bundleCodeSourcePath(ctx context.Context, cfg *runConfig, configPath, bundl // 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 { +func codeOutsideBundleError(snap *snapshotSourceConfig, root, bundleDir, experimentName 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("%s already holds a bundle (%s), and converting there would replace it; pass --output-dir with another directory that contains the code directory, or %s", quotedPath(parent), name, remedy) + if quoted, ok := shellquote.Arg(runtime.GOOS, parent); ok && isWorkloadBundle(parent, experimentName) { + remedy = fmt.Sprintf("pass --output-dir %s --force to replace this workload's bundle, or move the code inside the bundle directory", quoted) + } } 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 { @@ -293,6 +296,16 @@ func codeOutsideBundleError(snap *snapshotSourceConfig, root, bundleDir string) return fmt.Errorf("code_source root_path %q (%s) is not inside the bundle directory %q, and convert-to-dabs can package only code inside the bundle directory; %s", snap.RootPath, quotedPath(root), bundleDir, remedy) } +// isWorkloadBundle requires a single databricks.yml because writeBundle replaces only that file. +func isWorkloadBundle(dir, experimentName string) bool { + path, err := config.FileNames.FindInPath(dir) + if err != nil || filepath.Base(path) != "databricks.yml" || experimentName == "" { + return false + } + existing, diags := config.Load(path) + return !diags.HasError() && existing.Bundle.Name == experimentName +} + // quotedPath quotes a local path the way the --output-dir suggestion does, so a // Windows path keeps single backslashes instead of the doubled ones %q prints. It // falls back to %q for a path the user's shell can't quote, such as one with diff --git a/cmd/air/convert_to_dabs_test.go b/cmd/air/convert_to_dabs_test.go index 01f348b163b..738efc582a4 100644 --- a/cmd/air/convert_to_dabs_test.go +++ b/cmd/air/convert_to_dabs_test.go @@ -834,3 +834,42 @@ func TestConvertToDabsGitPinOutsideBundleDoesNotSuggestOutputDir(t *testing.T) { assert.Contains(t, err.Error(), "; move the code inside the bundle directory") assert.NotContains(t, err.Error(), "--output-dir") } + +// reconvertSharedCode runs the conversion with the same arguments as a workload author. +func reconvertSharedCode(t *testing.T, path string, flags ...string) error { + t.Helper() + cmd := newConvertToDabsCommand() + ctx, _ := cmdio.NewTestContextWithStderr(t.Context()) + cmd.SetContext(ctx) + cmd.SetArgs(append(flags, path)) + return cmd.Execute() +} + +func TestConvertToDabsReconvertSharedCode(t *testing.T) { + dir := t.TempDir() + _, path := rayLayoutConfig(t, dir, "core") + + require.NoError(t, reconvertSharedCode(t, path, "--output-dir", dir)) + quoted, ok := shellquote.Arg(runtime.GOOS, dir) + require.True(t, ok) + require.ErrorContains(t, reconvertSharedCode(t, path), "--output-dir "+quoted+" --force") + require.NoError(t, reconvertSharedCode(t, path, "--output-dir", dir, "--force")) + for _, content := range []string{"bundle: {name: ray-data}", "bundle: {}", "bundle: ["} { + require.NoError(t, os.WriteFile(filepath.Join(dir, "databricks.yml"), []byte(content), 0o600)) + err := reconvertSharedCode(t, path) + require.ErrorContains(t, err, "already holds a bundle") + assert.NotContains(t, err.Error(), "--force") + } + require.NoError(t, os.WriteFile(filepath.Join(dir, "databricks.yaml"), []byte("bundle: {name: ray-core}"), 0o600)) + for _, duplicate := range []bool{true, false} { + yml := filepath.Join(dir, "databricks.yml") + if duplicate { + require.NoError(t, os.WriteFile(yml, []byte("bundle: {name: ray-core}"), 0o600)) + } else { + require.NoError(t, os.Remove(yml)) + } + err := reconvertSharedCode(t, path) + require.ErrorContains(t, err, "already holds a bundle") + assert.NotContains(t, err.Error(), "--force") + } +}