github.com/opentofu/opentofu@v1.7.1/internal/configs/configload/loader_load.go (about)

     1  // Copyright (c) The OpenTofu Authors
     2  // SPDX-License-Identifier: MPL-2.0
     3  // Copyright (c) 2023 HashiCorp, Inc.
     4  // SPDX-License-Identifier: MPL-2.0
     5  
     6  package configload
     7  
     8  import (
     9  	"fmt"
    10  
    11  	version "github.com/hashicorp/go-version"
    12  	"github.com/hashicorp/hcl/v2"
    13  
    14  	"github.com/opentofu/opentofu/internal/configs"
    15  )
    16  
    17  // LoadConfig reads the OpenTofu module in the given directory and uses it as the
    18  // root module to build the static module tree that represents a configuration,
    19  // assuming that all required descendent modules have already been installed.
    20  //
    21  // If error diagnostics are returned, the returned configuration may be either
    22  // nil or incomplete. In the latter case, cautious static analysis is possible
    23  // in spite of the errors.
    24  //
    25  // LoadConfig performs the basic syntax and uniqueness validations that are
    26  // required to process the individual modules
    27  func (l *Loader) LoadConfig(rootDir string) (*configs.Config, hcl.Diagnostics) {
    28  	return l.loadConfig(l.parser.LoadConfigDir(rootDir))
    29  }
    30  
    31  // LoadConfigWithTests matches LoadConfig, except the configs.Config contains
    32  // any relevant .tftest.hcl files.
    33  func (l *Loader) LoadConfigWithTests(rootDir string, testDir string) (*configs.Config, hcl.Diagnostics) {
    34  	return l.loadConfig(l.parser.LoadConfigDirWithTests(rootDir, testDir))
    35  }
    36  
    37  func (l *Loader) loadConfig(rootMod *configs.Module, diags hcl.Diagnostics) (*configs.Config, hcl.Diagnostics) {
    38  	if rootMod == nil || diags.HasErrors() {
    39  		// Ensure we return any parsed modules here so that required_version
    40  		// constraints can be verified even when encountering errors.
    41  		cfg := &configs.Config{
    42  			Module: rootMod,
    43  		}
    44  
    45  		return cfg, diags
    46  	}
    47  
    48  	cfg, cDiags := configs.BuildConfig(rootMod, configs.ModuleWalkerFunc(l.moduleWalkerLoad))
    49  	diags = append(diags, cDiags...)
    50  
    51  	return cfg, diags
    52  }
    53  
    54  // moduleWalkerLoad is a configs.ModuleWalkerFunc for loading modules that
    55  // are presumed to have already been installed.
    56  func (l *Loader) moduleWalkerLoad(req *configs.ModuleRequest) (*configs.Module, *version.Version, hcl.Diagnostics) {
    57  	// Since we're just loading here, we expect that all referenced modules
    58  	// will be already installed and described in our manifest. However, we
    59  	// do verify that the manifest and the configuration are in agreement
    60  	// so that we can prompt the user to run "tofu init" if not.
    61  
    62  	key := l.modules.manifest.ModuleKey(req.Path)
    63  	record, exists := l.modules.manifest[key]
    64  
    65  	if !exists {
    66  		return nil, nil, hcl.Diagnostics{
    67  			{
    68  				Severity: hcl.DiagError,
    69  				Summary:  "Module not installed",
    70  				Detail:   "This module is not yet installed. Run \"tofu init\" to install all modules required by this configuration.",
    71  				Subject:  &req.CallRange,
    72  			},
    73  		}
    74  	}
    75  
    76  	var diags hcl.Diagnostics
    77  
    78  	// Check for inconsistencies between manifest and config.
    79  
    80  	// We ignore a nil SourceAddr here, which represents a failure during
    81  	// configuration parsing, and will be reported in a diagnostic elsewhere.
    82  	if req.SourceAddr != nil && req.SourceAddr.String() != record.SourceAddr {
    83  		diags = append(diags, &hcl.Diagnostic{
    84  			Severity: hcl.DiagError,
    85  			Summary:  "Module source has changed",
    86  			Detail:   "The source address was changed since this module was installed. Run \"tofu init\" to install all modules required by this configuration.",
    87  			Subject:  &req.SourceAddrRange,
    88  		})
    89  	}
    90  	if len(req.VersionConstraint.Required) > 0 && record.Version == nil {
    91  		diags = append(diags, &hcl.Diagnostic{
    92  			Severity: hcl.DiagError,
    93  			Summary:  "Module version requirements have changed",
    94  			Detail:   "The version requirements have changed since this module was installed and the installed version is no longer acceptable. Run \"tofu init\" to install all modules required by this configuration.",
    95  			Subject:  &req.SourceAddrRange,
    96  		})
    97  	}
    98  	if record.Version != nil && !req.VersionConstraint.Required.Check(record.Version) {
    99  		diags = append(diags, &hcl.Diagnostic{
   100  			Severity: hcl.DiagError,
   101  			Summary:  "Module version requirements have changed",
   102  			Detail: fmt.Sprintf(
   103  				"The version requirements have changed since this module was installed and the installed version (%s) is no longer acceptable. Run \"tofu init\" to install all modules required by this configuration.",
   104  				record.Version,
   105  			),
   106  			Subject: &req.SourceAddrRange,
   107  		})
   108  	}
   109  
   110  	mod, mDiags := l.parser.LoadConfigDir(record.Dir)
   111  	diags = append(diags, mDiags...)
   112  	if mod == nil {
   113  		// nil specifically indicates that the directory does not exist or
   114  		// cannot be read, so in this case we'll discard any generic diagnostics
   115  		// returned from LoadConfigDir and produce our own context-sensitive
   116  		// error message.
   117  		return nil, nil, hcl.Diagnostics{
   118  			{
   119  				Severity: hcl.DiagError,
   120  				Summary:  "Module not installed",
   121  				Detail:   fmt.Sprintf("This module's local cache directory %s could not be read. Run \"tofu init\" to install all modules required by this configuration.", record.Dir),
   122  				Subject:  &req.CallRange,
   123  			},
   124  		}
   125  	}
   126  
   127  	return mod, record.Version, diags
   128  }