- Notifications
You must be signed in to change notification settings - Fork1.1k
feat(provisioner): warn when .terraform.lock.hcl is modified during init#20451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Fixes#18237Implements the proposed solution from the issue:When users run `terraform init` locally on a different OS/architecturethan their Coder instance, the generated .terraform.lock.hcl file maybe missing provider hashes for the target platform. This causes Terraformto modify the lock file during provisioning and download providers fromthe internet on every workspace build instead of using the shared cache,significantly slowing down provisioning.## ImplementationThis change implements the proposed solution:1. **Record the checksum** of .terraform.lock.hcl before running terraform init2. **Compare checksums** after terraform init completes3. **Warn the user** if the checksum changes, with actionable guidance## Changes- Added `checksumFile()` function to calculate SHA256 checksums- Added `getTerraformLockFilePath()` helper function- Modified `init()` to: - Calculate lock file checksum before running terraform init - Calculate lock file checksum after terraform init - Call `warnLockFileModified()` if checksums differ- Created `warnLockFileModified()` that: - Logs a warning for observability - Displays a prominent, actionable warning to users with: - Clear explanation of the problem and its impact - Multi-platform solution (linux_amd64, linux_arm64, darwin_amd64, darwin_arm64, windows_amd64) - Single-platform quick fix option - Link to official documentation- Added comprehensive tests: - `TestChecksumFile`: validates checksum calculation and change detection - `TestGetTerraformLockFilePath`: validates path construction - `TestWarnLockFileModified`: validates warning message content## Advantages Over Alternative ApproachesThis checksum-based approach (as proposed in the issue):- ✅ Detects ANY lock file modification, not just specific messages- ✅ Works regardless of Terraform version or output format changes- ✅ Simple, reliable implementation- ✅ No parsing of Terraform output required- ✅ Catches edge cases where Terraform modifies lock file without the expected messageThe warning message provides both:- Multi-platform command for teams with mixed OS/arch- Single-platform command for quick fix targeting just Coder's platform
Fixes#18237When users run `terraform init` locally on a different OS/architecturethan their Coder instance, the generated .terraform.lock.hcl file maybe missing provider hashes for the target platform. This causes Terraformto download providers from the internet on every workspace build insteadof using the shared cache, significantly slowing down provisioning.## SolutionThis change detects when the lock file is modified during `terraform init`by comparing CRC32 checksums before and after. When a change is detected,a concise warning is displayed with the exact command to fix the issue.## Implementation- Added `checksumFileCRC32()` function to calculate CRC32 checksums - Uses CRC32 instead of SHA256 for speed (lock files are small) - Returns 0 if file doesn't exist or can't be read- Modified `init()` to: - Calculate lock file checksum before running terraform init - Calculate lock file checksum after terraform init - Call `warnLockFileModified()` if checksums differ- Created `warnLockFileModified()` that: - Logs a warning for observability - Displays a concise WARN message with: - Brief explanation of the problem - Exact command to fix with multiple platform targets- Added tests: - `TestChecksumFileCRC32`: validates checksum calculation and change detection - `TestWarnLockFileModified`: validates warning message content## Example Warning```WARN: .terraform.lock.hcl was modified during init. This means provider hashesare missing for the current platform (linux_amd64). Update the lock file with: terraform providers lock -platform=linux_amd64 -platform=linux_arm64 -platform=darwin_amd64 -platform=darwin_arm64 -platform=windows_amd64```## Advantages- ✅ **Detects any lock file modification** - not dependent on Terraform output format- ✅ **Fast CRC32 checksum** - optimized for small files- ✅ **Concise warning** - gets straight to the point- ✅ **Copy-paste ready command** - users know exactly what to run- ✅ **Multi-platform by default** - prevents future issues
The checksumFileCRC32 function signature was updated to include context.Contextand slog.Logger parameters. Updated the test to pass these required arguments.
mtojek commentedOct 24, 2025
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
- Use testutil.Context with WaitShort timeout instead of context.Background- Replace slog.Make() with testutil.Logger(t)- Use hardcoded expected checksum (0x08f39f51) instead of calling checksumFileCRC32 twice for comparison- Move context and logger creation into each subtest
johnstcn approved these changesOct 24, 2025
40e1784 intomain 32 checks passed
Uh oh!
There was an error while loading.Please reload this page.
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes#18237
This PR detects when
.terraform.lock.hclis modified duringterraform initand warns users with a concise message and the exact command to fix the issue.Problem
When users run
terraform initlocally on a different OS/architecture than their Coder instance, the generated lock file may be missing provider hashes for the target platform. This causes Terraform to download providers from the internet on every workspace build instead of using the shared cache, significantly slowing down provisioning.Solution
This change uses CRC32 checksums to detect lock file modifications and displays a concise warning with the fix command.
Example Warning
Changes
checksumFileCRC32()- uses CRC32 for fast checksum calculationinit()- compares checksums before/after terraform initwarnLockFileModified()- displays concise warning with fix commandTesting
Advantages
Notes
This is a simpler approach compared to the previous attempt in PR#18280:
cmp.Diff