hcl-lang icon indicating copy to clipboard operation
hcl-lang copied to clipboard

Panic raised in `SymbolsInFile` if `AttributeSchema` has no `Constraint`

Open rcjsuen opened this issue 1 year ago • 1 comments

I modified the code provided here and triggered a panic. The error is on this line.

https://github.com/hashicorp/hcl-lang/blob/f5c7d040b70fffe6d92da992408c0f8481bb260c/schema/attribute_schema.go#L145

package main

import (
	"context"

	"github.com/hashicorp/hcl-lang/decoder"
	"github.com/hashicorp/hcl-lang/lang"
	"github.com/hashicorp/hcl-lang/reference"
	"github.com/hashicorp/hcl-lang/schema"
	"github.com/hashicorp/hcl/v2"
	"github.com/hashicorp/hcl/v2/hclsyntax"
)

type PathReader struct {
	Files map[string]*hcl.File
}

var _ decoder.PathReader = &PathReader{}

func (pr *PathReader) PathContext(path lang.Path) (*decoder.PathContext, error) {
	schema := &schema.BodySchema{
		Blocks: map[string]*schema.BlockSchema{
			"variable": {
				Body: &schema.BodySchema{
					Attributes: map[string]*schema.AttributeSchema{
						"targets": {},
					},
				},
			},
		},
	}

	pathContext := &decoder.PathContext{
		Schema:           schema,
		ReferenceOrigins: make(reference.Origins, 0),
		ReferenceTargets: make(reference.Targets, 0),
		Files:            pr.Files,
	}

	return pathContext, nil
}

func (pr *PathReader) Paths(ctx context.Context) []lang.Path {
	return []lang.Path{}
}

func main() {
	f, pDiags := hclsyntax.ParseConfig([]byte("variable{}"), "example.tf", hcl.InitialPos)
	if len(pDiags) > 0 {
		panic("Found errrors stop parsing")
	}

	decoder := decoder.NewDecoder(&PathReader{
		Files: map[string]*hcl.File{
			"example.tf": f,
		},
	})

	pathDecoder, err := decoder.Path(lang.Path{
		Path:       ".",
		LanguageID: "terraform",
	})
	if err != nil {
		panic(err)
	}

	pathDecoder.SymbolsInFile("example.tf")
}
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x18 pc=0x10077c064]

goroutine 1 [running]:
github.com/hashicorp/hcl-lang/schema.(*AttributeSchema).Copy(0x140000e8000)
	../go/pkg/mod/github.com/hashicorp/[email protected]/schema/attribute_schema.go:145 +0x174
github.com/hashicorp/hcl-lang/schema.(*BodySchema).Copy(0x140000ec000)
	../go/pkg/mod/github.com/hashicorp/[email protected]/schema/body_schema.go:218 +0x438
github.com/hashicorp/hcl-lang/decoder/internal/schemahelper.MergeBlockBodySchemas(0x140000a5040, 0x140000ea000)
	../go/pkg/mod/github.com/hashicorp/[email protected]/decoder/internal/schemahelper/block_schema.go:14 +0x48
github.com/hashicorp/hcl-lang/decoder.(*PathDecoder).symbolsForBody(0x140000e8090, {0x1008b53a8?, 0x140000d84d0?}, 0x140000ec0a0)
	../go/pkg/mod/github.com/hashicorp/[email protected]/decoder/symbols.go:119 +0x384
github.com/hashicorp/hcl-lang/decoder.(*PathDecoder).SymbolsInFile(0x140000e8090, {0x10080979a, 0xa})
	../go/pkg/mod/github.com/hashicorp/[email protected]/decoder/symbols.go:37 +0xd8
main.main()
	../code/docker/docker-language-server/main/main.go:67 +0x15c
exit status 2

rcjsuen avatar Oct 10 '24 18:10 rcjsuen