@@ -2,7 +2,6 @@ package parser
22
33import (
44 "context"
5- "errors"
65 "fmt"
76 "io/fs"
87 "path"
@@ -78,9 +77,19 @@ func (e *evaluator) loadModule(ctx context.Context, b *terraform.Block) (*Module
7877 return nil , fmt .Errorf ("could not read module source attribute at %s" , metadata .Range ().String ())
7978 }
8079
81- if def , err := e .loadModuleFromTerraformCache (ctx , b , source ); err == nil {
82- e .logger .Debug ("Using module from Terraform cache .terraform/modules" , log .String ("source" , source ))
83- return def , nil
80+ if e .moduleMetadata != nil {
81+ // if we have module metadata we can parse all the modules as they'll be cached locally!
82+ def , err := e .loadModuleFromTerraformCache (ctx , b , source )
83+ if err == nil {
84+ e .logger .Debug ("Using module from Terraform cache .terraform/modules" ,
85+ log .String ("source" , source ))
86+ return def , nil
87+ }
88+
89+ e .logger .Debug ("Failed to load module from .terraform cache" ,
90+ log .String ("module_source" , source ),
91+ log .Err (err ),
92+ )
8493 }
8594
8695 // we don't have the module installed via 'terraform init' so we need to grab it...
@@ -89,38 +98,31 @@ func (e *evaluator) loadModule(ctx context.Context, b *terraform.Block) (*Module
8998
9099func (e * evaluator ) loadModuleFromTerraformCache (ctx context.Context , b * terraform.Block , source string ) (* ModuleDefinition , error ) {
91100 var modulePath string
92- if e .moduleMetadata != nil {
93- // if we have module metadata we can parse all the modules as they'll be cached locally!
94- moduleKey := b .ModuleKey ()
95- for _ , module := range e .moduleMetadata .Modules {
96- if module .Key == moduleKey {
97- modulePath = path .Clean (path .Join (e .projectRootPath , module .Dir ))
98- break
99- }
101+ moduleKey := b .ModuleKey ()
102+ for _ , module := range e .moduleMetadata .Modules {
103+ if module .Key == moduleKey {
104+ modulePath = path .Clean (path .Join (e .projectRootPath , module .Dir ))
105+ break
100106 }
101107 }
102108 if modulePath == "" {
103- return nil , errors . New ( "failed to load module from .terraform/modules" )
109+ return nil , fmt . Errorf ( "resolve module with key %q from .terraform/modules", moduleKey )
104110 }
111+
105112 if strings .HasPrefix (source , "." ) {
106113 source = ""
107114 }
108115
109- if prefix , relativeDir , ok := strings .Cut (source , "//" ); ok && ! strings .HasSuffix (prefix , ":" ) && strings .Count (prefix , "/" ) == 2 {
110- if ! strings .HasSuffix (modulePath , relativeDir ) {
111- modulePath = fmt .Sprintf ("%s/%s" , modulePath , relativeDir )
112- }
113- }
114-
115116 e .logger .Debug ("Module resolved using modules.json" ,
116117 log .String ("block" , b .FullName ()),
117118 log .String ("source" , source ),
118119 log .String ("modulePath" , modulePath ),
119120 )
120121 moduleParser := e .parentParser .newModuleParser (e .filesystem , source , modulePath , b .Label (), b )
121122 if err := moduleParser .ParseFS (ctx , modulePath ); err != nil {
122- return nil , err
123+ return nil , fmt . Errorf ( "parse module filesystem: %w" , err )
123124 }
125+
124126 return & ModuleDefinition {
125127 Name : b .Label (),
126128 Path : modulePath ,
0 commit comments