Repository: opentofu/opentofu
File: internal/getproviders/package_location_local_archive.go (Go)

Code before:
				"failed to calculate checksum for %s %s package at %s: %w",
				meta.Provider, meta.Version, meta.Location, err,
			)
		} else if !matches {
			return authResult, fmt.Errorf(
				"the current package for %s %s doesn't match any of the checksums previously recorded in the dependency lock file; for more information: https://opentofu.org/docs/language/files/dependency-lock/#checksum-verification",
				meta.Provider, meta.Version,
			)
		}
	}

	filename := meta.Location.String()

	// NOTE: Packages are immutable, but we may want to skip overwriting the existing
	// files in due to specific scenarios defined below.

	if entries, err := os.ReadDir(targetDir); err == nil && len(entries) != 0 {
		// If the package might already be installed, we should try to skip overwriting the contents.
		// When run with TF_PLUGIN_CACHE_DIR or similar, a given provider might already be executing
		// and therefore locking the provider binary in the target directory (preventing the overwrite below)
		//
		// This does incur the overhead of two additional hash computations and could be
		// skipped with smarter checks around re-use scenarios in the future.

		targetHash, targetErr := PackageHashV1(PackageLocalDir(targetDir))
		fileHash, fileErr := PackageHashV1(meta.Location)

		if targetHash == fileHash && fileErr == nil && targetErr == nil {
			// Package is properly installed, bad or missing lock file will be caught elsewhere
			log.Printf("[INFO] Skipping local installation of provider %s %s as the existing contents already match the new contents", meta.Provider, meta.Version)
			return authResult, nil
		}
	}

	//nolint:mnd // magic number predates us using this linter
	err := unzip.Decompress(targetDir, filename, true, 0000)
	if err != nil {
		return authResult, err
	}

	return authResult, nil
}


Review comment (performance):
It's kinda annoying that we will already have calculated the same `fileHash` indirectly above by calling `meta.MatchesAnyHash` but the result is discarded before that function returns. :confounded: 

Not a huge deal -- the OS filesystem cache will probably save us most of the time -- but maybe one day (not in this PR) we can find a better way to structure this so that we don't end up needing to repeat this work.


Code after:
				"failed to calculate checksum for %s %s package at %s: %w",
				meta.Provider, meta.Version, meta.Location, err,
			)
		} else if !matches {
			return authResult, fmt.Errorf(
				"the current package for %s %s doesn't match any of the checksums previously recorded in the dependency lock file; for more information: https://opentofu.org/docs/language/files/dependency-lock/#checksum-verification",
				meta.Provider, meta.Version,
			)
		}
	}

	filename := meta.Location.String()

	// NOTE: Packages are immutable, but we may want to skip overwriting the existing
	// files in due to specific scenarios defined below.

	if _, err := os.Stat(targetDir); err == nil {
		// If the package might already be installed, we should try to skip overwriting the contents.
		// When run with TF_PLUGIN_CACHE_DIR or similar, a given provider might already be executing
		// and therefore locking the provider binary in the target directory (preventing the overwrite below)
		//
		// This does incur the overhead of two additional hash computations and could be
		// skipped with smarter checks around re-use scenarios in the future.

		targetHash, targetErr := PackageHashV1(PackageLocalDir(targetDir))
		fileHash, fileErr := PackageHashV1(meta.Location)

		if targetHash == fileHash && fileErr == nil && targetErr == nil {
			// Package is properly installed, bad or missing lock file will be caught elsewhere
			log.Printf("[INFO] Skipping local installation of provider %s %s as the existing contents already match the new contents", meta.Provider, meta.Version)
			return authResult, nil
		}
	}

	//nolint:mnd // magic number predates us using this linter
	err := unzip.Decompress(targetDir, filename, true, 0000)
	if err != nil {
		return authResult, err
	}

	return authResult, nil
}