Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Panic raised in SymbolsInFile if AttributeSchema has no Constraint #420

Open
rcjsuen opened this issue Oct 10, 2024 · 1 comment
Open

Panic raised in SymbolsInFile if AttributeSchema has no Constraint #420

rcjsuen opened this issue Oct 10, 2024 · 1 comment

Comments

@rcjsuen
Copy link

rcjsuen commented Oct 10, 2024

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

Constraint: as.Constraint.Copy(),

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
@jpogran
Copy link
Contributor

jpogran commented Oct 10, 2024

The error is a bit obtuse, but the root cause is you need more information in your schema in order to generate things like symbols. In this case, if I add a constraint and whether it's optional that parses correctly.

schema := &schema.BodySchema{
    Blocks: map[string]*schema.BlockSchema{
      "variable": {
        Body: &schema.BodySchema{
          Attributes: map[string]*schema.AttributeSchema{
            "targets": {
              Constraint: schema.LiteralType{Type: cty.String},
              IsOptional: true,
            },
          },
        },
      },
    },
  }

For an example schema you could look at the definition of [variable](https://github.com/hashicorp/terraform-schema/blob/main/internal/schema/0.12/variable_block.go. For terraform-ls, we build in Terraform schemas, bundle provider schemas, and use terraform provider --json to provide symbols.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants