You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Implement the tfenv install command for the Go edition with full parity to the Bash edition, including all version specifiers and concurrent install locking.
Parent Epic
Part of #488 — Go Edition: Full Feature Parity Implementation
Motivation
tfenv install is the primary command users interact with. It must resolve version specifiers, download and verify binaries, and install them to the versions directory. It must handle concurrent installs safely (e.g., multiple Terragrunt workers calling tfenv install simultaneously).
Clean-Room Constraint
This is a clean-room implementation. Contributors MUST NOT read, reference, copy, or adapt source code from tofuutils/tenv, hashicorp/hc-install, or any other third-party tfenv-like tool. The sole reference is tfenv's own Bash source code, documentation, and test suite.
When called without arguments, reads .terraform-version or default version file.
Install Flow
Validate argument count (0 or 1 args only — [ "${#}" -gt 1 ] is an error)
Resolve version specifier → version:regex pair (using resolve-version logic)
Pre-release filtering: If TFENV_SKIP_REMOTE_CHECK is not set, fetch remote versions. For latest with numeric-only regex, exclude pre-release versions (containing -). For regex containing letters (e.g., rc, alpha), include pre-releases.
Check if version already installed (${TFENV_CONFIG_DIR}/versions/${version}/terraform exists) — this check happens BEFORE any network calls for exact versions
If already installed, print Terraform v${version} is already installed and exit 0
Acquire per-version install lock
Re-check if installed (another process may have completed while we waited for lock)
Uses mkdir as an atomic lock: mkdir "${TFENV_CONFIG_DIR}/.install-lock-${version}"
If lock exists, wait 1 second and retry (up to 60 retries)
On first wait, print info: Another process is installing Terraform v${version}. Waiting...
After timeout (60s), warn about stale lock, remove it, and retry once
Check if version appeared while waiting (another process finished)
Lock removed on normal exit, error, INT, and TERM signals via trap
Go edition should use the same mkdir approach for cross-platform atomic locking (works on Linux, macOS, and Windows/MSYS). flock/fcntl is not portable to Windows.
defer for cleanup ensures lock is released on all exit paths
Stale lock detection: if lock is older than N minutes (e.g., 10), consider it stale
Auto-Install
TFENV_AUTO_INSTALL=true (default) triggers install when the terraform shim is invoked and the resolved version is not installed. This is handled by the exec/shim layer but the install command must support being called programmatically (not just via CLI).
Acceptance Criteria
tfenv install 1.5.0 installs Terraform 1.5.0
tfenv install latest installs the latest stable version
tfenv install latest:^1.5 installs the latest 1.5.x version
tfenv install latest-allowed installs based on HCL constraints
tfenv install min-required installs the minimum satisfying version
tfenv install (no args) reads .terraform-version or default version file
Already-installed versions print a message and exit 0 (no re-download)
Concurrent installs of the same version are serialized via locking
Concurrent installs of different versions proceed in parallel
Lock is released on success, failure, and process termination
Stale locks (from crashed processes) are detected and cleaned up
Exit code 0 on success, non-zero on failure
Clear error messages for: version not found, network failure, verification failure, disk full
Acceptance tests cover all version specifiers (using mock server)
Acceptance test covers concurrent install scenario
Summary
Implement the
tfenv installcommand for the Go edition with full parity to the Bash edition, including all version specifiers and concurrent install locking.Parent Epic
Part of #488 — Go Edition: Full Feature Parity Implementation
Motivation
tfenv installis the primary command users interact with. It must resolve version specifiers, download and verify binaries, and install them to the versions directory. It must handle concurrent installs safely (e.g., multiple Terragrunt workers callingtfenv installsimultaneously).Clean-Room Constraint
This is a clean-room implementation. Contributors MUST NOT read, reference, copy, or adapt source code from
tofuutils/tenv,hashicorp/hc-install, or any other third-party tfenv-like tool. The sole reference is tfenv's own Bash source code, documentation, and test suite.Proposed Design
Command Interface
When called without arguments, reads
.terraform-versionor default version file.Install Flow
[ "${#}" -gt 1 ]is an error)version:regexpair (using resolve-version logic)TFENV_SKIP_REMOTE_CHECKis not set, fetch remote versions. Forlatestwith numeric-only regex, exclude pre-release versions (containing-). For regex containing letters (e.g.,rc,alpha), include pre-releases.${TFENV_CONFIG_DIR}/versions/${version}/terraformexists) — this check happens BEFORE any network calls for exact versionsTerraform v${version} is already installedand exit 0Installation of terraform v${version} successful. To make this your default version, run 'tfenv use ${version}'Concurrent Install Locking
When multiple processes try to install the same version simultaneously (common with Terragrunt
run-all):Bash edition mechanism (reference:
libexec/tfenv-installlines 100-125):mkdiras an atomic lock:mkdir "${TFENV_CONFIG_DIR}/.install-lock-${version}"Another process is installing Terraform v${version}. Waiting...Go edition should use the same
mkdirapproach for cross-platform atomic locking (works on Linux, macOS, and Windows/MSYS).flock/fcntlis not portable to Windows.${TFENV_CONFIG_DIR}/.install-lock-${version}deferfor cleanup ensures lock is released on all exit pathsAuto-Install
TFENV_AUTO_INSTALL=true(default) triggers install when the terraform shim is invoked and the resolved version is not installed. This is handled by the exec/shim layer but the install command must support being called programmatically (not just via CLI).Acceptance Criteria
tfenv install 1.5.0installs Terraform 1.5.0tfenv install latestinstalls the latest stable versiontfenv install latest:^1.5installs the latest 1.5.x versiontfenv install latest-allowedinstalls based on HCL constraintstfenv install min-requiredinstalls the minimum satisfying versiontfenv install(no args) reads.terraform-versionor default version fileDependencies
Implementation Notes
libexec/tfenv-install— 350 lines including standalone boilerplate. The actual logic is lines 65-350.tfenv-resolve-versionmkdirfor locking (not flock) — the Go edition should use the same approach for portabilitytfenv installfails if Terraform is already installed, but Hashicorp's CDN is unavailable #395 (install fails if already installed but CDN unavailable) — the Go edition should check local first, before any network callsLabels
type:feature,priority:high,complexity:medium,category:install