Skip to content

Commit a475d47

Browse files
committed
fix(hooks): check terragrunt syntax version against tool_path, not $PATH
common::terragrunt_version_ge_0.78 ran bare `terragrunt --version`, picking the CLI syntax (pre/post-0.78) based on whatever's on $PATH - unrelated to the resolved/pinned binary that --tool-version actually executes. A pinned version on the other side of the 0.78 boundary from $PATH (or nothing on $PATH at all) picks the wrong subcommand syntax and the hook fails. Give the function a required tool_path argument and resolve it early in each of the 4 terragrunt_*.sh hooks before the version-gate check, mirroring terraform_tflint.sh's existing early-resolution precedent. Assisted-by: Sisyphus:claude-sonnet-5 opencode
1 parent a432cff commit a475d47

5 files changed

Lines changed: 47 additions & 14 deletions

File tree

hooks/_common.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -880,22 +880,27 @@ function common::export_provided_env_vars {
880880
}
881881

882882
#######################################################################
883-
# Check if the installed Terragrunt version is >=0.78.0 or not
883+
# Check if the given Terragrunt binary's version is >=0.78.0 or not
884884
#
885885
# This function helps to determine which terragrunt subcomand to use
886886
# based on Terragrunt version
887887
#
888+
# Arguments:
889+
# tool_path (string) resolved path to the terragrunt binary to check
890+
# (the actually resolved/pinned binary, NOT whatever's on $PATH -
891+
# those can differ once --tool-version is in play)
888892
# Returns:
889893
# - 0 if version >= 0.78.0
890894
# - 1 if version < 0.78.0
891895
# Defaults to 0 if version cannot be determined
892896
#######################################################################
893897
# TODO: Drop after May 2027. Two years to upgrade is more than enough.
894898
function common::terragrunt_version_ge_0.78 {
899+
local -r tool_path="$1"
895900
local terragrunt_version
896901

897902
# Extract version number (e.g., "terragrunt version v0.80.4" -> "0.80")
898-
terragrunt_version=$(terragrunt --version 2> /dev/null | grep -oE '[0-9]+\.[0-9]+')
903+
terragrunt_version=$("$tool_path" --version 2> /dev/null | grep -oE '[0-9]+\.[0-9]+')
899904
# If we can't parse version, default to newer command
900905
[[ ! $terragrunt_version ]] && return 0
901906

hooks/terragrunt_fmt.sh

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,21 @@ function main {
1414
common::parse_and_export_env_vars
1515
# JFYI: `terragrunt hcl format` color already suppressed via PRE_COMMIT_COLOR=never
1616

17-
if common::terragrunt_version_ge_0.78; then
17+
local -r tool_name="terragrunt"
18+
# Needed early to pick the correct CLI syntax below against the actually
19+
# resolved/pinned terragrunt binary, not whatever's on $PATH.
20+
# common::per_dir_hook resolves it again for the per-dir runs.
21+
local -r tool_version=$(common::get_hook_config_value "--tool-version")
22+
local tool_path
23+
tool_path=$(common::resolve_tool_path "$tool_name" "$tool_version") || exit $?
24+
readonly tool_path
25+
26+
if common::terragrunt_version_ge_0.78 "$tool_path"; then
1827
local -ra SUBCOMMAND=(hcl format)
1928
else
2029
local -ra SUBCOMMAND=(hclfmt)
2130
fi
2231

23-
local -r tool_name="terragrunt"
24-
2532
# shellcheck disable=SC2153 # False positive
2633
common::per_dir_hook "$HOOK_ID" "$tool_name" "${#ARGS[@]}" "${ARGS[@]}" "${FILES[@]}"
2734
}

hooks/terragrunt_providers_lock.sh

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,23 @@ function main {
1414
common::parse_and_export_env_vars
1515
# JFYI: terragrunt providers lock color already suppressed via PRE_COMMIT_COLOR=never
1616

17-
if common::terragrunt_version_ge_0.78; then
17+
local -r tool_name="terragrunt"
18+
# Needed early to pick the correct CLI syntax below against the actually
19+
# resolved/pinned terragrunt binary, not whatever's on $PATH.
20+
# common::per_dir_hook resolves it again for the per-dir runs.
21+
local -r tool_version=$(common::get_hook_config_value "--tool-version")
22+
local tool_path
23+
tool_path=$(common::resolve_tool_path "$tool_name" "$tool_version") || exit $?
24+
readonly tool_path
25+
26+
if common::terragrunt_version_ge_0.78 "$tool_path"; then
1827
local -ra SUBCOMMAND=(run -- providers lock)
1928
local -ra RUN_ALL_SUBCOMMAND=(run --all -- providers lock)
2029
else
2130
local -ra SUBCOMMAND=(providers lock)
2231
local -ra RUN_ALL_SUBCOMMAND=(run-all providers lock)
2332
fi
2433

25-
local -r tool_name="terragrunt"
26-
2734
# shellcheck disable=SC2153 # False positive
2835
common::per_dir_hook "$HOOK_ID" "$tool_name" "${#ARGS[@]}" "${ARGS[@]}" "${FILES[@]}"
2936
}

hooks/terragrunt_validate.sh

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,23 @@ function main {
1414
common::parse_and_export_env_vars
1515
# JFYI: terragrunt validate color already suppressed via PRE_COMMIT_COLOR=never
1616

17-
if common::terragrunt_version_ge_0.78; then
17+
local -r tool_name="terragrunt"
18+
# Needed early to pick the correct CLI syntax below against the actually
19+
# resolved/pinned terragrunt binary, not whatever's on $PATH.
20+
# common::per_dir_hook resolves it again for the per-dir runs.
21+
local -r tool_version=$(common::get_hook_config_value "--tool-version")
22+
local tool_path
23+
tool_path=$(common::resolve_tool_path "$tool_name" "$tool_version") || exit $?
24+
readonly tool_path
25+
26+
if common::terragrunt_version_ge_0.78 "$tool_path"; then
1827
local -ra SUBCOMMAND=(run -- validate)
1928
local -ra RUN_ALL_SUBCOMMAND=(run --all -- validate)
2029
else
2130
local -ra SUBCOMMAND=(validate)
2231
local -ra RUN_ALL_SUBCOMMAND=(run-all validate)
2332
fi
2433

25-
local -r tool_name="terragrunt"
26-
2734
# shellcheck disable=SC2153 # False positive
2835
common::per_dir_hook "$HOOK_ID" "$tool_name" "${#ARGS[@]}" "${ARGS[@]}" "${FILES[@]}"
2936
}

hooks/terragrunt_validate_inputs.sh

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,23 @@ function main {
1414
common::parse_and_export_env_vars
1515
# JFYI: terragrunt validate color already suppressed via PRE_COMMIT_COLOR=never
1616

17-
if common::terragrunt_version_ge_0.78; then
17+
local -r tool_name="terragrunt"
18+
# Needed early to pick the correct CLI syntax below against the actually
19+
# resolved/pinned terragrunt binary, not whatever's on $PATH.
20+
# common::per_dir_hook resolves it again for the per-dir runs.
21+
local -r tool_version=$(common::get_hook_config_value "--tool-version")
22+
local tool_path
23+
tool_path=$(common::resolve_tool_path "$tool_name" "$tool_version") || exit $?
24+
readonly tool_path
25+
26+
if common::terragrunt_version_ge_0.78 "$tool_path"; then
1827
local -ra SUBCOMMAND=(hcl validate --inputs)
1928
local -ra RUN_ALL_SUBCOMMAND=(run --all hcl validate --inputs)
2029
else
2130
local -ra SUBCOMMAND=(validate-inputs)
2231
local -ra RUN_ALL_SUBCOMMAND=(run-all validate-inputs)
2332
fi
2433

25-
local -r tool_name="terragrunt"
26-
2734
# shellcheck disable=SC2153 # False positive
2835
common::per_dir_hook "$HOOK_ID" "$tool_name" "${#ARGS[@]}" "${ARGS[@]}" "${FILES[@]}"
2936
}

0 commit comments

Comments
 (0)