Do not pass an empty argument to tofu without a root directory

What

When GITLAB_TOFU_ROOT_DIR is unset, gitlab-tofu called tofu "${tf_chdir_opt}" ... with an empty tf_chdir_opt, so tofu got an empty first argument. OpenTofu up to 1.12 (mitchellh/cli) skipped empty arguments when picking the subcommand. The new CLI parser that 1.13.0 enables by default treats "" as the command name and fails with OpenTofu has no command named "".

This blocks !576 (merged) (OpenTofu 1.12.6 -> 1.13.0): unit-test:gitlab-tofu: [1.13.0, *] fails at gitlab-tofu version in before_script, and every unit-test:gitlab-tofu:source job fails too (pipeline).

Component users are not affected because the templates always set root_dir (default .). Users who run gitlab-tofu from the image directly without GITLAB_TOFU_ROOT_DIR are affected.

Fix

Pass the option only when it is set: tofu ${tf_chdir_opt:+"${tf_chdir_opt}"} .... This is POSIX parameter expansion. When the variable is empty it produces no word in sh, bash, ksh and zsh (zsh drops empty unquoted expansions even without sh_word_split). When it is set, it stays a single word, even if the path contains spaces.

I rejected always passing -chdir=${abs_tf_root}. Without GITLAB_TOFU_ROOT_DIR, abs_tf_root is CI_PROJECT_DIR, so tofu would move from the working directory to the project directory. That breaks running from within the root module (cd infra && gitlab-tofu plan):

  • tofu would resolve the relative default plan.cache and --var-file paths against the project directory, while the shell's > "${plan_json_path}" redirect still resolves against the working directory;
  • fmt -recursive would scan the whole project.

I did not use the TOFU_EXPERIMENTAL_CLI_ENABLED=false opt-out, because the empty argument is our bug and not an OpenTofu one.

Tests

  • The existing test meant to cover this ran unset "$GITLAB_TOFU_ROOT_DIR", which unsets a variable named after the path, so it never ran without the variable. It now unsets the variable itself and has a title that matches what it does.
  • A new plan test also runs without the variable and checks that plan.cache and plan.json land in the working directory.

The CI matrix on main does not include 1.13.0 yet, so these tests only catch the regression once !576 (merged) lands. Local verification (macOS, local backend override for tests/iac):

1.12.6 1.13.0
main: both tests without GITLAB_TOFU_ROOT_DIR ok fail (no command named "")
this MR: both tests without GITLAB_TOFU_ROOT_DIR ok ok
this MR: version/init/validate/fmt/plan/apply/output, with and without GITLAB_TOFU_ROOT_DIR ok ok
this MR: sourced in bash/zsh/ksh, tofu_init ok ok

shellcheck src/gitlab-tofu.sh is clean.

Sourcing the script twice

A second commit resets tf_chdir_opt, default_tf_plan_cache and default_tf_plan_json when GITLAB_TOFU_ROOT_DIR is unset. Before, sourcing the script again in the same shell after unsetting the variable kept the previous root directory for -chdir and the plan paths. A new source-tagged test covers this; locally it fails before the change and passes after it in bash, zsh and ksh.

🤖 Generated with Claude Code

Edited by Timo Furrer

Merge request reports

Loading
Loading