Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nextchanges/bundles/8183.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Fix target variable overrides being dropped with a warning, so the root value was used: short-form and complex-value overrides under the deprecated `environments` key, and unquoted dates under `targets`. Short-form overrides under `environments` of a variable that is not defined now fail with `variable x is not defined but is assigned a value`, as they already did under `targets`. A target override no longer panics when the variable's `type` is not a string, and a scalar `targets` value (such as `targets: false`) is reported at its location instead of failing with `failed to rewrite`. ([#8183](https://github.com/databricks/cli/pull/8183))
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bundle: {name: shapes}
environments: {dev: {}, old: {variables: oops}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bundle: {name: shapes}
targets: {dev: {}, old: {variables: oops}}
Original file line number Diff line number Diff line change
@@ -1,6 +1,48 @@

>>> [CLI] bundle validate
Error: failed to rewrite [TEST_TMP_DIR]/databricks.yml: expected a map or sequence, found bool
Error: expected map, found bool
at targets
in databricks.yml:4:10


Found 1 error

=== malformed-targets

>>> [CLI] bundle validate -t dev
Error: failed to rewrite [TEST_TMP_DIR]/malformed-targets/databricks.yml: expected a map or sequence, found string


Found 1 error

=== malformed-environments

>>> [CLI] bundle validate -t dev
Warning: expected map, found string
at environments.old.variables
in databricks.yml:2:42

Name: shapes
Target: dev
Workspace:
User: [USERNAME]
Path: /Workspace/Users/[USERNAME]/.bundle/shapes/dev

Found 1 warning

=== sequence-targets

>>> [CLI] bundle validate -t dev
Error: failed to rewrite [TEST_TMP_DIR]/sequence-targets/databricks.yml: expected a map or sequence, found string


Found 1 error

=== toplevel

>>> [CLI] bundle validate -o json -t dev
Warning: expected map, found bool
at environments
in databricks.yml:3:15

json.variables.x.value = "target_value";
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
# A scalar targets section is an error.
# A scalar targets section is an error at its location.
musterr trace $CLI bundle validate

# A malformed variables block in another target: an error under targets (also in a sequence),
# a warning under environments, as before.
title malformed-targets
withdir malformed-targets musterr trace $CLI bundle validate -t dev
title malformed-environments
withdir malformed-environments trace $CLI bundle validate -t dev
title sequence-targets
withdir sequence-targets musterr trace $CLI bundle validate -t dev

# environments that is not a map, next to targets: a warning, and the targets override applies.
title toplevel
withdir toplevel trace $CLI bundle validate -o json -t dev | gron.py | grep '^json\.variables\..*\.value'
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bundle: {name: shapes}
targets: [{variables: oops}]
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
Badness = 'The error names an internal step ("failed to rewrite") and has no location, instead of reporting a type error at targets.'
# Cases in subdirectories leave .databricks there.
Ignore = [".databricks"]

# Windows prints the config path with backslashes in the rewrite error.
[[Repls]]
Old = '\\'
New = '/'
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bundle: {name: shapes}
variables: {x: {default: root_value}}
environments: false
targets: {dev: {variables: {x: target_value}}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
bundle:
name: test-bundle

variables:
x:
default: base

# "environments" is the deprecated name of "targets".
environments:
dev:
variables:
x: 2020-01-02
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

>>> [CLI] bundle validate -t dev -o json
Warning: expected map, found time
at targets.dev.variables.x
in databricks.yml:11:10
json.variables.x.default = "2020-01-02";
json.variables.x.value = "2020-01-02";

{
"default": "base",
"value": "base"
}
=== environments

>>> [CLI] bundle validate -t dev -o json
json.variables.x.default = "2020-01-02";
json.variables.x.value = "2020-01-02";
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# A timestamp as a target variable shorthand.
trace $CLI bundle validate -t dev -o json | jq .variables.x
# A timestamp as a target variable shorthand, under targets and under environments.
trace $CLI bundle validate -t dev -o json | gron.py | grep '^json\.variables\.x\.'
title environments
withdir environments trace $CLI bundle validate -t dev -o json | gron.py | grep '^json\.variables\.x\.'
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Badness = 'The timestamp shorthand is dropped with "expected map, found time" instead of being used as the default.'
# The environments case leaves .databricks in its subdirectory.
Ignore = [".databricks"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
bundle: {name: shapes}
variables:
with_sensitive: {default: root_value}
metadata_only: {default: root_value}
value_key: {default: root_value}
complex_with_sensitive: {type: complex, default: {workers: 1}}
complex_metadata_only: {type: complex, default: {workers: 1}}
complex_value_key: {type: complex, default: {workers: 1}}
environments:
dev:
variables:
with_sensitive: {default: target_value, sensitive: false}
metadata_only: {sensitive: false}
value_key: {value: target_value}
complex_with_sensitive: {default: {workers: 2}, sensitive: false}
complex_metadata_only: {sensitive: false}
complex_value_key: {value: {workers: 2}}
18 changes: 11 additions & 7 deletions acceptance/bundle/variables/environments_shorthand/output.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@

>>> [CLI] bundle validate -o json -t dev
Warning: expected map, found string
at environments.dev.variables.short
in databricks.yml:18:14
json.variables.complex.value.key = "target_value";
json.variables.full.value = "target_value";
json.variables.short.value = "target_value";

Warning: unknown field: key
at environments.dev.variables.complex
in databricks.yml:22:9
=== full-form

{"complex":{"key":"root_value"},"full":"target_value","short":"root_value"}
>>> [CLI] bundle validate -o json -t dev
json.variables.complex_metadata_only.value.workers = 1;
json.variables.complex_value_key.value.workers = 1;
json.variables.complex_with_sensitive.value.workers = 2;
json.variables.metadata_only.value = "root_value";
json.variables.value_key.value = "root_value";
json.variables.with_sensitive.value = "target_value";
6 changes: 5 additions & 1 deletion acceptance/bundle/variables/environments_shorthand/script
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
trace $CLI bundle validate -o json -t dev | jq -c '.variables | map_values(.value)'
trace $CLI bundle validate -o json -t dev | gron.py | grep '^json\.variables\..*\.value'

# Full-form overrides under environments keep what normalization applied to them before.
title full-form
withdir full-form trace $CLI bundle validate -o json -t dev | gron.py | grep '^json\.variables\..*\.value'
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Badness = "Short-form target variable overrides under the deprecated 'environments' key (short and complex here) are dropped with a warning, so the root value is used instead of target_value. The same overrides under 'targets', or in the full form, work."
# The full-form case leaves .databricks in its subdirectory.
Ignore = [".databricks"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bundle: {name: shapes}
variables: {x: {type: 5, default: {a: 1}}}
environments: {dev: {variables: {x: {a: 2}}}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cloud = false
EnvMatrix.DMS = ["", "true"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

=== targets

>>> [CLI] bundle validate -o json -t dev
Warning: invalid value "5" for enum field. Valid values are [complex]
at variables.x.type
in databricks.yml:2:23

json.variables.x.value.a = 2;

=== environments

>>> [CLI] bundle validate -o json -t dev
Warning: unknown field: a
at environments.dev.variables.x
in databricks.yml:3:38

Warning: invalid value "5" for enum field. Valid values are [complex]
at variables.x.type
in databricks.yml:2:23

json.variables.x.value.a = 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# A non-string variable type with a map override: normalization reports the type, no panic.
title targets
withdir targets trace $CLI bundle validate -o json -t dev | gron.py | grep '^json\.variables\..*\.value'
title environments
withdir environments trace $CLI bundle validate -o json -t dev | gron.py | grep '^json\.variables\..*\.value'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bundle: {name: shapes}
variables: {x: {type: 5, default: {a: 1}}}
targets: {dev: {variables: {x: {a: 2}}}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Each case leaves .databricks in its subdirectory.
Ignore = [".databricks"]
94 changes: 87 additions & 7 deletions bundle/config/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"fmt"
"os"
"reflect"
"slices"
"strings"
"sync"

"github.com/databricks/cli/bundle/config/resources"
"github.com/databricks/cli/bundle/config/variable"
Expand All @@ -19,6 +21,7 @@ import (
"github.com/databricks/cli/libs/dyn/yamlloader"
"github.com/databricks/cli/libs/log"
"github.com/databricks/cli/libs/structs/structpath"
"github.com/databricks/cli/libs/structs/structtag"
"github.com/databricks/databricks-sdk-go/service/jobs"
)

Expand Down Expand Up @@ -125,6 +128,10 @@ func LoadFromBytes(path string, raw []byte) (*Root, diag.Diagnostics) {
return nil, diag.Errorf("failed to load %s: %v", path, err)
}

if diags := scalarTargetsError(v); diags.HasError() {
return nil, diags
}

// Rewrite configuration tree where necessary.
v, err = rewriteShorthands(v)
if err != nil {
Expand Down Expand Up @@ -497,25 +504,94 @@ func isFullVariableOverrideDef(v dyn.Value) bool {
return false
}

// variableDefinitionKeys returns the keys normalization accepts in a target variable override.
var variableDefinitionKeys = sync.OnceValue(func() []string {
var keys []string
for f := range reflect.TypeFor[variable.TargetVariable]().Fields() {
name := structtag.JSONTag(f.Tag.Get("json")).Name()
if name != "" && name != "-" {
keys = append(keys, name)
}
}
return keys
})

// isLegacyVariableOverrideDef reports whether normalization already applied v as a variable
// definition: it has "default" or "lookup", or it only has definition keys (e.g. {sensitive: false}).
func isLegacyVariableOverrideDef(v dyn.Value) bool {
Comment thread
denik marked this conversation as resolved.
Resolved
mv, ok := v.AsMap()
if !ok {
return false
}
if v.Get("default").IsValid() || v.Get("lookup").IsValid() {
return true
}
for _, pair := range mv.Pairs() {
if !slices.Contains(variableDefinitionKeys(), pair.Key.MustString()) {
return false
}
}
return true
}

// scalarTargetsError reports a "targets" value that is a scalar. It is an error, not a warning
// from normalization, because the bundle could otherwise silently fall back to the default target.
func scalarTargetsError(v dyn.Value) diag.Diagnostics {
t := v.Get("targets")
switch t.Kind() {
case dyn.KindString, dyn.KindBool, dyn.KindInt, dyn.KindFloat, dyn.KindTime:
return diag.Diagnostics{{
Severity: diag.Error,
Summary: fmt.Sprintf("expected %s, found %s", dyn.KindMap, t.Kind()),
Locations: t.Locations(),
Paths: dyn.ToStructPaths(dyn.NewPath(dyn.Key("targets"))),
}}
case dyn.KindInvalid, dyn.KindNil, dyn.KindMap, dyn.KindSequence:
}
return nil
}

// rewriteShorthands performs lightweight rewriting of the configuration
// tree where we allow users to write a shorthand and must rewrite to the full form.
func rewriteShorthands(v dyn.Value) (dyn.Value, error) {
if v.Kind() != dyn.KindMap {
return v, nil
}

// For each target, rewrite the variables block.
return dyn.Map(v, "targets", dyn.Foreach(func(_ dyn.Path, target dyn.Value) (dyn.Value, error) {
// For each target, rewrite the variables block. "environments" is the deprecated name of
// "targets"; it is renamed only after loading, so its variables need the same rewrite.
for _, key := range []string{"targets", "environments"} {
// "environments" was not rewritten before, so its other shapes are left to normalization,
// which reports them at their location. "targets" keeps the rewrite and its errors in any shape.
if key == "environments" && v.Get(key).Kind() != dyn.KindMap {
continue
}
var err error
v, err = dyn.Map(v, key, rewriteTargetShorthands(v, key == "environments"))
if err != nil {
return dyn.InvalidValue, err
}
}
return v, nil
}

// rewriteTargetShorthands rewrites the variables of each target. "environments" was not rewritten
// at all before, so with legacy only what normalization used to drop is rewritten: scalars, and maps
// that are not variable definitions and are values of a complex variable. Everything else is left to
// normalization, as before.
func rewriteTargetShorthands(v dyn.Value, legacy bool) dyn.MapFunc {
return dyn.Foreach(func(_ dyn.Path, target dyn.Value) (dyn.Value, error) {
// Confirm it has a variables block.
if target.Get("variables").Kind() == dyn.KindInvalid {
vars := target.Get("variables")
if vars.Kind() == dyn.KindInvalid || (legacy && vars.Kind() != dyn.KindMap) {
return target, nil
}

// For each variable, normalize its contents if it is a single string.
return dyn.Map(target, "variables", dyn.Foreach(func(p dyn.Path, variable dyn.Value) (dyn.Value, error) {
switch variable.Kind() {

case dyn.KindString, dyn.KindBool, dyn.KindFloat, dyn.KindInt:
case dyn.KindString, dyn.KindBool, dyn.KindFloat, dyn.KindInt, dyn.KindTime:
// Rewrite the variable to a map with a single key called "default".
// This conforms to the variable type. Normalization back to the typed
// configuration will convert this to a string if necessary.
Expand All @@ -525,7 +601,7 @@ func rewriteShorthands(v dyn.Value) (dyn.Value, error) {

case dyn.KindMap, dyn.KindSequence:
// If it's a full variable definition, leave it as is.
if isFullVariableOverrideDef(variable) {
if isFullVariableOverrideDef(variable) || (legacy && isLegacyVariableOverrideDef(variable)) {
return variable, nil
}

Expand All @@ -534,13 +610,17 @@ func rewriteShorthands(v dyn.Value) (dyn.Value, error) {
// Type might not be found if the variable overridden in a separate file
// and configuration is not merged yet.
typeV, err := dyn.GetByPath(v, p.Append(dyn.Key("type")))
if err == nil && typeV.MustString() == "complex" {
if typ, ok := typeV.AsString(); err == nil && ok && typ == "complex" {
return dyn.NewValue(map[string]dyn.Value{
"type": typeV,
"default": variable,
}, variable.Locations()), nil
}

if legacy {
return variable, nil
}

// If it's a shorthand, rewrite it to a full variable definition.
return dyn.NewValue(map[string]dyn.Value{
"default": variable,
Expand All @@ -550,7 +630,7 @@ func rewriteShorthands(v dyn.Value) (dyn.Value, error) {
return variable, nil
}
}))
}))
})
}

// validateVariableOverrides checks that all variables specified
Expand Down
Loading
Loading