Skip to content

Conversation

@bentsku
Copy link
Contributor

@bentsku bentsku commented Jul 3, 2025

This PR now returns the version of terraform-local when running tflocal -v (or --version, -version, all accepted by Terraform).

This was sometimes an issue when users had a global installation of tflocal and one in a virtual environment, and only updated one of them. It was hard to know which version you'd be calling/using.

We are printing to stderr to avoid breaking existing user setup maybe relying on tflocal --version returning the underlying Terraform version unmodified.

It is not trivial to get the version without directly parsing the setup.cfg file, or changing the setup itself to create a __version__ variable somewhere in the code to be imported.

Relying on from importlib.metadata import version seems safe, but I'm not sure if it could import a different version than the one that is being called right now... 🤔 I doubt it
This seems to be the preferred way: https://packaging.python.org/en/latest/discussions/versioning/#runtime-version-access

@bentsku bentsku self-assigned this Jul 3, 2025
@bentsku bentsku marked this pull request as ready for review July 3, 2025 17:37
@bentsku bentsku requested a review from cloutierMat July 3, 2025 17:37

## Change Log

* v0.24.0: Add support to return `terraform-local` version when calling `tflocal -version` and fix AWS provider detection
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this doesn't sound clear... lmk if you find something better, I'd happily take any suggestion 😅

Copy link
Member

@cloutierMat cloutierMat left a comment

Choose a reason for hiding this comment

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

Awesome! Thanks for taking this initiative to solve this pain point and your diligence to not break users possible flows in the process. more information is always better!

I only left a question regarding the test. It seems that there might be an extra copy paste comment, unless I am missing something 😄


## Change Log

* v0.24.0: Add support to return `terraform-local` version when calling `tflocal -version` and fix AWS provider detection
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for adding the previous pr changelog as well! 🙏

Comment on lines +643 to +645
def get_tf_local_version():
from importlib.metadata import version
return version("terraform-local")
Copy link
Member

Choose a reason for hiding this comment

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

I agree that this seems safe. It will find the version that is currently available in the venv, so we won't have issues with multiple install of tflocal.

I have tested install with pipx, with pip in a venv and my hybrid dev env, and all seem to give proper result. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

awesome, thanks a lot for taking the time to test extra scenarios 🙏

Comment on lines +745 to +746
except Exception:
pass
Copy link
Member

Choose a reason for hiding this comment

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

🎉 Hurray for extra safety here! 🚀

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks! made me think I'll update those to use contextlib.suppress, but this will be for a follow up 👌

return subprocess.check_output(cmd, **kwargs)

with tempfile.TemporaryDirectory(delete=True) as temp_dir:
# we need the `terraform init` command to create a lock file, so it cannot be a `DRY_RUN`
Copy link
Member

Choose a reason for hiding this comment

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

Is that a forgotten copy paste? I fail to see where the init or the dry run would impact the version command 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it is indeed, my bad 😅 will update

@bentsku bentsku merged commit 72f5a2a into main Jul 4, 2025
3 checks passed
@bentsku bentsku deleted the print-version branch July 4, 2025 09:10
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

Successfully merging this pull request may close these issues.

3 participants