Repository navigation
[AIR] CNXT-2679: warn in dry-run when command uses a path relative to code_source #7865
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vinchenzo-db
wants to merge
8
commits into
main
Choose a base branch
from
vinchenzo-db/cnxt-2679
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+199
−21
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
004917b
[AIR] CNXT-2679: warn in dry-run when command uses a path relative to…
vinchenzo-db 0d006ee
Add changelog fragment
vinchenzo-db 89bbf70
Stop at cd and check container commands
vinchenzo-db af65bcd
Check command paths against the snapshot file list
vinchenzo-db 3acf5b9
List no files for an empty ls-tree and rename the test
vinchenzo-db d1d5274
Trim quotes from a --flag=value value
vinchenzo-db 52b1d5a
Split command words on shell operators and stop at comments
vinchenzo-db 1d783c3
Point the relative script warning at the docs instead of stating runt…
vinchenzo-db File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| * `air run --dry-run` warns when `command` refers to a file by a path relative to `code_source.root_path`, suggests `$CODE_SOURCE_PATH/<file>` and links the docs for command paths. ([#7865](https://github.com/databricks/cli/pull/7865)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| experiment_name: smoke-test | ||
| command: python train.py | ||
| compute: | ||
| accelerator_type: GPU_1xA10 | ||
| num_accelerators: 1 | ||
| code_source: | ||
| root_path: script-src |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| print("hello") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package aircmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "path" | ||
| "slices" | ||
| "strings" | ||
| "unicode" | ||
| ) | ||
|
|
||
| // relativeScriptWarning returns a warning when command (or a container's | ||
| // command) refers to a snapshot file by a path relative to code_source.root_path, | ||
| // such as `python train.py`, or "" when none does. snapshotFiles are the | ||
| // snapshot-relative paths that would be packaged. Whether such a path resolves | ||
| // depends on the runtime, which the CLI doesn't own, so the warning suggests | ||
| // $CODE_SOURCE_PATH and links the docs rather than asserting how the run behaves. | ||
| func relativeScriptWarning(cfg *runConfig, snapshotFiles []string) string { | ||
| if cfg.Command != nil { | ||
| if rel := relativeScriptInCommand(*cfg.Command, snapshotFiles); rel != "" { | ||
| return relativeScriptMessage("command", rel) | ||
| } | ||
| } | ||
| for _, c := range cfg.Containers { | ||
| if c.Command == nil { | ||
| continue | ||
| } | ||
| if rel := relativeScriptInCommand(*c.Command, snapshotFiles); rel != "" { | ||
| return relativeScriptMessage(fmt.Sprintf("containers[%s].command", c.Name), rel) | ||
| } | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| // relativeScriptInCommand returns the first word of command that names a file in | ||
| // snapshotFiles, or "". Words are split on whitespace and shell operators, so | ||
| // `&&cd` is seen as `cd`. A `--flag=value` word is checked by its value; a | ||
| // `NAME=value` word is an environment assignment and skipped. Scanning stops at a | ||
| // `#` comment and at a `cd` or `pushd`: once the command changes directory, as in | ||
| // `cd "$CODE_SOURCE_PATH" && python train.py`, later relative paths are resolved | ||
| // against a directory this check can't know. | ||
| func relativeScriptInCommand(command string, snapshotFiles []string) string { | ||
| for word := range strings.FieldsFuncSeq(command, isShellWordBreak) { | ||
| word = strings.Trim(word, `"'`) | ||
| if word == "cd" || word == "pushd" || strings.HasPrefix(word, "#") { | ||
|
vinchenzo-db marked this conversation as resolved.
Resolved
|
||
| return "" | ||
| } | ||
| if strings.HasPrefix(word, "-") { | ||
| _, word, _ = strings.Cut(word, "=") | ||
|
vinchenzo-db marked this conversation as resolved.
Resolved
|
||
| word = strings.Trim(word, `"'`) | ||
| } else if strings.Contains(word, "=") { | ||
| continue | ||
| } | ||
| if word == "" || strings.Contains(word, "$") || strings.HasPrefix(word, "/") { | ||
| continue | ||
| } | ||
| if slices.Contains(snapshotFiles, path.Clean(word)) { | ||
| return word | ||
| } | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| func isShellWordBreak(r rune) bool { | ||
| return unicode.IsSpace(r) || strings.ContainsRune(";&|()", r) | ||
| } | ||
|
|
||
| func relativeScriptMessage(field, rel string) string { | ||
| var b strings.Builder | ||
| fmt.Fprintf(&b, "Warning: %s refers to %q, a path relative to code_source.root_path. Consider \"$CODE_SOURCE_PATH/%s\" instead. See how command paths and $CODE_SOURCE_PATH work:", field, rel, path.Clean(rel)) | ||
| for _, d := range configDocsBases { | ||
| fmt.Fprintf(&b, "\n %-6s %s%s", d.cloud+":", d.base, yamlConfigDocsPath) | ||
| } | ||
| return b.String() | ||
| } | ||
|
|
||
| // yamlConfigDocsPath is the docs page that describes command paths and | ||
| // $CODE_SOURCE_PATH; see configDocsBases. | ||
| const yamlConfigDocsPath = "/machine-learning/ai-runtime/connecting/cli/yaml-config#command" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| package aircmd | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestRelativeScriptWarning(t *testing.T) { | ||
| snapshot := []string{"train.py", "src/main.py", "configs/base.yaml"} | ||
| cases := []struct { | ||
| name string | ||
| command string | ||
| word string // the command word reported, "" when no warning is expected | ||
| suggest string // the path suggested under $CODE_SOURCE_PATH | ||
| }{ | ||
| {"bare script", "python train.py", "train.py", "train.py"}, | ||
| {"dot-slash script", "./train.py --epochs 1", "./train.py", "train.py"}, | ||
| {"quoted nested script", `torchrun --nproc_per_node=8 "src/main.py"`, "src/main.py", "src/main.py"}, | ||
| {"flag value with equals", "python -m mod --config=configs/base.yaml", "configs/base.yaml", "configs/base.yaml"}, | ||
| {"flag value with space", "python -m mod --config configs/base.yaml", "configs/base.yaml", "configs/base.yaml"}, | ||
| {"quoted flag value with equals", `python -m mod --config="configs/base.yaml"`, "configs/base.yaml", "configs/base.yaml"}, | ||
| {"code source path variable", `python "$CODE_SOURCE_PATH/train.py"`, "", ""}, | ||
| {"absolute path", "python /opt/train.py", "", ""}, | ||
| {"environment assignment", "SCRIPT=train.py python -m mod", "", ""}, | ||
| {"not in snapshot", "python other.py", "", ""}, | ||
| {"parent directory", "python ../tools/prep.py", "", ""}, | ||
| {"directory", "ls src", "", ""}, | ||
| {"cd into code source first", `cd "$CODE_SOURCE_PATH" && python train.py`, "", ""}, | ||
| {"subshell cd", `(cd "$CODE_SOURCE_PATH" && python train.py --epochs 1)`, "", ""}, | ||
| {"pushd", `pushd "$CODE_SOURCE_PATH"; python train.py`, "", ""}, | ||
| {"glued cd", `pip install -r "$CODE_SOURCE_PATH/req.txt"&&cd "$CODE_SOURCE_PATH" && python train.py`, "", ""}, | ||
| {"comment", `python "$CODE_SOURCE_PATH/train.py" # was train.py`, "", ""}, | ||
| {"path before a comment", "python train.py # entrypoint", "train.py", "train.py"}, | ||
| } | ||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| cfg := &runConfig{Command: &tc.command} | ||
| got := relativeScriptWarning(cfg, snapshot) | ||
| if tc.word == "" { | ||
| assert.Empty(t, got) | ||
| return | ||
| } | ||
| assert.Contains(t, got, "Warning: command refers to \""+tc.word+"\"") | ||
| assert.Contains(t, got, "Consider \"$CODE_SOURCE_PATH/"+tc.suggest+"\" instead.") | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestRelativeScriptWarningContainers(t *testing.T) { | ||
| head := `python "$CODE_SOURCE_PATH/train.py"` | ||
| worker := "python train.py" | ||
| cfg := &runConfig{Containers: []containerConfig{ | ||
| {Name: "head", Command: &head}, | ||
| {Name: "worker", Command: &worker}, | ||
| }} | ||
| got := relativeScriptWarning(cfg, []string{"train.py"}) | ||
| assert.Contains(t, got, "Warning: containers[worker].command refers to \"train.py\"") | ||
| assert.Contains(t, got, "Consider \"$CODE_SOURCE_PATH/train.py\" instead.") | ||
| assert.Contains(t, got, "https://docs.databricks.com/aws/en/machine-learning/ai-runtime/connecting/cli/yaml-config#command") | ||
| assert.Contains(t, got, "https://learn.microsoft.com/azure/databricks/machine-learning/ai-runtime/connecting/cli/yaml-config#command") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.