diff --git a/.changes/unreleased/BUG FIXES-20240808-105000.yaml b/.changes/unreleased/BUG FIXES-20240808-105000.yaml new file mode 100644 index 00000000..61147490 --- /dev/null +++ b/.changes/unreleased/BUG FIXES-20240808-105000.yaml @@ -0,0 +1,6 @@ +kind: BUG FIXES +body: fix panic on blocks with no labels during validation of label name +time: 2024-08-08T10:50:00.988265+02:00 +custom: + Issue: "1791" + Repository: terraform-ls diff --git a/internal/features/stacks/decoder/validations/valid_name.go b/internal/features/stacks/decoder/validations/valid_name.go index 97fe31d4..0ce256d2 100644 --- a/internal/features/stacks/decoder/validations/valid_name.go +++ b/internal/features/stacks/decoder/validations/valid_name.go @@ -59,7 +59,7 @@ func (sb StackBlockValidName) Visit(ctx context.Context, node hclsyntax.Node, no } func hasValidNameLabel(block *hclsyntax.Block, diags hcl.Diagnostics) hcl.Diagnostics { - if !hclsyntax.ValidIdentifier(block.Labels[0]) { + if len(block.Labels) > 0 && !hclsyntax.ValidIdentifier(block.Labels[0]) { diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagError, Summary: fmt.Sprintf("Invalid %q name %q", block.Type, block.Labels[0]), diff --git a/internal/features/stacks/decoder/validations/valid_name_test.go b/internal/features/stacks/decoder/validations/valid_name_test.go new file mode 100644 index 00000000..6592e26a --- /dev/null +++ b/internal/features/stacks/decoder/validations/valid_name_test.go @@ -0,0 +1,20 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package validations + +import ( + "testing" + + "github.com/hashicorp/hcl/v2" + "github.com/hashicorp/hcl/v2/hclsyntax" +) + +func TestHasValidNameLabel_no_label(t *testing.T) { + var diags hcl.Diagnostics + block := hclsyntax.Block{ + Labels: []string{}, // should not panic + } + + hasValidNameLabel(&block, diags) +}