diff --git a/internal/terraform/ast/variables.go b/internal/terraform/ast/variables.go index 5245ac68f..56453326c 100644 --- a/internal/terraform/ast/variables.go +++ b/internal/terraform/ast/variables.go @@ -16,16 +16,25 @@ func NewVarsFilename(name string) (VarsFilename, bool) { } func IsVarsFilename(name string) bool { - return strings.HasSuffix(name, ".tfvars") && !isIgnoredFile(name) + return (strings.HasSuffix(name, ".tfvars") || + strings.HasSuffix(name, ".tfvars.json")) && + !isIgnoredFile(name) } func (vf VarsFilename) String() string { return string(vf) } +func (vf VarsFilename) IsJSON() bool { + return strings.HasSuffix(string(vf), ".json") +} + func (vf VarsFilename) IsAutoloaded() bool { name := string(vf) - return strings.HasSuffix(name, ".auto.tfvars") || name == "terraform.tfvars" + return strings.HasSuffix(name, ".auto.tfvars") || + strings.HasSuffix(name, ".auto.tfvars.json") || + name == "terraform.tfvars" || + name == "terraform.tfvars.json" } type VarsFiles map[VarsFilename]*hcl.File diff --git a/internal/terraform/parser/variables.go b/internal/terraform/parser/variables.go index 943da9eef..20837b668 100644 --- a/internal/terraform/parser/variables.go +++ b/internal/terraform/parser/variables.go @@ -3,8 +3,6 @@ package parser import ( "path/filepath" - "github.com/hashicorp/hcl/v2" - "github.com/hashicorp/hcl/v2/hclsyntax" "github.com/hashicorp/terraform-ls/internal/terraform/ast" ) @@ -35,8 +33,10 @@ func ParseVariableFiles(fs FS, modPath string) (ast.VarsFiles, ast.VarsDiags, er return nil, nil, err } - f, pDiags := hclsyntax.ParseConfig(src, name, hcl.InitialPos) filename := ast.VarsFilename(name) + + f, pDiags := parseFile(src, filename) + diags[filename] = pDiags if f != nil { files[filename] = f