@@ -659,16 +659,45 @@ func (i *ModuleInstaller) installRegistryModule(ctx context.Context, req *config
659
659
// Finally we are ready to try actually loading the module.
660
660
mod , mDiags := i .loader .Parser ().LoadConfigDir (modDir )
661
661
if mod == nil {
662
- // nil indicates missing or unreadable directory, so we'll
663
- // discard the returned diags and return a more specific
664
- // error message here. For registry modules this actually
665
- // indicates a bug in the code above, since it's not the
666
- // user's responsibility to create the directory in this case.
667
- diags = diags .Append (& hcl.Diagnostic {
668
- Severity : hcl .DiagError ,
669
- Summary : "Unreadable module directory" ,
670
- Detail : fmt .Sprintf ("The directory %s could not be read. This is a bug in Terraform and should be reported." , modDir ),
671
- })
662
+ // a nil module indicates a missing or unreadable directory, typically
663
+ // this would indicate that Terraform has done something wrong.
664
+ // However, if the subDir is not empty then it is possible that the
665
+ // module was properly downloaded but the user is trying to read a
666
+ // subdirectory that doesn't exist. In this case, it's not a problem
667
+ // with Terraform.
668
+ if len (subDir ) > 0 {
669
+ // Let's make this error message as precise as possible.
670
+ _ , instErr := os .Stat (instPath )
671
+ _ , subErr := os .Stat (modDir )
672
+ if instErr == nil && os .IsNotExist (subErr ) {
673
+ // Then the root directory the module was downloaded to could
674
+ // be loaded fine, but the subdirectory does not exist. This
675
+ // definitely means the user is trying to read a subdirectory
676
+ // that doesn't exist.
677
+ diags = diags .Append (& hcl.Diagnostic {
678
+ Severity : hcl .DiagError ,
679
+ Summary : "Unreadable module subdirectory" ,
680
+ Detail : fmt .Sprintf ("The directory %s does not exist. The target submodule %s does not exist within the target module." , modDir , subDir ),
681
+ })
682
+ } else {
683
+ // There's something else gone wrong here, so we'll report it
684
+ // as a bug in Terraform.
685
+ diags = diags .Append (& hcl.Diagnostic {
686
+ Severity : hcl .DiagError ,
687
+ Summary : "Unreadable module directory" ,
688
+ Detail : fmt .Sprintf ("The directory %s could not be read. This is a bug in Terraform and should be reported." , modDir ),
689
+ })
690
+ }
691
+ } else {
692
+ // If there is no subDir, then somehow the module was downloaded but
693
+ // could not be read even at the root directory it was downloaded into.
694
+ // This is definitely something that Terraform is doing wrong.
695
+ diags = diags .Append (& hcl.Diagnostic {
696
+ Severity : hcl .DiagError ,
697
+ Summary : "Unreadable module directory" ,
698
+ Detail : fmt .Sprintf ("The directory %s could not be read. This is a bug in Terraform and should be reported." , modDir ),
699
+ })
700
+ }
672
701
} else if vDiags := mod .CheckCoreVersionRequirements (req .Path , req .SourceAddr ); vDiags .HasErrors () {
673
702
// If the core version requirements are not met, we drop any other
674
703
// diagnostics, as they may reflect language changes from future
0 commit comments