diff --git a/internal/decoder/validations/unreferenced_origin.go b/internal/decoder/validations/unreferenced_origin.go index 046acc9a..1a99c1b1 100644 --- a/internal/decoder/validations/unreferenced_origin.go +++ b/internal/decoder/validations/unreferenced_origin.go @@ -6,6 +6,7 @@ package validations import ( "context" "fmt" + "slices" "github.com/hashicorp/hcl-lang/decoder" "github.com/hashicorp/hcl-lang/lang" @@ -44,11 +45,12 @@ func UnreferencedOrigins(ctx context.Context, pathCtx *decoder.PathContext) lang continue } - // we only initially validate variables + // we only initially validate variables & local values // resources and data sources can have unknown schema // and will be researched at a later point - firstStep := address[0] - if firstStep.String() != "var" { + supported := []string{"var", "local"} + firstStep := address[0].String() + if !slices.Contains(supported, firstStep) { continue } diff --git a/internal/decoder/validations/unreferenced_origin_test.go b/internal/decoder/validations/unreferenced_origin_test.go index fba76ecf..7c39cd6b 100644 --- a/internal/decoder/validations/unreferenced_origin_test.go +++ b/internal/decoder/validations/unreferenced_origin_test.go @@ -50,6 +50,35 @@ func TestUnreferencedOrigins(t *testing.T) { }, }, }, + { + name: "undeclared local value", + origins: reference.Origins{ + reference.LocalOrigin{ + Range: hcl.Range{ + Filename: "test.tf", + Start: hcl.Pos{}, + End: hcl.Pos{}, + }, + Addr: lang.Address{ + lang.RootStep{Name: "local"}, + lang.AttrStep{Name: "foo"}, + }, + }, + }, + want: lang.DiagnosticsMap{ + "test.tf": hcl.Diagnostics{ + &hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: "No declaration found for \"local.foo\"", + Subject: &hcl.Range{ + Filename: "test.tf", + Start: hcl.Pos{}, + End: hcl.Pos{}, + }, + }, + }, + }, + }, { name: "unsupported variable of complex type", origins: reference.Origins{