Skip to content

terraform init hashes a cached provider package three times when linking it from the plugin cache #39252

Open

Description

@Dutchy-

I was annoyed with how slow terraform init is, so I went looking for bugs in the provider installer. This is one I found, AI-assisted.

When a provider is linked from the global plugin cache and the lock file has checksums for it, the installer reads and hashes the whole cached package three times. I have a small fix to internal/providercache/installer.go that hashes it at most once and reuses the result. It leaves the lock file checks themselves (#32129) unchanged. For the configuration below it takes terraform init from 1.28s to 0.50s wall time and from 0.99s to 0.35s CPU time (median of 8 runs, Apple silicon Mac). I'd like to open a PR for it if that's welcome.

Terraform Version

Terraform v1.16.3
on darwin_arm64

Also reproduces on main (78194be).

Terraform Configuration Files

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "6.44.0"
    }
  }
}

Debug Output

Not attached. The trace log has no lines for the hashing itself, only a gap before providercache.Dir.LinkFromOtherCache: linking registry.terraform.io/hashicorp/aws.

Expected Behavior

The cached package is read and hashed once.

Actual Behavior

The same package is hashed three times during EnsureProviderVersions, and each pass reads the whole package (770MB for hashicorp/aws 6.44.0):

  1. installer.go:426: cached.MatchesAnyHash(preferredHashes) checks the cache entry against the lock file.
  2. dir_modify.go:66: LinkFromOtherCache checks the same hashes again before linking.
  3. installer.go:540: cached.Hash() computes the hash again for the lock file update.

Steps to Reproduce

  1. Save the configuration above as main.tf, with a plugin cache enabled: export TF_PLUGIN_CACHE_DIR=$HOME/.terraform.d/plugin-cache.
  2. terraform providers lock -platform=darwin_arm64 -platform=linux_amd64
  3. terraform init once, to fill the plugin cache.
  4. Copy main.tf and .terraform.lock.hcl into an empty directory and run time terraform init there.

Additional Context

It only happens with the global plugin cache enabled and a lock file that has checksums for the provider. Without a lock file entry, the cache entry is only used with plugin_cache_may_break_dependency_lock_file set, and is then hashed once.

Generative AI / LLM assisted development?

Claude Code (Claude Opus 5). I used it to find the problem and to write this issue text.

Activity

  1. crw commented on Sep 21, 2026

    @crw
    Contributor

    Thanks for this feature request! If you are viewing this issue and would like to indicate your interest, please use the 👍 reaction on the issue description to upvote this issue. We also welcome additional use case descriptions. Thanks again!

  2. Dutchy- commented on Oct 21, 2026

    @Dutchy-
    ContributorAuthor

    Is there anything I can do to move this forward?

  3. crw commented on Oct 21, 2026

    @crw
    Contributor

    For now, generating interest and use cases here will help promote and eventually prioritize the issue. I will also check in with the maintainers to see if this was being solved with other related changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions