diff --git a/.nextchanges/air/7646.md b/.nextchanges/air/7646.md new file mode 100644 index 00000000000..7b814571dbc --- /dev/null +++ b/.nextchanges/air/7646.md @@ -0,0 +1 @@ +* `air run` now lists a `code_source.root_path` outside a git repository relative to `root_path`, so running from a subdirectory of `root_path` no longer uploads an empty or partial snapshot. ([#7646](https://github.com/databricks/cli/pull/7646)) diff --git a/acceptance/air/run/non-git-subdir.yaml b/acceptance/air/run/non-git-subdir.yaml new file mode 100644 index 00000000000..b6b6c9878c7 --- /dev/null +++ b/acceptance/air/run/non-git-subdir.yaml @@ -0,0 +1,12 @@ +experiment_name: smoke-test +command: python main.py +compute: + accelerator_type: GPU_1xH100 + num_accelerators: 1 +code_source: + type: snapshot + snapshot: + root_path: .. + include_paths: + - main.py + - data diff --git a/acceptance/air/run/output.txt b/acceptance/air/run/output.txt index 8bb2f5c4cce..aa875f4e0cf 100644 --- a/acceptance/air/run/output.txt +++ b/acceptance/air/run/output.txt @@ -65,6 +65,12 @@ Tip: use the --debug flag to see more details and a trace of this error: Exit code: 1 +=== a non-git root_path selects the same files when run from its subdirectory + +>>> [CLI] air run -f workload.yaml --dry-run +Dry run: code_source snapshot would include 2 file(s). +Dry run: configuration for "smoke-test" is valid; not submitting. + === git.remote is rejected >>> [CLI] air run -f git-remote.yaml --dry-run diff --git a/acceptance/air/run/script b/acceptance/air/run/script index 4b0163bdec2..cedb9545208 100644 --- a/acceptance/air/run/script +++ b/acceptance/air/run/script @@ -33,6 +33,16 @@ echo 'print(1)' > .scratch/proj/main.py errcode trace $CLI air run -f ignored-root.yaml --dry-run rm -fr .git .gitignore .scratch +title "a non-git root_path selects the same files when run from its subdirectory" +# sub/main.py shadows main.py; a listing relative to the working directory would pick 1 file. +mkdir -p proj/sub proj/data +echo 'print(1)' > proj/main.py +echo 'print(2)' > proj/sub/main.py +echo 'a,b' > proj/data/x.csv +cp non-git-subdir.yaml proj/sub/workload.yaml +(cd proj/sub && trace $CLI air run -f workload.yaml --dry-run) +rm -fr proj + title "git.remote is rejected" errcode trace $CLI air run -f git-remote.yaml --dry-run diff --git a/cmd/air/snapshot_package.go b/cmd/air/snapshot_package.go index c1dc9c5f89d..956833b96a9 100644 --- a/cmd/air/snapshot_package.go +++ b/cmd/air/snapshot_package.go @@ -146,7 +146,9 @@ func snapshotFiles(ctx context.Context, repoPath string, includePaths []string, if output, err := cmd.CombinedOutput(); err != nil { return nil, fmt.Errorf("failed to initialize temporary git metadata: %w: %s", err, strings.TrimSpace(string(output))) } - args = []string{"--git-dir", gitDir, "--work-tree", repoPath, "ls-files", "-z", "--cached", "--others", "--exclude-standard"} + // Without -C, git resolves include_paths and prints file names relative to the + // CLI's working directory, not repoPath. + args = []string{"-C", repoPath, "--git-dir", gitDir, "--work-tree", repoPath, "ls-files", "-z", "--cached", "--others", "--exclude-standard"} } if len(includePaths) > 0 { diff --git a/cmd/air/snapshot_package_test.go b/cmd/air/snapshot_package_test.go index 068997e2671..961ec560008 100644 --- a/cmd/air/snapshot_package_test.go +++ b/cmd/air/snapshot_package_test.go @@ -217,6 +217,38 @@ func TestCreatePlainTarball_IncludePaths(t *testing.T) { assert.NotContains(t, entries, dirName+"/a.txt") } +func TestSnapshotFiles_NonGitRootFromSubdirectory(t *testing.T) { + // The CLI often runs from the workload's directory, inside root_path. sub/main.py + // shadows main.py, so a listing relative to the working directory picks the wrong + // files instead of failing. + tests := []struct { + name string + includePaths []string + want []string + }{ + {name: "include_paths", includePaths: []string{"main.py", "data"}, want: []string{"data/x.csv", "main.py"}}, + {name: "whole root", want: []string{"data/x.csv", "main.py", "sub/main.py"}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + root := t.TempDir() + writeRepoFile(t, root, "main.py", "print('root')") + writeRepoFile(t, root, "sub/main.py", "print('sub')") + writeRepoFile(t, root, "data/x.csv", "a,b") + t.Chdir(filepath.Join(root, "sub")) + + files, err := snapshotFiles(t.Context(), root, tt.includePaths, false) + require.NoError(t, err) + var got []string + for _, f := range files { + got = append(got, filepath.ToSlash(f.rel)) + } + slices.Sort(got) + assert.Equal(t, tt.want, got) + }) + } +} + func TestCreatePlainTarball_HonorsNestedGitignoreAndNegation(t *testing.T) { repo := t.TempDir() writeRepoFile(t, repo, ".gitignore", "*.log\n!keep.log\n/root-only.txt\n")