Skip to content
Draft
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
11 changes: 11 additions & 0 deletions acceptance/bundle/variables/did-you-mean/databricks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
bundle:
name: did-you-mean

variables:
my_cluster_id:
default: "abc-123"

resources:
jobs:
my_job:
name: "test-${var.my_clster_id}"
5 changes: 5 additions & 0 deletions acceptance/bundle/variables/did-you-mean/out.test.toml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions acceptance/bundle/variables/did-you-mean/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Error: reference does not exist: ${var.my_clster_id}. Did you mean ${var.my_cluster_id}?

Name: did-you-mean
Target: default
Workspace:
User: [USERNAME]
Path: /Workspace/Users/[USERNAME]/.bundle/did-you-mean/default

Found 1 error

Exit code: 1
1 change: 1 addition & 0 deletions acceptance/bundle/variables/did-you-mean/script
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$CLI bundle validate
47 changes: 46 additions & 1 deletion bundle/config/mutator/resolve_variable_references.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"variables",
}

var artifactPath = dyn.MustPathFromString("artifacts")

Check failure on line 46 in bundle/config/mutator/resolve_variable_references.go

View workflow job for this annotation

GitHub Actions / lint

File is not properly formatted (gofumpt)
var resourcesPath = dyn.MustPathFromString("resources")

type resolveVariableReferences struct {
prefixes []string
Expand Down Expand Up @@ -202,6 +203,8 @@
//
normalized, _ := convert.Normalize(b.Config, root, convert.IncludeMissingFields)

suggestFn := m.makeSuggestFn(normalized, prefixes, varPath)

// If the pattern is nil, we resolve references in the entire configuration.
root, err := dyn.MapByPattern(root, m.pattern, func(p dyn.Path, v dyn.Value) (dyn.Value, error) {
// Resolve variable references in all values.
Expand Down Expand Up @@ -235,8 +238,50 @@
}
}

// For references starting with "resources" that are not in
// the resolution prefixes: validate the path against the
// normalized tree. If invalid, emit a warning with a
// suggestion. Either way, skip resolution (resources are
// resolved later by terraform).
if path.HasPrefix(resourcesPath) {
_, lookupErr := m.lookupFn(normalized, path, b)
if lookupErr != nil && dyn.IsNoSuchKeyError(lookupErr) {
key := rewriteToVarShorthand(path.String())
msg := fmt.Sprintf("reference does not exist: ${%s}", key)
if suggestion := suggestFn(key); suggestion != "" {
msg += fmt.Sprintf(". Did you mean ${%s}?", suggestion)
}
diags = diags.Append(diag.Diagnostic{
Severity: diag.Warning,
Summary: msg,
})
}
return dyn.InvalidValue, dynvar.ErrSkipResolution
}

// Check for prefix typos before skipping. If the first
// component is close to a valid prefix, emit a warning
// with a suggestion. The reference is left unresolved to
// avoid breaking existing behavior.
if len(path) > 0 {
firstKey := path[0].Key()
prefixNames := m.suggestPrefixNames(prefixes)
best, dist := closestMatch(firstKey, prefixNames)
if best != "" && dist > 0 {
corrected := make(dyn.Path, len(path))
copy(corrected, path)
corrected[0] = dyn.Key(best)
suggestion := rewriteToVarShorthand(corrected.String())
key := rewriteToVarShorthand(path.String())
diags = diags.Append(diag.Diagnostic{
Severity: diag.Warning,
Summary: fmt.Sprintf("reference does not exist: ${%s}. Did you mean ${%s}?", key, suggestion),
})
}
}

return dyn.InvalidValue, dynvar.ErrSkipResolution
})
}, dynvar.WithSuggestFn(suggestFn))
})
if err != nil {
return dyn.InvalidValue, err
Expand Down
Loading
Loading