diff --git a/internal/configs/static_scope.go b/internal/configs/static_scope.go index 5165365d7b..e9c2dc5370 100644 --- a/internal/configs/static_scope.go +++ b/internal/configs/static_scope.go @@ -22,9 +22,9 @@ import ( ) // newStaticScope creates a lang.Scope that's backed by the static view of the module represented by the StaticEvaluator -func newStaticScope(eval *StaticEvaluator, stack ...StaticIdentifier) *lang.Scope { +func newStaticScope(eval *StaticEvaluator, stack0 StaticIdentifier, stack ...StaticIdentifier) *lang.Scope { return &lang.Scope{ - Data: staticScopeData{eval, stack}, + Data: staticScopeData{eval, append([]StaticIdentifier{stack0}, stack...)}, ParseRef: addrs.ParseRef, BaseDir: ".", // Always current working directory for now. (same as Evaluator.Scope()) PureOnly: false, @@ -56,16 +56,11 @@ func (s staticScopeData) scope(ident StaticIdentifier) (*lang.Scope, tfdiags.Dia }) } } - return newStaticScope(s.eval, append(s.stack, ident)...), diags + return newStaticScope(s.eval, s.stack[0], append(s.stack[1:], ident)...), diags } // If an error occurs when resolving a dependent value, we need to add additional context to the diagnostics func (s staticScopeData) enhanceDiagnostics(ident StaticIdentifier, diags tfdiags.Diagnostics) tfdiags.Diagnostics { - if len(s.stack) == 0 { - // No enhancement possible if we have nothing on our identifier stack. - // (We should not typically get here, but can in some contrived situations like unit tests) - return diags - } if diags.HasErrors() { top := s.stack[len(s.stack)-1] diags = diags.Append(&hcl.Diagnostic{ diff --git a/internal/configs/static_scope_test.go b/internal/configs/static_scope_test.go index 7a5bce0cca..5e635315b1 100644 --- a/internal/configs/static_scope_test.go +++ b/internal/configs/static_scope_test.go @@ -20,6 +20,9 @@ import ( ) func TestStaticScope_GetInputVariable(t *testing.T) { + test_ident := StaticIdentifier{ + Subject: "local.test_scenario", + } t.Run("valid", func(t *testing.T) { // This covers an assortment of valid cases that we can test in a single // pass because they are all independent of one another. @@ -191,7 +194,7 @@ func TestStaticScope_GetInputVariable(t *testing.T) { } eval := NewStaticEvaluator(mod, call) - scope := newStaticScope(eval) + scope := newStaticScope(eval, test_ident) for name, test := range tests { t.Run(name, func(t *testing.T) { @@ -227,7 +230,7 @@ func TestStaticScope_GetInputVariable(t *testing.T) { assertNoDiagnostics(t, diags) eval := NewStaticEvaluator(mod, call) - scope := newStaticScope(eval) + scope := newStaticScope(eval, test_ident) addr := addrs.InputVariable{Name: "bad_default"} _, moreDiags := scope.Data.GetInputVariable(addr, tfdiags.SourceRange{Filename: "test.tf"}) @@ -265,7 +268,7 @@ func TestStaticScope_GetInputVariable(t *testing.T) { assertNoDiagnostics(t, diags) eval := NewStaticEvaluator(mod, call) - scope := newStaticScope(eval) + scope := newStaticScope(eval, test_ident) addr := addrs.InputVariable{Name: "not_nullable"} _, moreDiags := scope.Data.GetInputVariable(addr, tfdiags.SourceRange{Filename: "test.tf"}) @@ -280,6 +283,10 @@ func TestStaticScope_GetInputVariable(t *testing.T) { } func TestStaticScope_GetLocalValue(t *testing.T) { + test_ident := StaticIdentifier{ + Subject: "local.test_scenario", + } + t.Run("valid", func(t *testing.T) { p := testParser(map[string]string{ "test.tf": ` @@ -303,7 +310,7 @@ func TestStaticScope_GetLocalValue(t *testing.T) { assertNoDiagnostics(t, diags) eval := NewStaticEvaluator(mod, call) - scope := newStaticScope(eval) + scope := newStaticScope(eval, test_ident) addr := addrs.LocalValue{Name: "foo"} got, moreDiags := scope.Data.GetLocalValue(addr, tfdiags.SourceRange{Filename: "test.tf"}) @@ -334,7 +341,7 @@ func TestStaticScope_GetLocalValue(t *testing.T) { assertNoDiagnostics(t, diags) eval := NewStaticEvaluator(mod, call) - scope := newStaticScope(eval) + scope := newStaticScope(eval, test_ident) addr := addrs.LocalValue{Name: "nonexist"} _, moreDiags := scope.Data.GetLocalValue(addr, tfdiags.SourceRange{Filename: "test.tf"})