@@ -15,6 +15,7 @@ import (
15
15
"sync"
16
16
"time"
17
17
18
+ "github.com/google/go-cmp/cmp"
18
19
"github.com/hashicorp/go-version"
19
20
tfjson"github.com/hashicorp/terraform-json"
20
21
"go.opentelemetry.io/otel/attribute"
@@ -249,21 +250,22 @@ func (e *executor) init(ctx, killCtx context.Context, logr logSink) error {
249
250
250
251
// Check if .terraform.lock.hcl was modified after terraform init
251
252
postInitLockFileContent ,_ := os .ReadFile (lockFilePath )
252
- diff := generateFileDiff (preInitLockFileContent ,postInitLockFileContent )
253
- if diff != "" {
254
- // Log informational message about lock file changes with diff
255
- infoMsg := "INFO: .terraform.lock.hcl was modified during 'terraform init'. " +
256
- "This is normal when Terraform downloads providers or updates dependencies. " +
253
+ if ! bytes .Equal (preInitLockFileContent ,postInitLockFileContent ) {
254
+ diff := cmp .Diff (string (preInitLockFileContent ),string (postInitLockFileContent ))
255
+ // Log warning message about lock file changes with diff
256
+ warnMsg := "WARN: .terraform.lock.hcl was modified during 'terraform init'. " +
257
+ "This may indicate that provider hashes are missing for your target architecture, " +
258
+ "which can cause unnecessary provider downloads and slower workspace builds. " +
257
259
"See https://developer.hashicorp.com/terraform/language/files/dependency-lock#understanding-lock-file-changes " +
258
260
"for more information about lock file changes."
259
261
260
- // Writeinfo message todebug stream
261
- if outWriter != nil {
262
- _ ,_ = outWriter .Write ([]byte (infoMsg + "\n " ))
263
- _ ,_ = outWriter .Write ([]byte ("\n Lock file changes:\n " + diff + "\n " ))
262
+ // Writewarning message toerror stream for visibility
263
+ if errWriter != nil {
264
+ _ ,_ = errWriter .Write ([]byte (warnMsg + "\n " ))
265
+ _ ,_ = errWriter .Write ([]byte ("\n Lock file changes:\n " + diff + "\n " ))
264
266
}
265
267
266
- e .logger .Info (ctx ,"terraform lock file modified during init" ,
268
+ e .logger .Warn (ctx ,"terraform lock file modified during init" ,
267
269
slog .F ("lock_file_path" ,lockFilePath ),
268
270
slog .F ("diff" ,diff ),
269
271
)
@@ -290,48 +292,7 @@ func getTerraformLockFilePath(workdir string) string {
290
292
return filepath .Join (workdir ,".terraform.lock.hcl" )
291
293
}
292
294
293
- // generateFileDiff generates a simple diff between two file contents.
294
- // Returns empty string if files are identical.
295
- func generateFileDiff (beforeContent ,afterContent []byte )string {
296
- if bytes .Equal (beforeContent ,afterContent ) {
297
- return ""
298
- }
299
-
300
- // Simple line-by-line diff
301
- beforeLines := strings .Split (string (beforeContent ),"\n " )
302
- afterLines := strings .Split (string (afterContent ),"\n " )
303
-
304
- var diff strings.Builder
305
- diff .WriteString ("--- .terraform.lock.hcl (before terraform init)\n " )
306
- diff .WriteString ("+++ .terraform.lock.hcl (after terraform init)\n " )
307
-
308
- // Simple diff showing added/removed lines
309
- beforeMap := make (map [string ]bool )
310
- for _ ,line := range beforeLines {
311
- beforeMap [line ]= true
312
- }
313
-
314
- afterMap := make (map [string ]bool )
315
- for _ ,line := range afterLines {
316
- afterMap [line ]= true
317
- }
318
295
319
- // Show removed lines
320
- for _ ,line := range beforeLines {
321
- if ! afterMap [line ]&& strings .TrimSpace (line )!= "" {
322
- diff .WriteString ("- " + line + "\n " )
323
- }
324
- }
325
-
326
- // Show added lines
327
- for _ ,line := range afterLines {
328
- if ! beforeMap [line ]&& strings .TrimSpace (line )!= "" {
329
- diff .WriteString ("+ " + line + "\n " )
330
- }
331
- }
332
-
333
- return diff .String ()
334
- }
335
296
336
297
// revive:disable-next-line:flag-parameter
337
298
func (e * executor )plan (ctx ,killCtx context.Context ,env ,vars []string ,logr logSink ,metadata * proto.Metadata ) (* proto.PlanComplete ,error ) {