Skip to content

feat(provisioner): warn when .terraform.lock.hcl is modified during init#20451

Merged
mtojek merged 6 commits into
mainfrom
18237-terraform-lock-modified
Oct 24, 2025
Merged

feat(provisioner): warn when .terraform.lock.hcl is modified during init#20451
mtojek merged 6 commits into
mainfrom
18237-terraform-lock-modified

Conversation

@mtojek
Copy link
Copy Markdown
Member

@mtojek mtojek commented Oct 24, 2025

Summary

Fixes #18237

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

Problem

When users run terraform 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

  • Added checksumFileCRC32() - uses CRC32 for fast checksum calculation
  • Modified init() - compares checksums before/after terraform init
  • Added warnLockFileModified() - 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 verbose cmp.Diff
  • Provides concise actionable warning instead of detailed diff
  • Focuses on telling users exactly what to do

Fixes #18237

Implements the proposed solution from the issue:

When users run `terraform init` locally on a different OS/architecture
than their Coder instance, the generated .terraform.lock.hcl file may
be missing provider hashes for the target platform. This causes Terraform
to modify the lock file during provisioning and download providers from
the internet on every workspace build instead of using the shared cache,
significantly slowing down provisioning.

## Implementation

This change implements the proposed solution:
1. **Record the checksum** of .terraform.lock.hcl before running terraform init
2. **Compare checksums** after terraform init completes
3. **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 Approaches

This 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 message

The 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 #18237

When users run `terraform init` locally on a different OS/architecture
than their Coder instance, the generated .terraform.lock.hcl 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 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 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
```

## 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
mtojek and others added 2 commits October 24, 2025 10:08
The checksumFileCRC32 function signature was updated to include context.Context
and slog.Logger parameters. Updated the test to pass these required arguments.
Comment thread provisioner/terraform/executor_internal_test.go Outdated
Comment thread provisioner/terraform/executor_internal_test.go Outdated
Comment thread provisioner/terraform/executor_internal_test.go Outdated
- 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
@mtojek mtojek marked this pull request as ready for review October 24, 2025 08:32
@mtojek mtojek requested a review from johnstcn October 24, 2025 08:32
@mtojek mtojek merged commit 40e1784 into main Oct 24, 2025
27 checks passed
@mtojek mtojek deleted the 18237-terraform-lock-modified branch October 24, 2025 08:44
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 24, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants