Skip to content

feat: warn when .terraform.lock.hcl is modified during terraform init #18280

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

blink-so[bot]
Copy link
Contributor

@blink-so blink-so bot commented Jun 9, 2025

Fixes #18237

This PR adds diff generation for .terraform.lock.hcl files before and after running terraform init to detect when provider hashes are missing for the target architecture.

Problem

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 architecture. This causes Terraform to download providers unnecessarily during provisioning, slowing down the process.

Solution

  • Read .terraform.lock.hcl content before running terraform init (stored in memory)
  • Read content again after terraform init completes
  • If content differs, generate and log a diff with actionable guidance
  • Info message appears in debug stream for visibility

Changes

  • Added getTerraformLockFilePath() helper function
  • Added generateFileDiff() helper function for byte array comparison
  • Modified init() function to perform content comparison
  • Added comprehensive unit tests
  • Info message guides users to official HashiCorp documentation

Testing

  • Unit tests verify diff generation and file path functions
  • Code compiles successfully
  • Info message only appears when lock file is actually modified

The info message provides neutral guidance and links to official documentation about lock file changes.

Addresses Feedback from #18276

This implementation incorporates feedback from @johnstcn:

  1. Stores content in memory instead of writing temporary files to disk
  2. Generates actual diff output instead of just checksum comparison
  3. Uses neutral INFO-level messaging instead of warnings
  4. Links to official HashiCorp documentation for authoritative guidance

blink-so bot and others added 2 commits June 9, 2025 11:51
Fixes #18237

This PR adds diff generation for .terraform.lock.hcl files before and after running terraform init to detect when provider hashes are missing for the target architecture.

## Problem
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 architecture. This causes Terraform to download providers unnecessarily during provisioning, slowing down the process.

## Solution
- Read .terraform.lock.hcl content before running terraform init (stored in memory)
- Read content again after terraform init completes
- If content differs, generate and log a diff with actionable guidance
- Info message appears in debug stream for visibility

## Changes
- Added getTerraformLockFilePath() helper function
- Added generateFileDiff() helper function for byte array comparison
- Modified init() function to perform content comparison
- Added comprehensive unit tests
- Info message guides users to official HashiCorp documentation

## Testing
- Unit tests verify diff generation and file path functions
- Code compiles successfully
- Info message only appears when lock file is actually modified

The info message provides neutral guidance and links to official documentation about lock file changes.

Co-authored-by: kylecarbs <7122116+kylecarbs@users.noreply.github.com>
Fixes compilation error in executor_internal_test.go where filepath was undefined.

Co-authored-by: kylecarbs <7122116+kylecarbs@users.noreply.github.com>
Comment on lines 295 to 334
func generateFileDiff(beforeContent, afterContent []byte) string {
if bytes.Equal(beforeContent, afterContent) {
return ""
}

// Simple line-by-line diff
beforeLines := strings.Split(string(beforeContent), "\n")
afterLines := strings.Split(string(afterContent), "\n")

var diff strings.Builder
diff.WriteString("--- .terraform.lock.hcl (before terraform init)\n")
diff.WriteString("+++ .terraform.lock.hcl (after terraform init)\n")

// Simple diff showing added/removed lines
beforeMap := make(map[string]bool)
for _, line := range beforeLines {
beforeMap[line] = true
}

afterMap := make(map[string]bool)
for _, line := range afterLines {
afterMap[line] = true
}

// Show removed lines
for _, line := range beforeLines {
if !afterMap[line] && strings.TrimSpace(line) != "" {
diff.WriteString("- " + line + "\n")
}
}

// Show added lines
for _, line := range afterLines {
if !beforeMap[line] && strings.TrimSpace(line) != "" {
diff.WriteString("+ " + line + "\n")
}
}

return diff.String()
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See https://pkg.go.dev/github.com/google/go-cmp/cmp#Diff
We already use this and its output is very readable!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent suggestion! Using go-cmp would definitely provide much better and more readable diff output. I'll switch to using cmp.Diff instead of the custom diff implementation.

Comment on lines 255 to 258
infoMsg := "INFO: .terraform.lock.hcl was modified during 'terraform init'. " +
"This is normal when Terraform downloads providers or updates dependencies. " +
"See https://developer.hashicorp.com/terraform/language/files/dependency-lock#understanding-lock-file-changes " +
"for more information about lock file changes."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would argue that this is better left at WARN. If the lockfile is being modified, this potentially means that Coder is needlessly downloading providers from the internet instead of using cached versions, which means longer workspace build times.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great point! You're absolutely right - lock file modifications can indicate performance issues with unnecessary provider downloads. I'll change it back to WARN level.

- Change from INFO to WARN level for better visibility of performance issues
- Use go-cmp for better, more readable diff output instead of custom implementation
- Write warning to error stream for better visibility
- Update messaging to emphasize potential performance impact
- Remove custom generateFileDiff function and related tests

Addresses feedback from @johnstcn in PR review.

Co-authored-by: kylecarbs <7122116+kylecarbs@users.noreply.github.com>
@kylecarbs kylecarbs closed this Jun 9, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Jun 9, 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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy