Skip to content

Commit 316b897

Browse files
Handle typos in the synthetic "var" prefix
When the first segment is close to "var" (e.g., "vr", "va"), rewrite to variables.X.value and suggest through the normal path. This handles cases like ${vr.my_cluster_id} → ${var.my_cluster_id}. Co-authored-by: Isaac
1 parent 1fd0df4 commit 316b897

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

bundle/config/mutator/suggest.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,16 @@ func (m *resolveVariableReferences) suggest(
146146
}
147147

148148
// Handle var.X → variables.X.value rewriting for internal lookup.
149+
// Also detect typos in the "var" prefix itself (e.g., "vr", "va").
149150
isVar := path.HasPrefix(varPath)
151+
varPrefixCorrected := false
152+
if !isVar && len(path) >= 2 {
153+
if c, _ := closestMatch(path[0].Key(), []string{"var"}); c != "" {
154+
isVar = true
155+
varPrefixCorrected = true
156+
}
157+
}
158+
150159
if isVar {
151160
newPath := dyn.NewPath(dyn.Key("variables"), path[1], dyn.Key("value"))
152161
if len(path) > 2 {
@@ -162,6 +171,15 @@ func (m *resolveVariableReferences) suggest(
162171
}
163172

164173
suggestion := suggestPath(segments, normalized)
174+
175+
// If suggestPath found no deeper fixes but the var prefix itself was
176+
// corrected, verify the rewritten path is valid and use it.
177+
if suggestion == "" && varPrefixCorrected {
178+
if _, err := dyn.GetByPath(normalized, path); err == nil {
179+
suggestion = path.String()
180+
}
181+
}
182+
165183
if suggestion == "" {
166184
return ""
167185
}

bundle/config/mutator/suggest_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,32 @@ func TestSuggestVarRewriting(t *testing.T) {
218218
require.Equal(t, "var.my_cluster_id", suggestion)
219219
}
220220

221+
func TestSuggestVarPrefixTypo(t *testing.T) {
222+
normalized := dyn.V(map[string]dyn.Value{
223+
"variables": dyn.V(map[string]dyn.Value{
224+
"my_cluster_id": dyn.V(map[string]dyn.Value{
225+
"value": dyn.V("abc-123"),
226+
}),
227+
}),
228+
})
229+
230+
m := &resolveVariableReferences{
231+
prefixes: defaultPrefixes,
232+
}
233+
234+
prefixes := []dyn.Path{dyn.MustPathFromString("variables")}
235+
varPath := dyn.NewPath(dyn.Key("var"))
236+
237+
// Typo in var prefix only, variable name is correct.
238+
assert.Equal(t, "var.my_cluster_id", m.suggest("vr.my_cluster_id", normalized, prefixes, varPath))
239+
240+
// Typo in var prefix AND variable name.
241+
assert.Equal(t, "var.my_cluster_id", m.suggest("vr.my_clster_id", normalized, prefixes, varPath))
242+
243+
// Var prefix typo but no matching variable.
244+
assert.Equal(t, "", m.suggest("vr.nonexistent", normalized, prefixes, varPath))
245+
}
246+
221247
func TestSuggestNoSuggestionForCorrectPath(t *testing.T) {
222248
normalized := dyn.V(map[string]dyn.Value{
223249
"variables": dyn.V(map[string]dyn.Value{

0 commit comments

Comments
 (0)