Skip to content
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

cli: Only rewrite provider locks file if changed #28230

Merged
merged 1 commit into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions command/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,15 +798,10 @@ 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.`))
}
}

// 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.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this TODO was out of date. Immediately above we already check if newLocks is different from previousLocks and output two different messages. The same applies to v0.14:

terraform/command/init.go

Lines 845 to 872 in 1c68b7b

if !newLocks.Equal(previousLocks) {
if previousLocks.Empty() {
// A change from empty to non-empty is special because it suggests
// we're running "terraform init" for the first time against a
// new configuration. In that case we'll take the opportunity to
// say a little about what the dependency lock file is, for new
// users or those who are upgrading from a previous Terraform
// version that didn't have dependency lock files.
c.Ui.Output(c.Colorize().Color(`
Terraform has created a lock file [bold].terraform.lock.hcl[reset] to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.`))
} else {
c.Ui.Output(c.Colorize().Color(`
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.`))
}
}
// 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)

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed. I'm pretty sure I just saw the TODO, TODID it and then forgot to remove the comment afterwards.


moreDiags = c.replaceLockedDependencies(newLocks)
diags = diags.Append(moreDiags)
moreDiags = c.replaceLockedDependencies(newLocks)
diags = diags.Append(moreDiags)
}

return true, false, diags
}
Expand Down
7 changes: 7 additions & 0 deletions command/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down