Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
mtojek merged 6 commits intomainfrom18237-terraform-lock-modified
Oct 24, 2025

Conversation

@mtojek
Copy link
Member

Summary

Fixes#18237

This PR detects when.terraform.lock.hcl is modified duringterraform init and warns users with a concise message and the exact command to fix the issue.

Problem

When users runterraform init locally 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

WARN: .terraform.lock.hcl was modified during init. This means provider hashes are 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

Changes

  • AddedchecksumFileCRC32() - uses CRC32 for fast checksum calculation
  • Modifiedinit() - compares checksums before/after terraform init
  • AddedwarnLockFileModified() - displays concise warning with fix command
  • Added tests for checksum calculation and warning message

Testing

  • ✅ All existing tests pass
  • ✅ New tests added for checksum functionality
  • ✅ Warning message validation test

Advantages

  • ✅ Fast CRC32 checksum (optimized for small files)
  • ✅ Concise 3-line warning message
  • ✅ Copy-paste ready command with multi-platform support
  • ✅ Independent of Terraform output format
  • ✅ Simple implementation (+45, -97 lines)

Notes

This is a simpler approach compared to the previous attempt in PR#18280:

  • Uses fast CRC32 instead of verbosecmp.Diff
  • Provides concise actionable warning instead of detailed diff
  • Focuses on telling users exactly what to do

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
mtojekand others added2 commitsOctober 24, 2025 10:08
The checksumFileCRC32 function signature was updated to include context.Contextand slog.Logger parameters. Updated the test to pass these required arguments.
- 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
@mtojekmtojek marked this pull request as ready for reviewOctober 24, 2025 08:32
@mtojekmtojek requested a review fromjohnstcnOctober 24, 2025 08:32
@mtojekmtojek merged commit40e1784 intomainOct 24, 2025
32 checks passed
@mtojekmtojek deleted the 18237-terraform-lock-modified branchOctober 24, 2025 08:44
@github-actionsgithub-actionsbot locked and limited conversation to collaboratorsOct 24, 2025
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.

Reviewers

@johnstcnjohnstcnjohnstcn approved these changes

Assignees

@mtojekmtojek

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Warn if your .terraform.lock.hcl is modified after runningterraform init

3 participants

@mtojek@johnstcn

[8]ページ先頭

©2009-2025 Movatter.jp