From 786d99207eb1004f1f93fe562fdbfc226990f336 Mon Sep 17 00:00:00 2001 From: Alisdair McDiarmid Date: Mon, 29 Mar 2021 16:03:29 -0400 Subject: [PATCH] cli: Only rewrite provider locks file if changed If the provider locks have not changed, there is no need to rewrite the locks file. Preventing this needless rewrite should allow Terraform to operate in a read-only directory, so long as the provider requirements don't change. --- command/init.go | 11 +++-------- command/init_test.go | 7 +++++++ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/command/init.go b/command/init.go index 670a4ae72..f9b39fbda 100644 --- a/command/init.go +++ b/command/init.go @@ -798,16 +798,11 @@ Terraform has made some changes to the provider dependency selections recorded in the .terraform.lock.hcl file. Review those changes and commit them to your version control system if they represent changes you intended to make.`)) } + + moreDiags = c.replaceLockedDependencies(newLocks) + diags = diags.Append(moreDiags) } - // TODO: Check whether newLocks is different from previousLocks and mention - // in the UI if so. We should emit a different message if previousLocks was - // empty, because that indicates we were creating a lock file for the first - // time and so we need to introduce the user to the idea of it. - - moreDiags = c.replaceLockedDependencies(newLocks) - diags = diags.Append(moreDiags) - return true, false, diags } diff --git a/command/init_test.go b/command/init_test.go index f55cc2335..c8aabaa90 100644 --- a/command/init_test.go +++ b/command/init_test.go @@ -1618,6 +1618,13 @@ provider "registry.terraform.io/hashicorp/test" { if diff := cmp.Diff(wantLockFile, string(buf)); diff != "" { t.Errorf("wrong dependency lock file contents\n%s", diff) } + + // Make the local directory read-only, and verify that rerunning init + // succeeds, to ensure that we don't try to rewrite an unchanged lock file + os.Chmod(".", 0555) + if code := c.Run(args); code != 0 { + t.Fatalf("bad: \n%s", ui.ErrorWriter.String()) + } } func TestInit_providerLockFileReadonly(t *testing.T) {