From c72951aa211ca66a84805eadffcafd68decb3a05 Mon Sep 17 00:00:00 2001 From: Daniel Banck Date: Tue, 5 Apr 2022 13:57:40 +0200 Subject: [PATCH] add list of filenames to module meta --- earlydecoder/decoder.go | 8 +++++++- module/meta.go | 5 ++--- schema/module_schema.go | 24 ++++++++++++++---------- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/earlydecoder/decoder.go b/earlydecoder/decoder.go index 7cf95dbd..a974b8ba 100644 --- a/earlydecoder/decoder.go +++ b/earlydecoder/decoder.go @@ -2,6 +2,7 @@ package earlydecoder import ( "fmt" + "sort" "github.com/hashicorp/go-version" "github.com/hashicorp/hcl/v2" @@ -11,13 +12,17 @@ import ( func LoadModule(path string, files map[string]*hcl.File) (*module.Meta, hcl.Diagnostics) { var diags hcl.Diagnostics + filenames := make([]string, 0) mod := newDecodedModule() - for _, f := range files { + for filename, f := range files { + filenames = append(filenames, filename) fDiags := loadModuleFromFile(f, mod) diags = append(diags, fDiags...) } + sort.Strings(filenames) + var coreRequirements version.Constraints for _, rc := range mod.RequiredCore { c, err := version.NewConstraint(rc) @@ -166,5 +171,6 @@ func LoadModule(path string, files map[string]*hcl.File) (*module.Meta, hcl.Diag CoreRequirements: coreRequirements, Variables: variables, Outputs: outputs, + Files: filenames, }, diags } diff --git a/module/meta.go b/module/meta.go index af6e483f..54615112 100644 --- a/module/meta.go +++ b/module/meta.go @@ -7,7 +7,8 @@ import ( ) type Meta struct { - Path string + Path string + Files []string Backend *Backend ProviderReferences map[ProviderRef]tfaddr.Provider @@ -15,8 +16,6 @@ type Meta struct { CoreRequirements version.Constraints Variables map[string]Variable Outputs map[string]Output - - // slice of sorted file names? } type ProviderRequirements map[tfaddr.Provider]version.Constraints diff --git a/schema/module_schema.go b/schema/module_schema.go index 6fdb80b2..0488a27d 100644 --- a/schema/module_schema.go +++ b/schema/module_schema.go @@ -91,16 +91,20 @@ func schemaForDependentModuleBlock(localName string, modMeta *module.Meta) (*sch NestedTargetables: targetableOutputs, }) - bodySchema.Targets = &schema.Target{ - Path: lang.Path{ - Path: modMeta.Path, - LanguageID: "terraform", - }, - Range: hcl.Range{ - Filename: "main.tf", // get from modMeta - Start: hcl.InitialPos, - End: hcl.InitialPos, - }, + if len(modMeta.Files) > 0 { + filename := modMeta.Files[0] + + bodySchema.Targets = &schema.Target{ + Path: lang.Path{ + Path: modMeta.Path, + LanguageID: "terraform", + }, + Range: hcl.Range{ + Filename: filename, + Start: hcl.InitialPos, + End: hcl.InitialPos, + }, + } } return bodySchema, nil