Skip to content

Commit 70519b6

Browse files
committed
fix(terraform_validate): Add better workaround for deal with parallelism race conditions in Terraform setups during t init: allow use of tofu binary just for t init, as OpenTofu 1.10+ have native lock mechanism
1 parent d0e12ca commit 70519b6

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,15 @@ To replicate functionality in `terraform_docs` hook:
970970
### terraform_validate
971971

972972
> [!IMPORTANT]
973-
> If you use [`TF_PLUGIN_CACHE_DIR`](https://developer.hashicorp.com/terraform/cli/config/config-file#provider-plugin-cache), we recommend enabling `--hook-config=--retry-once-with-cleanup=true` or disabling parallelism (`--hook-config=--parallelism-limit=1`) to avoid [race conditions when `terraform init` writes to it](https://github.com/hashicorp/terraform/issues/31964).
973+
> If you use both:
974+
>
975+
> 1. Global Provider Cache. [Terraform docs](https://developer.hashicorp.com/terraform/cli/config/config-file#provider-plugin-cache), [OpenTofu docs](https://opentofu.org/docs/cli/config/config-file/#provider-plugin-cache)
976+
> 2.`tofu` < v1.10 or any `terraform` version
977+
>
978+
> We recommend to do one of:
979+
>
980+
> * Disable parallelism (`--hook-config=--parallelism-limit=1`) as it can cause [race conditions when `terraform init` writes to it](https://github.com/hashicorp/terraform/issues/31964).
981+
> * Install [OpenTofu v1.10+](https://opentofu.org/blog/help-us-test-opentofu-1-10-0-alpha2/#global-provider-cache-locking) even if you use Terraform - it will be used just for `t init` operations.
974982

975983
1. `terraform_validate` supports custom arguments so you can pass supported `-no-color` or `-json` flags:
976984

hooks/_common.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,15 @@ function common::terraform_init {
555555
init_output=$("$tf_path" init -backend=false "${TF_INIT_ARGS[@]}" 2>&1)
556556
exit_code=$?
557557
else
558+
# The global provider cache is safe for concurrent use by multiple processes for OpenTofu v1.10+
559+
# More details - https://github.com/opentofu/opentofu/pull/1878
560+
# For that reason, we switch to `tofu init` when possible
561+
if [[ "$($tf_path -version | head -1 | grep '^Terraform')" == "Terraform" ]] &&
562+
common::tofu_version_ge_1.10; then
563+
tf_path=$(command -v tofu)
564+
common::colorify "green" "Using OpenTofu binary ($tf_path) for Terraform init operations, as it supports concurrent provider initialization."
565+
fi
566+
558567
# Locking just doesn't work, and the below works quicker instead. Details:
559568
# https://github.com/hashicorp/terraform/issues/31964#issuecomment-1939869453
560569
for i in {1..10}; do
@@ -609,6 +618,37 @@ function common::export_provided_env_vars {
609618
done
610619
}
611620

621+
#######################################################################
622+
# Check if the installed Tofu version is >=1.10.0 or not
623+
#
624+
# This function helps to determine if tofu version allow to use
625+
# parallelism based on Tofu version
626+
#
627+
# Returns:
628+
# - 0 if version >= 1.10.0
629+
# - 1 if version < 1.10.0
630+
# Defaults to 1 if version cannot be determined
631+
#######################################################################
632+
# TODO: Drop after Jun 2027. Two years to upgrade is more than enough.
633+
function common::tofu_version_ge_1.10 {
634+
local tofu_version
635+
636+
# Extract version number (e.g., "tofu version v1.10.4" -> "1.10")
637+
tofu_version=$(tofu --version 2> /dev/null | grep -oE '[0-9]+\.[0-9]+')
638+
# If we can't parse version, default to older command
639+
[[ ! $tofu_version ]] && return 1
640+
641+
local major minor
642+
IFS='.' read -r major minor <<< "$tofu_version"
643+
644+
# New functional added in v1.10.0 (June 2025)
645+
if [[ $major -gt 1 || ($major -eq 1 && $minor -ge 10) ]]; then
646+
return 0
647+
else
648+
return 1
649+
fi
650+
}
651+
612652
#######################################################################
613653
# Check if the installed Terragrunt version is >=0.78.0 or not
614654
#

0 commit comments

Comments
 (0)