Skip to content

Commit

Permalink
fix: fix panic on blocks with no labels during validation of label na…
Browse files Browse the repository at this point in the history
…me (#1791)

* fix: fix panic on blocks with no labels during validation of label name

* chore: add changie entry
  • Loading branch information
ansgarm authored Aug 8, 2024
1 parent d1fdc88 commit b7674b8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changes/unreleased/BUG FIXES-20240808-105000.yaml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion internal/features/stacks/decoder/validations/valid_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
Expand Down
20 changes: 20 additions & 0 deletions internal/features/stacks/decoder/validations/valid_name_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit b7674b8

Please sign in to comment.