Skip to content

Commit 651eecc

Browse files
denikIsaac
andcommitted
Fix regressions recorded by the config edge-case tests
- apply_default_task_source: an explicit source: "" is user-set; detect it by its definition location (not every task type has ForceSendFields). - pipeline_development_deprecated: warn on any user-written development value, including a reference resolving to an unset field. - rewriteShorthands: a later merged map takes precedence over an earlier one, and merge keys are expanded before detecting a full variable definition. - required: look dashboards up by a path that round-trips keys with path syntax. Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 3ca9616 commit 651eecc

4 files changed

Lines changed: 48 additions & 26 deletions

File tree

‎bundle/config/mutator/resourcemutator/apply_default_task_source.go‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package resourcemutator
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/databricks/cli/bundle"
78
"github.com/databricks/cli/libs/diag"
@@ -49,10 +50,11 @@ func (a *applyDefaultTaskSource) Apply(ctx context.Context, b *bundle.Bundle) di
4950

5051
for i := range job.Tasks {
5152
task := &job.Tasks[i]
53+
taskPath := fmt.Sprintf("resources.jobs.%s.tasks[%d]", name, i)
5254
if task.ForEachTask != nil {
53-
setGitTaskSource(&task.ForEachTask.Task)
55+
setGitTaskSource(b, taskPath+".for_each_task.task", &task.ForEachTask.Task)
5456
}
55-
setGitTaskSource(task)
57+
setGitTaskSource(b, taskPath, task)
5658
}
5759
}
5860
return nil
@@ -62,27 +64,35 @@ func (a *applyDefaultTaskSource) Apply(ctx context.Context, b *bundle.Bundle) di
6264
// supports the field, unless the user already set it.
6365
// The task types are the ones that support the `source` field:
6466
// https://docs.databricks.com/api/workspace/jobs/create
65-
func setGitTaskSource(task *jobs.Task) {
66-
var sources []*jobs.Source
67+
func setGitTaskSource(b *bundle.Bundle, taskPath string, task *jobs.Task) {
68+
type taskSource struct {
69+
key string
70+
source *jobs.Source
71+
}
72+
var sources []taskSource
6773
if task.DbtTask != nil {
68-
sources = append(sources, &task.DbtTask.Source)
74+
sources = append(sources, taskSource{"dbt_task", &task.DbtTask.Source})
6975
}
7076
if task.GenAiComputeTask != nil {
71-
sources = append(sources, &task.GenAiComputeTask.Source)
77+
sources = append(sources, taskSource{"gen_ai_compute_task", &task.GenAiComputeTask.Source})
7278
}
7379
if task.NotebookTask != nil {
74-
sources = append(sources, &task.NotebookTask.Source)
80+
sources = append(sources, taskSource{"notebook_task", &task.NotebookTask.Source})
7581
}
7682
if task.SparkPythonTask != nil {
77-
sources = append(sources, &task.SparkPythonTask.Source)
83+
sources = append(sources, taskSource{"spark_python_task", &task.SparkPythonTask.Source})
7884
}
7985
if task.SqlTask != nil && task.SqlTask.File != nil {
80-
sources = append(sources, &task.SqlTask.File.Source)
86+
sources = append(sources, taskSource{"sql_task.file", &task.SqlTask.File.Source})
8187
}
82-
for _, source := range sources {
83-
if *source == "" {
84-
*source = jobs.SourceGit
85-
return
88+
for _, s := range sources {
89+
// An explicit `source: ""` is set by the user, although the typed value is
90+
// empty; not every task type has ForceSendFields to record it, but it is
91+
// defined in the configuration.
92+
if *s.source != "" || b.Config.DefinitionLocation(taskPath+"."+s.key+".source").File != "" {
93+
continue
8694
}
95+
*s.source = jobs.SourceGit
96+
return
8797
}
8898
}

‎bundle/config/root.go‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,7 @@ func mappingValue(node *yaml.Node, key string) *yaml.Node {
361361
}
362362

363363
// expandMergeKeys returns a copy of the mapping node with "<<" merge keys replaced
364-
// by the pairs they merge in (keys set explicitly take precedence), so the pairs
365-
// can be rewritten individually.
364+
// by the pairs they merge in, so the pairs can be rewritten individually.
366365
func expandMergeKeys(node *yaml.Node) *yaml.Node {
367366
var merged, explicit []*yaml.Node
368367
for i := 0; i+1 < len(node.Content); i += 2 {
@@ -383,16 +382,29 @@ func expandMergeKeys(node *yaml.Node) *yaml.Node {
383382
if merged == nil {
384383
return node
385384
}
385+
// Like the YAML decoder, a later merged map takes precedence over an earlier one and
386+
// keys set explicitly take precedence over all merged maps.
386387
out := *node
387-
out.Content = explicit
388-
for i := 0; i+1 < len(merged); i += 2 {
389-
if mappingValue(&out, merged[i].Value) == nil {
390-
out.Content = append(out.Content, merged[i], merged[i+1])
388+
out.Content = nil
389+
for _, kvs := range [][]*yaml.Node{merged, explicit} {
390+
for i := 0; i+1 < len(kvs); i += 2 {
391+
setMappingValue(&out, kvs[i], kvs[i+1])
391392
}
392393
}
393394
return &out
394395
}
395396

397+
// setMappingValue replaces the value of key in the mapping node, or appends the pair.
398+
func setMappingValue(node, key, value *yaml.Node) {
399+
for i := 0; i+1 < len(node.Content); i += 2 {
400+
if node.Content[i].Value == key.Value {
401+
node.Content[i+1] = value
402+
return
403+
}
404+
}
405+
node.Content = append(node.Content, key, value)
406+
}
407+
396408
func mappingKeys(node *yaml.Node) []string {
397409
var keys []string
398410
for i := 0; i+1 < len(node.Content); i += 2 {
@@ -438,7 +450,7 @@ func rewriteShorthands(root *yaml.Node) {
438450

439451
case variable.Kind == yaml.MappingNode || variable.Kind == yaml.SequenceNode:
440452
// If it's a full variable definition, leave it as is.
441-
if variable.Kind == yaml.MappingNode && isFullVariableOverrideDef(mappingKeys(variable)) {
453+
if variable.Kind == yaml.MappingNode && isFullVariableOverrideDef(mappingKeys(expandMergeKeys(variable))) {
442454
continue
443455
}
444456

‎bundle/config/validate/pipeline_development_deprecated.go‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ func (v *pipelineDevelopmentDeprecated) Apply(_ context.Context, b *bundle.Bundl
3737
pipelines := b.Config.Resources.Pipelines
3838
for _, key := range slices.Sorted(maps.Keys(pipelines)) {
3939
pipeline := pipelines[key]
40-
//nolint:staticcheck // SA1019: pipeline development is deprecated in the SDK but remains a supported bundle config field
41-
if pipeline == nil || (!pipeline.Development && !slices.Contains(pipeline.ForceSendFields, "Development")) {
40+
if pipeline == nil {
4241
continue
4342
}
4443

‎bundle/config/validate/required.go‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,14 @@ func errorForMissingFields(ctx context.Context, b *bundle.Bundle) diag.Diagnosti
9595

9696
diags := diag.Diagnostics{}
9797
for key, dashboard := range b.Config.Resources.Dashboards {
98+
path := structpath.NewPath(nil, "resources", "dashboards", key)
9899
if dashboard.DisplayName == "" {
99-
nameLocations = append(nameLocations, b.Config.GetLocations("resources.dashboards."+key)...)
100-
namePaths = append(namePaths, structpath.NewPath(nil, "resources", "dashboards", key))
100+
nameLocations = append(nameLocations, b.Config.GetLocations(path.String())...)
101+
namePaths = append(namePaths, path)
101102
}
102103
if dashboard.WarehouseId == "" {
103-
warehouseIdLocations = append(warehouseIdLocations, b.Config.GetLocations("resources.dashboards."+key)...)
104-
warehouseIdPaths = append(warehouseIdPaths, structpath.NewPath(nil, "resources", "dashboards", key))
104+
warehouseIdLocations = append(warehouseIdLocations, b.Config.GetLocations(path.String())...)
105+
warehouseIdPaths = append(warehouseIdPaths, path)
105106
}
106107
}
107108

0 commit comments

Comments
 (0)