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

terraform symlink is broken when a relative path is used for the --install parameter #553

Open
m0ps opened this issue Mar 3, 2025 · 4 comments

Comments

@m0ps
Copy link

m0ps commented Mar 3, 2025

tfswitch creates an incorrect symlink when both the --chdir and --install flags are provided, and the'-- bin' flag is used.

Actual behavior:

tfswitch --chdir ./testdir --install ./testdir --bin ./testdir/terraform
ls -la testdir/terraform
lrwxr-xr-x@ 1 m0ps  staff  51 Mar  3 10:46 testdir/terraform -> testdir/.terraform.versions/terraform_1.10.5

Expected behavior:

tfswitch --chdir ./testdir --install ./testdir --bin ./testdir/terraform
ls -la testdir/terraform
lrwxr-xr-x@ 1 m0ps  staff  51 Mar  3 10:46 testdir/terraform -> .terraform.versions/terraform_1.10.5

As a workaround, it's required to change the directory before the command execution (use cd ./tesdir instead of using --chdir ./testdir flag)

@yermulnik
Copy link
Collaborator

It's probably a poor naming of the chdir option that you tripped over 🤷🏻
The chdir isn't meant to actually change directory (despite it's, I'd agree, a bit ambiguous name) and it isn't meant to affect values of other command line flags. It is sole use is to read Terraform version constraints from expected sources inside the chdir directory. Its name is a reflection of the same command line option from Terraform, but still has nothing to do with actually changing directory.

I'm not sure what your use case is, though it looks a bit unexpected that you're cutting short tfswitch's capability to cache TF versions.
If the workaround, that you've found, works for you, I'd say it is the best way to go in this case.

@m0ps
Copy link
Author

m0ps commented Mar 5, 2025

After completing a few additional experiments, I've found that --chdir is not really related. In fact, the problem occurs if I try to use a relative (but not an absolute) path for --install flag. The following command produces a broken symlink:

tfswitch --install . --bin ../terraform
file ../terraform
../terraform: broken symbolic link to .terraform.versions/terraform_1.10.5

But this one works fine:

tfswitch --install $(pwd) --bin ../terraform
file ../terraform
../terraform: Mach-O 64-bit executable arm64

I do agree that the help contains the example of using the absolute path only:

 -i, --install=value
                    Custom install path. Ex: tfswitch -i /Users/username. The
                    binaries will be in the sub installDir directory e.g.
                    /Users/username/.terraform.versions

But still, it's uncommon for cli tools to accept absolute paths only, so it's better to update the help to highlight this limitation (or fix it).

P.S. I'll update the issue headline to avoid confusion

@m0ps m0ps changed the title terraform symlink broken with if both --chdir and --install flags are used terraform symlink broken if relative path is used for --install parameter Mar 5, 2025
@m0ps m0ps changed the title terraform symlink broken if relative path is used for --install parameter terraform symlink is broken when a relative path is used for the --install parameter Mar 5, 2025
@yermulnik
Copy link
Collaborator

But still, it's uncommon for cli tools to accept absolute paths only, so it's better to update the help to highlight this limitation (or fix it).

Would you be up to contribute? 🤔 We really appreciate contributions.

@m0ps
Copy link
Author

m0ps commented Mar 10, 2025

I wish I could... But my coding skills are pretty limited 🤷‍♂. Maybe it's time to learn a bit? WIll see if I can find a time soon. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants