tfenv overwrites .terraform-version file when installing terraform

Created on 12 Aug 2019  ·  7Comments  ·  Source: tfutils/tfenv

Problem

I use the .terraform-version file to select which version(s) of Terraform should be used for a specific directory tree. I regularly use the regex method for the contents of .terraform-version. When necessary, I'll run tfenv install to get the appropriate Terraform version on my system should it not be present. However, in doing so, tfenv overwrites my .terraform-version file with the exact version number that was installed.

Steps to reproduce

$ echo "latest:^0.12" > .terraform-version
$ tfenv install
....
$ terraform version
Terraform v0.12.6
$ cat .terraform-version
0.12.6

Expected behavior

I would expect the contents of .terraform-version to remain the same unless I specifically change it.

Setup

  • OS: MacOS Mojave (10.14.6)
  • Shell: Bash 5.0.7
  • TFEnv version: 1.0.1
bug

Most helpful comment

@e-nomem that seems to be the issue indeed. Given how I use tfenv I think a case can be made to never call tfenv-use at the end of tfenv-install. Just because I install a version of Terraform, doesn't mean I want to use it right now, or have it as my global default version.

However, it would be nice if you could configure this behaviour. For instance:

## Default, use version file
$ tfenv install
(just installs version from file, no tfenv-use required)

$ tfenv install 0.12.6
(just installs, but when detecting an interactive shell, it asks what you want)
Do you want to set Terraform 0.12.6 as your global default (y/n)? y
Updating global version in /Users/benny/.terraform-version... done

$ tfenv install 0.12.6 --global
(installs and sets the default)

All 7 comments

This is concerning. Will investigate asap

This seems to come from the call to tfenv-use at the end of tfenv-install, because tfenv-use overwrites the version file.
It seems to me that the call to tfenv-use is not necessary for install, because only the global version file should be updated, and only if the global version file does not already exist.

@e-nomem that seems to be the issue indeed. Given how I use tfenv I think a case can be made to never call tfenv-use at the end of tfenv-install. Just because I install a version of Terraform, doesn't mean I want to use it right now, or have it as my global default version.

However, it would be nice if you could configure this behaviour. For instance:

## Default, use version file
$ tfenv install
(just installs version from file, no tfenv-use required)

$ tfenv install 0.12.6
(just installs, but when detecting an interactive shell, it asks what you want)
Do you want to set Terraform 0.12.6 as your global default (y/n)? y
Updating global version in /Users/benny/.terraform-version... done

$ tfenv install 0.12.6 --global
(installs and sets the default)

I would definitely like a way to turn off the overwrite at least, even if it's a shell variable. We're rolling out a bunch of complicated upgrades from tf 0.11 to 0.12 and we want to track the latest 0.12 generally, but whenever someone runs tfenv install from outside CI, the .terraform-version gets updated with the specific version as in the example. I'm fine with not breaking existing the existing functionality of tfenv install if people really use the overwrite, but if we could turn it off by setting something like TFENV_NOOVERWRITE, I'd be happy with that.

Overwriting the project-specific .terraform-version file almost renders the use of the file useless, and also runs the risk of somebody unintentionally committing the change to source control. We use the file in a repo to _fix_ the version used for the project, so overwriting it with a tfenv command is rather problematic.

I agree that tfenv use should not modify the .terraform-version file.
Stopping it from doing this will likely confuse users who issue a tfenv, and then don't get the version they asked for because they have a version file.
I welcome thoughts on improving that user experience, but it's surely the right thing to stop the overwrite.

I'm not sure what sort of thoughts you're looking for, but in my opinion, tfenv should _never_ write to that file, even if it doesn't exist. It should treat that file as _read only_. It is up to the _users_ of tfenv to create that file, if they so desire, and then to commit it to their own repos if they want to use a fixed version of Terraform for their projects.

The tfenv install and tfenv use commands should look for the .terraform-version file when no version is explicitly specified on the command line. If the file exists, the commands should use it to determine which version to use with the command, and if it does not exist, the commands should fail, indicating that the user should either specify a version on the command line, or within a .terraform-version file. When a version is specified on the command line, the .terraform-version file should be ignored.

Was this page helpful?
0 / 5 - 0 ratings