Skip to content

Commit 7688bec

Browse files
Fix lookup precedence bug (#4916)
## Changes Remove lookup if explicit override exists for target ## Why Fixes #4915 [Documentation](https://docs.databricks.com/aws/en/dev-tools/bundles/variables#precedence-order) says the order is: 4. Within any `variables` mappings, among the `targets` mappings within your bundle configuration files. 5. Any `default` value for that variable's definition, among the top-level `variables` mappings within your bundle configuration files. ## Tests Added unit and acceptance tests --------- Co-authored-by: simon <4305831+simonfaltum@users.noreply.github.com>
1 parent 1c84f9f commit 7688bec

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

acceptance/bundle/variables/variable_overrides_in_target/databricks.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ variables:
2222
default: true
2323
description: "A boolean variable"
2424

25+
w_lookup:
26+
description: "A variable with a lookup"
27+
lookup:
28+
cluster: "some-test-cluster" # id 4321
29+
2530
targets:
2631
use-default-variable-values:
2732

@@ -37,3 +42,13 @@ targets:
3742
variables:
3843
foo: "overridden_string"
3944
baz: false
45+
46+
override-lookup-variable-with-value:
47+
variables:
48+
w_lookup: "my-specific-cluster-id"
49+
50+
override-lookup-variable-with-lookup:
51+
variables:
52+
w_lookup:
53+
lookup:
54+
cluster: "some-other-cluster" # id 9876

acceptance/bundle/variables/variable_overrides_in_target/output.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,9 @@
8686
}
8787
}
8888
}
89+
90+
>>> [CLI] bundle validate -o json -t override-lookup-variable-with-value
91+
"my-specific-cluster-id"
92+
93+
>>> [CLI] bundle validate -o json -t override-lookup-variable-with-lookup
94+
"9876"

acceptance/bundle/variables/variable_overrides_in_target/script

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ trace $CLI bundle validate -o json -t use-default-variable-values | jq .resource
22
trace $CLI bundle validate -o json -t override-string-variable | jq .resources
33
trace $CLI bundle validate -o json -t override-int-variable | jq .resources
44
trace $CLI bundle validate -o json -t override-both-bool-and-string-variables | jq .resources
5+
trace $CLI bundle validate -o json -t override-lookup-variable-with-value | jq .variables.w_lookup.value
6+
trace $CLI bundle validate -o json -t override-lookup-variable-with-lookup | jq .variables.w_lookup.value

bundle/config/root.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,12 +347,28 @@ func (r *Root) MergeTargetOverrides(name string) error {
347347
if vDefault.Kind() != dyn.KindInvalid {
348348
defaultPath := varPath.Append(dyn.Key("default"))
349349
root, err = dyn.SetByPath(root, defaultPath, vDefault)
350+
if err != nil {
351+
return root, err
352+
}
353+
354+
// If the target explicitly sets a default value, drop any lookup from the
355+
// root variable definition so SetVariables can assign this default.
356+
lookupPath := varPath.Append(dyn.Key("lookup"))
357+
root, err = dyn.SetByPath(root, lookupPath, dyn.NilValue)
350358
}
351359

352360
vLookup := variable.Get("lookup")
353361
if vLookup.Kind() != dyn.KindInvalid {
354362
lookupPath := varPath.Append(dyn.Key("lookup"))
355363
root, err = dyn.SetByPath(root, lookupPath, vLookup)
364+
if err != nil {
365+
return root, err
366+
}
367+
368+
// If the target explicitly sets a lookup, drop any default value from the
369+
// root variable definition so lookup resolution remains authoritative.
370+
defaultPath := varPath.Append(dyn.Key("default"))
371+
root, err = dyn.SetByPath(root, defaultPath, dyn.NilValue)
356372
}
357373

358374
return root, err

0 commit comments

Comments
 (0)