-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Lock Files, Plugin Cache and using several architectures fails terraform init #29958
Description
So, since I am using several stacks of Terraform configuration quite regularly and their plugins in use are mostly the same, I didn't want providers to be downloaded for each one, so I enabled plugin cache directory (see below). Also, at some point in time I started using lock files instead of relying on using version = "= x.y" to always reuse the same version.
Since we use Terraform on several machines, and some developers are using different architectures (I'm using Linux Amd64, my colleague is using Darwin Amd64, more architectures might come with M1), I expect lock files to support being used on several architectures. Documentation states that even though h1s only for current architectures are populated, since zhs are always present they are enough to check either plugin for a new architecture already matches known hashes, so that Terraform can append new h1 hash on a first download. Sounds great, actually (or one can assume we can remember h1s directly, but it's not my expertise).
The only problem is that, somehow, Terraform always does a little different thing for me:
- When I run
terraform initthat resolved to a plugin version that is not in the cache, Terraform actually does it's job: it downloads the plugin, populates the lock file with allzhs and a singleh1. If I will use this lock file on a different machine everything works as expected. - When I run
terraform initfor the same plugin version in another directory, it is accessed from the cache, and only h1 hash is written into the lock file. - When my colleague will run
terraform initon the second stack with my lock file (if I commit it into VCS), they will get an error.
As I have found out, instead of trying to use different hardware I can emulate the behaviour by just running terraform providers lock -platform=darwin_amd64, where platform can be anything but not the current one.
Terraform Version
Terraform v1.0.11
on linux_amd64
+ provider registry.terraform.io/hashicorp/null v3.1.0
Also I've seen this on a lot of versions, since, I think, around 0.15.
Terraform Configuration Files
There is nothing relevant beyond version requirements:
terraform {
required_version = ">= 1.0.4"
required_providers {
null = {
source = "hashicorp/null"
version = "~> 3.1"
}
}
}Relevant ~/.terraformrc:
plugin_cache_dir = "$HOME/.cache/terraform/plugin-cache"Debug Output
Expected Behavior
terraform init should populate lock file well enough for future use or switch to redownloading plugin in case lock file doesn't provide enough information.
Actual Behavior
Terraform disrupts developer experience by forcing to do terraform providers lock -platform=… with every platform in use so that terraform init wouldn't fail in the future.
Steps to Reproduce
Here I assume that I'm running on linux_amd64, and I'm using darwin_amd64 and darwin_arm64 as different achitectures. However, I experienced this bug with other combinations, I don't think exact platforms are relevant here.
- Create
~/.terraformrcfile with line above, create given directory / check that it's empty. - Create an empty directory
awithversions.tffile as above. terraform initin it. Should work like a charm.- Open
.terraform.lock.hclto see that there are a lot of hash lines. terraform providers lock -platform=darwin_amd64. Works, lock file is updated with a newh1hash.- Create an empty direcory
bwith the sameversions.tffile. terraform initin it. Should work.- However,
terraform.lock.hclonly contains a singleh1hash. terraform providers lock -platform=darwin_arm64. Fails:╷ │ Error: Could not retrieve providers for locking │ │ Terraform failed to fetch the requested providers for darwin_arm64 in order │ to calculate their checksums: some providers could not be installed: │ - registry.terraform.io/hashicorp/null: the current package for │ registry.terraform.io/hashicorp/null 3.1.0 doesn't match any of the │ checksums previously recorded in the dependency lock file. ╵
Additional Context
I am sorry, this can be already discussed in another Cache+Lock-related issue, but I haven't seen exactly my case being described. I am opening this one with the exact reproducible case.
References
For a lot of time I was following #28041 for this problem, but it seems to me it has lost the direction I am interested in.