Skip to content

Commit 2c96f11

Browse files
Add --strict to bundle validate (#4232)
Some of our warnings cause errors down the line. For example, if required values are missing or invalid enum values are provided. The `--strict` flag allows users to opt-in into a more strict validation that can help them enforce our recommendations better.
1 parent 665c353 commit 2c96f11

File tree

7 files changed

+105
-1
lines changed

7 files changed

+105
-1
lines changed

acceptance/bundle/help/bundle-validate/output.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Validate bundle configuration for errors, warnings and recommendations.
55
Run validate before deploy to catch configuration issues early:
66
databricks bundle validate # Validate default target
77
databricks bundle validate --target prod # Validate specific target
8+
databricks bundle validate --strict # Fail on warnings
89

910
Validation checks the configuration syntax and schema, permissions etc.
1011

@@ -14,7 +15,8 @@ Usage:
1415
databricks bundle validate [flags]
1516

1617
Flags:
17-
-h, --help help for validate
18+
-h, --help help for validate
19+
--strict Treat warnings as errors
1820

1921
Global Flags:
2022
--debug enable debug logging
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
bundle:
2+
name: test-bundle
3+
4+
variables:
5+
my_variable:
6+
type: INVALID_TYPE
7+
default: "value"
8+
9+
targets:
10+
foo:
11+
default: true
12+
13+
bar:
14+
artifacts:
15+
my_artifact:
16+
type: INVALID_TYPE
17+
build: "echo 'hello'"

acceptance/bundle/validate/strict/out.test.toml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
>>> [CLI] bundle validate
3+
Warning: invalid value "INVALID_TYPE" for enum field. Valid values are [complex]
4+
at variables.my_variable.type
5+
in databricks.yml:6:11
6+
7+
Name: test-bundle
8+
Target: foo
9+
Workspace:
10+
User: [USERNAME]
11+
Path: /Workspace/Users/[USERNAME]/.bundle/test-bundle/foo
12+
13+
Found 1 warning
14+
15+
>>> [CLI] bundle validate --strict
16+
Warning: invalid value "INVALID_TYPE" for enum field. Valid values are [complex]
17+
at variables.my_variable.type
18+
in databricks.yml:6:11
19+
20+
Name: test-bundle
21+
Target: foo
22+
Workspace:
23+
User: [USERNAME]
24+
Path: /Workspace/Users/[USERNAME]/.bundle/test-bundle/foo
25+
26+
Found 1 warning
27+
Error: 1 warning was found. Warnings are not allowed in strict mode
28+
29+
Exit code: 1
30+
31+
>>> [CLI] bundle validate --strict -t bar
32+
Warning: invalid value "INVALID_TYPE" for enum field. Valid values are [complex]
33+
at variables.my_variable.type
34+
in databricks.yml:6:11
35+
36+
Warning: invalid value "INVALID_TYPE" for enum field. Valid values are [whl jar]
37+
at artifacts.my_artifact.type
38+
in databricks.yml:16:15
39+
40+
Name: test-bundle
41+
Target: bar
42+
Workspace:
43+
User: [USERNAME]
44+
Path: /Workspace/Users/[USERNAME]/.bundle/test-bundle/bar
45+
46+
Found 2 warnings
47+
Error: 2 warnings were found. Warnings are not allowed in strict mode
48+
49+
Exit code: 1
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Without --strict, warnings don't cause exit code 1
2+
trace $CLI bundle validate
3+
4+
# With --strict, warnings cause exit code 1
5+
errcode trace $CLI bundle validate --strict
6+
7+
errcode trace $CLI bundle validate --strict -t bar

cmd/bundle/validate.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package bundle
22

33
import (
44
"encoding/json"
5+
"fmt"
56

67
"github.com/databricks/cli/bundle"
78
"github.com/databricks/cli/bundle/render"
@@ -35,6 +36,7 @@ func newValidateCommand() *cobra.Command {
3536
Run validate before deploy to catch configuration issues early:
3637
databricks bundle validate # Validate default target
3738
databricks bundle validate --target prod # Validate specific target
39+
databricks bundle validate --strict # Fail on warnings
3840
3941
Validation checks the configuration syntax and schema, permissions etc.
4042
@@ -43,8 +45,10 @@ Please run this command before deploying to ensure configuration quality.`,
4345
}
4446

4547
var includeLocations bool
48+
var strict bool
4649
cmd.Flags().BoolVar(&includeLocations, "include-locations", false, "Include location information in the output")
4750
cmd.Flags().MarkHidden("include-locations")
51+
cmd.Flags().BoolVar(&strict, "strict", false, "Treat warnings as errors")
4852

4953
cmd.RunE = func(cmd *cobra.Command, args []string) error {
5054
b, err := utils.ProcessBundle(cmd, utils.ProcessOptions{
@@ -74,6 +78,18 @@ Please run this command before deploying to ensure configuration quality.`,
7478
}
7579
}
7680

81+
// In strict mode, treat warnings as errors.
82+
numWarnings := logdiag.NumWarnings(ctx)
83+
if err == nil && strict && numWarnings > 0 {
84+
prefix := ""
85+
if numWarnings == 1 {
86+
prefix = "1 warning was found"
87+
} else {
88+
prefix = fmt.Sprintf("%d warnings were found", numWarnings)
89+
}
90+
return fmt.Errorf("%s. Warnings are not allowed in strict mode", prefix)
91+
}
92+
7793
return err
7894
}
7995

libs/logdiag/logdiag.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ func HasError(ctx context.Context) bool {
7575
return val.Errors > 0
7676
}
7777

78+
func NumWarnings(ctx context.Context) int {
79+
val := read(ctx)
80+
val.mu.Lock()
81+
defer val.mu.Unlock()
82+
83+
return val.Warnings
84+
}
85+
7886
func SetSeverity(ctx context.Context, target diag.Severity) {
7987
val := read(ctx)
8088
val.mu.Lock()

0 commit comments

Comments
 (0)