From 938762baa810c3afb578d733b7b050eccc134bf8 Mon Sep 17 00:00:00 2001 From: Daniel Banck Date: Fri, 5 Nov 2021 11:23:26 +0100 Subject: [PATCH 1/2] identify and parse json tfvars --- internal/terraform/ast/variables.go | 8 +++++++- internal/terraform/parser/variables.go | 6 +++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/internal/terraform/ast/variables.go b/internal/terraform/ast/variables.go index 5245ac68f..0ea9c3bb2 100644 --- a/internal/terraform/ast/variables.go +++ b/internal/terraform/ast/variables.go @@ -16,13 +16,19 @@ 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" 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 From e3c7c2f3e3daff9dc46b28ea0fc07343566f6d57 Mon Sep 17 00:00:00 2001 From: Daniel Banck Date: Mon, 8 Nov 2021 11:17:20 +0100 Subject: [PATCH 2/2] add json to autoloaded check --- internal/terraform/ast/variables.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/terraform/ast/variables.go b/internal/terraform/ast/variables.go index 0ea9c3bb2..56453326c 100644 --- a/internal/terraform/ast/variables.go +++ b/internal/terraform/ast/variables.go @@ -31,7 +31,10 @@ func (vf VarsFilename) IsJSON() bool { 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