Repository: opentofu/opentofu
File: internal/initwd/module_install.go (Go)

Code before:
func isSubDirNonExistent(modDir string) (isNonExistent bool, missingDir string) {
	modDir = filepath.Clean(modDir)

	_, err := os.Stat(modDir)
	// If the directory exists, this isn't a missing subdir case
	if err == nil {
		return false, ""
	}
	// If it's not a "does not exist" error, return false (unexpected error)
	if !os.IsNotExist(err) {
		return false, ""
	}

	var missingParts []string
	current := modDir

	for {
		parent := filepath.Dir(current)
		if parent == current {
			return false, "" // reached root without finding it
		}

		missingParts = append([]string{filepath.Base(current)}, missingParts...)

		info, err := os.Stat(parent)
		if err == nil && info.IsDir() { // return the mising parts!
			if len(missingParts) > 0 {
				return true, missingParts[0]
			}
			return true, filepath.Base(current)
		}

		current = parent
	}
}

// InstallModules analyses the root module in the given directory and installs
// all of its direct and transitive dependencies into the given modules
// directory, which must already exist.
//
// Since InstallModules makes possibly-time-consuming calls to remote services,
// a hook interface is supported to allow the caller to be notified when
// each module is installed and, for remote modules, when downloading begins.
// LoadConfig guarantees that two hook calls will not happen concurrently but
// it does not guarantee any particular ordering of hook calls. This mechanism
// is for UI feedback only and does not give the caller any control over the
// process.
//
// If modules are already installed in the target directory, they will be
// skipped unless their source address or version have changed or unless
// the upgrade flag is set.

Review comment (suggestion):
```suggestion
		if err == nil && info.IsDir() { // return the missing parts!
```

Code after:
func isSubDirNonExistent(modDir string) (isNonExistent bool, missingDir string) {
	modDir = filepath.Clean(modDir)

	_, err := os.Stat(modDir)
	// If the directory exists, this isn't a missing subdir case
	if err == nil {
		return false, ""
	}
	// If it's not a "does not exist" error, return false (unexpected error)
	if !os.IsNotExist(err) {
		return false, ""
	}

	var missingParts []string
	current := modDir

	for {
		parent := filepath.Dir(current)
		if parent == current {
			return false, "" // reached root without finding it
		}

		missingParts = append([]string{filepath.Base(current)}, missingParts...)

		info, err := os.Stat(parent)
		if err == nil && info.IsDir() { // return the missing parts!
			if len(missingParts) > 0 {
				return true, missingParts[0]
			}
			return true, filepath.Base(current)
		}

		current = parent
	}
}

// InstallModules analyses the root module in the given directory and installs
// all of its direct and transitive dependencies into the given modules
// directory, which must already exist.
//
// Since InstallModules makes possibly-time-consuming calls to remote services,
// a hook interface is supported to allow the caller to be notified when
// each module is installed and, for remote modules, when downloading begins.
// LoadConfig guarantees that two hook calls will not happen concurrently but
// it does not guarantee any particular ordering of hook calls. This mechanism
// is for UI feedback only and does not give the caller any control over the
// process.
//
// If modules are already installed in the target directory, they will be
// skipped unless their source address or version have changed or unless
// the upgrade flag is set.