func getModuleDir()

in internal/meta/base_meta.go [1176:1201]


func getModuleDir(modulePaths []string, moduleDir string) (string, error) {
	// Ensure the module path is something called by the main module
	// We are following the module source and recursively call the LoadModule below. This is valid since we only support local path modules.
	// (remote sources are not supported since we will end up generating config to that module, it only makes sense for local path modules)
	module, err := tfconfig.LoadModule(moduleDir)
	if err != nil {
		return "", fmt.Errorf("loading main module: %v", err)
	}

	for i, moduleName := range modulePaths {
		mc := module.ModuleCalls[moduleName]
		if mc == nil {
			return "", fmt.Errorf("no module %q invoked by the root module", strings.Join(modulePaths[:i+1], "."))
		}
		// See https://developer.hashicorp.com/terraform/language/modules/sources#local-paths
		if !strings.HasPrefix(mc.Source, "./") && !strings.HasPrefix(mc.Source, "../") {
			return "", fmt.Errorf("the source of module %q is not a local path", strings.Join(modulePaths[:i+1], "."))
		}
		moduleDir = filepath.Join(moduleDir, mc.Source)
		module, err = tfconfig.LoadModule(moduleDir)
		if err != nil {
			return "", fmt.Errorf("loading module %q: %v", strings.Join(modulePaths[:i+1], "."), err)
		}
	}
	return moduleDir, nil
}