diff --git a/.nextchanges/air/7744.md b/.nextchanges/air/7744.md new file mode 100644 index 00000000000..11ec2b7ae0d --- /dev/null +++ b/.nextchanges/air/7744.md @@ -0,0 +1 @@ +* `air run` now warns about missing PyTorch when a pinned snapshot's `include_paths` selects a Python file. ([#7744](https://github.com/databricks/cli/pull/7744)) diff --git a/cmd/air/torchcheck.go b/cmd/air/torchcheck.go index 81bd78c4a94..5d69b84b03b 100644 --- a/cmd/air/torchcheck.go +++ b/cmd/air/torchcheck.go @@ -140,6 +140,12 @@ func findTorchImportAtCommit(ctx context.Context, g gitRepo, plan snapshotPlan) for _, b := range bases { args = append(args, ":(top,glob)"+path.Join(b, "**", "*.py")) } + for _, p := range plan.includePaths { + if strings.HasSuffix(p, ".py") { + file := path.Join(plan.subtreePrefix, filepath.ToSlash(p)) + args = append(args, ":(top,literal)"+file) + } + } out, err := g.run(ctx, args...) if err != nil { // git grep exits 1 when nothing matches. diff --git a/cmd/air/torchcheck_test.go b/cmd/air/torchcheck_test.go index 76e1ef803d9..5d5613f3362 100644 --- a/cmd/air/torchcheck_test.go +++ b/cmd/air/torchcheck_test.go @@ -107,4 +107,23 @@ func TestMissingTorchWarningScansPinnedCommit(t *testing.T) { assert.Contains(t, missingTorchWarning(t.Context(), cfg, filepath.Join(dir, "run.yaml")), "Warning: model/train.py contains a torch import") }) + + t.Run("single file include", func(t *testing.T) { + repo := newTestRepo(t) + writeRepoFile(t, repo, "train.py", "import torch\n") + sha := commitAll(t, repo, "torch") + cfg := writeTorchRunConfig(t, repo, " git:\n commit: "+sha+"\n include_paths:\n - train.py\n") + + assert.Contains(t, missingTorchWarning(t.Context(), cfg, filepath.Join(repo, "run.yaml")), "Warning: train.py contains a torch import") + }) + + t.Run("single file include excludes other imports", func(t *testing.T) { + repo := newTestRepo(t) + writeRepoFile(t, repo, "train.py", "print('hi')\n") + writeRepoFile(t, repo, "other.py", "import torch\n") + sha := commitAll(t, repo, "torch outside include") + cfg := writeTorchRunConfig(t, repo, " git:\n commit: "+sha+"\n include_paths:\n - train.py\n") + + assert.Empty(t, missingTorchWarning(t.Context(), cfg, filepath.Join(repo, "run.yaml"))) + }) }