tofuutils / tenv

OpenTofu / Terraform / Terragrunt and Atmos version manager
https://tofuutils.github.io/tenv/
Apache License 2.0
693 stars 34 forks source link

update-path not working #105

Closed cacack closed 6 months ago

cacack commented 6 months ago

Describe the bug tenv update-path is not including tenv in the path output.

To Reproduce Steps to reproduce the behavior:

  1. tenv tf use 1.7.5
  2. tenv update-path

Expected behavior I'd expect the output of tenv update-path to include the path to Terraform so that executing terraform results in the correct binary being invoked.

Screenshots

❯ tenv version
tenv version v1.7.1

❯ tenv tf use 1.7.5
Terraform 1.7.5 already installed
Written 1.7.5 in /Users/xxxx/.tenv/Terraform/version

❯ tenv update-path |grep tenv

❯ which terraform
terraform not found

Environment (please complete the following information):

Additional context Ideally this is like other *env tools where I can "inject" the path programmatically. Obviously I could do something like to inject the path myself:

export PATH="/Users/xxxx/.tenv/Terraform/$(cat /Users/xxxx/.tenv/Terraform/version)/:$PATH"

However, that is not as clean as having the tool do this as ideally the tool should adjust pathing, for example if I change projects which have differing Terraform versions.

dvaumoron commented 6 months ago

How did you install tenv ?

which terraform should indicate the proxy version installed with tenv

export PATH="/Users/xxxx/.tenv/Terraform/$(cat /Users/xxxx/.tenv/Terraform/version)/:$PATH"

That's not the correct way to use tenv, you should add to your PATH the location of tenv project which include 5 binaries : tenv to manage the tools, and the proxies tofu, terraform, terragrunt and tf which are used to call the version of the tool corresponding to your configuration.

dvaumoron commented 6 months ago

Simple example of usage with tofu proxy :

$ cd tenv_test/
$ tofu version
OpenTofu v1.6.2
on linux_amd64
$ echo 1.6.0 > .opentofu-version
$ tofu version
Resolved version from .opentofu-version : 1.6.0
Installing OpenTofu 1.6.0
Fetching release information from https://api.github.com/repos/opentofu/opentofu/releases/tags/v1.6.0
Downloading https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_linux_amd64.zip
Downloading https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_SHA256SUMS
Downloading https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_SHA256SUMS.sig
Downloading https://github.com/opentofu/opentofu/releases/download/v1.6.0/tofu_1.6.0_SHA256SUMS.pem
Installation of OpenTofu 1.6.0 successful
OpenTofu v1.6.0
on linux_amd64
$ TOFUENV_TOFU_VERSION=1.6.1 tofu version
Resolved version from TOFUENV_TOFU_VERSION : 1.6.1
Installing OpenTofu 1.6.1
Fetching release information from https://api.github.com/repos/opentofu/opentofu/releases/tags/v1.6.1
Downloading https://github.com/opentofu/opentofu/releases/download/v1.6.1/tofu_1.6.1_linux_amd64.zip
Downloading https://github.com/opentofu/opentofu/releases/download/v1.6.1/tofu_1.6.1_SHA256SUMS
Downloading https://github.com/opentofu/opentofu/releases/download/v1.6.1/tofu_1.6.1_SHA256SUMS.sig
Downloading https://github.com/opentofu/opentofu/releases/download/v1.6.1/tofu_1.6.1_SHA256SUMS.pem
Installation of OpenTofu 1.6.1 successful
OpenTofu v1.6.1
on linux_amd64
cacack commented 6 months ago

I'm installing using binaries provided by the releases:

❯ curl -Ls -o tenv.tar.gz https://github.com/tofuutils/tenv/releases/download/v1.7.1/tenv_v1.7.1_Darwin_arm64.tar.gz
❯ tar -xvzf tenv.tar.gz -C /Users/xxxx/.local/bin tenv
x tenv
❯ rm tenv.tar.gz

❯ which tenv
/Users/xxxx/.local/bin/tenv

Installation using the release binaries allows parity between developer environments and our pipelines...

I'd assume my shell needs to be configured such that tenv is able to "present" the correct version of Terraform. Other *env tools I've used in the past use shims or other mechanisms, but based on the README, I read it as the tenv update-path was the way to do this. As it stands, nothing is manipulating my shell.

cacack commented 6 months ago

lol, and looking in the tarball I now see where I might have misstep.. 😵‍💫

❯ tar -tvzf tenv.tar.gz 
-rw-r--r--  0 runner docker      0 Apr  9 03:05 CHANGELOG.md
-rw-r--r--  0 runner docker  11349 Apr  9 03:05 LICENSE
-rw-r--r--  0 runner docker  46446 Apr  9 03:05 README.md
-rwxr-xr-x  0 runner docker 12683682 Apr  9 03:09 tenv
-rwxr-xr-x  0 runner docker  9101618 Apr  9 03:10 tofu
-rwxr-xr-x  0 runner docker  9084114 Apr  9 03:10 terraform
-rwxr-xr-x  0 runner docker  8677538 Apr  9 03:10 terragrunt
-rwxr-xr-x  0 runner docker  9119330 Apr  9 03:10 tf
cacack commented 6 months ago

I suspect, the terraform binary provided by the release is some sort of "redirect" to tenv and able to pick up the various versioning information it provides?

cacack commented 6 months ago

Yep, that's exactly what it's doing.. https://github.com/tofuutils/tenv/blob/main/cmd/terraform/terraform.go

dvaumoron commented 6 months ago

The tenv update-path have been requested as one feature among those making easier to use tenv with GitHub Actions.

More globally that command can help when the original tool appear in PATH before the corresponding proxy, when :

$ which tenv
/Users/xxxx/.local/bin/tenv

Then it should look like :

$ tenv update-path
/Users/xxxx/.local/bin:$PATH

So export PATH=$(tenv update-path) would place the tenv location first, ensuring the proxy is found before any corresponding tool.

However, in most case you doesn't need it (I don't think many system have one of the managed tool installed by default), when using tenv installing the tool in other way seem strange...

dvaumoron commented 6 months ago

Maybe we need to make that clearer in the README.

rdhar commented 6 months ago

While it's not exactly built-into to the system, some popular docker-oriented images like Alpine Linux includes Terraform.

Or at least they used, until Hashicorp's license change.