Skip to content

fix: Adding explicit check to see if user set --tf-path #4493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 2, 2025

Conversation

yhakbar
Copy link
Collaborator

@yhakbar yhakbar commented Jul 1, 2025

Description

Fixes #4489

Separately track if a user has explicitly set the --tf-path, and use that instead of checking if the version in --tf-path differs from the default.

TODOs

Read the Gruntwork contribution guidelines.

  • I authored this code entirely myself
  • I am submitting code based on open source software (e.g. MIT, MPL-2.0, Apache)]
  • I am adding or upgrading a dependency or adapted code and confirm it has a compatible open source license
  • Update the docs.
  • Run the relevant tests successfully, including pre-commit checks.
  • Include release notes. If this PR is backward incompatible, include a migration guide.

Release Notes (draft)

Updated implementation of --tf-path check to just store a boolean indicating that the user has explicitly set the --tf-path flag, rather than checking if the version in --tf-path differs from the default.

Migration Guide

Summary by CodeRabbit

  • Refactor

    • Renamed the Terraform binary path field in configuration and options from TerraformPath to TFPath.
    • Added a flag to track if the Terraform path was explicitly set by the user.
    • Updated error messages, environment variables, and help output to use the new field name.
    • Improved logic for handling overrides and configuration of the Terraform binary path.
  • Tests

    • Updated and added tests to reflect the new field name and logic.
    • Introduced new integration tests for binary path overrides.
    • Added helper functions to check for the installation of Terraform and OpenTofu binaries.
  • Chores

    • Added and updated test fixture scripts and configuration files to support new test scenarios.
    • Removed obsolete scripts and imports.

Copy link

vercel bot commented Jul 1, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
terragrunt-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 2, 2025 2:44pm

Copy link
Contributor

coderabbitai bot commented Jul 1, 2025

📝 Walkthrough

Walkthrough

This change refactors the handling of the Terraform binary path option throughout the codebase, renaming the TerraformPath field to TFPath in the TerragruntOptions struct and introducing a boolean TFPathExplicitlySet to track whether the path was set via CLI or environment variable. The logic now ensures that CLI flag and environment variable take precedence over configuration file settings for the Terraform binary path.

Changes

File(s) Change Summary
options/options.go Renamed TerraformPath to TFPath in TerragruntOptions; added TFPathExplicitlySet flag; updated constructor.
cli/commands/commands.go, cli/commands/info/print/print.go, cli/commands/run/cli.go, Updated references from TerraformPath to TFPath throughout CLI logic and printing.
cli/commands/run/errors.go, cli/commands/run/help.go, cli/commands/run/hook.go, Updated error messages and env var assignments to use TFPath.
cli/commands/run/flags.go Changed flag handling to set TFPath and TFPathExplicitlySet via a custom setter instead of direct assignment.
cli/commands/run/version_check.go, cli/commands/run/version_command.go, Updated logic to use TFPathExplicitlySet for precedence over config.
config/config_partial.go, config/dependency.go Replaced logic to check/set Terraform binary path using TFPathExplicitlySet and TFPath.
shell/run_cmd.go, tf/run_cmd.go Switched command execution logic to use TFPath.
cli/commands/run/cli_test.go, cli/commands/run/run_test.go, tf/run_cmd_test.go Updated tests to use TFPath instead of TerraformPath.
test/fixtures/tf-path/basic/other-tf.sh, test/fixtures/tf-path/basic/tf.sh, Simplified/added test scripts for binary path testing.
test/fixtures/tf-path/tf.sh Removed obsolete test script.
test/fixtures/tf-path/tofu-terraform/main.tf, test/fixtures/tf-path/tofu-terraform/terragrunt.hcl Added new test fixture for tofu/terraform binary override scenarios.
test/helpers/package.go Added helpers to check for OpenTofu and Terraform installation.
test/integration_test.go Refactored and expanded tests to verify correct precedence of binary path options.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CLI
    participant Options
    participant Config

    User->>CLI: Run terragrunt with --tf-path or TG_TF_PATH
    CLI->>Options: Set TFPath, set TFPathExplicitlySet = true
    CLI->>Config: Load terragrunt.hcl (terraform_binary)
    Config->>Options: If !TFPathExplicitlySet, set TFPath from config
    CLI->>CLI: Use Options.TFPath to invoke Terraform/OpenTofu
Loading

Assessment against linked issues

Objective Addressed Explanation
CLI flag and TG_TF_PATH env var should override terragrunt.hcl terraform_binary parameter (#4489)
Ensure correct precedence: --tf-path > TG_TF_PATH > terraform_binary in config (#4489)
Use explicit flag tracking to avoid config override if CLI/env is set (#4489)

Possibly related PRs

  • gruntwork-io/terragrunt#3895: Both PRs modify CLI command logic for Terraform path handling, including the isTerraformPath function and flag restructuring.
  • gruntwork-io/terragrunt#4332: Both PRs update logic in config/config_partial.go to ensure CLI flag takes precedence over config for Terraform binary path.

Suggested reviewers

  • levkohimins
  • denis256
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@yhakbar yhakbar marked this pull request as ready for review July 2, 2025 12:35
@yhakbar yhakbar requested review from denis256 and wakeful as code owners July 2, 2025 12:35
denis256
denis256 previously approved these changes Jul 2, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4b9994e and cab47c5.

📒 Files selected for processing (2)
  • test/helpers/package.go (1 hunks)
  • test/integration_test.go (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • test/helpers/package.go
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.go`: Review the Go code for quality and correctness. Make sure that the Go code follows best practices, is performant, and is easy to understand and maintain.

**/*.go: Review the Go code for quality and correctness. Make sure that the Go code follows best practices, is performant, and is easy to understand and maintain.

⚙️ Source: CodeRabbit Configuration File

List of files the instruction was applied to:

  • test/integration_test.go
🧠 Learnings (2)
📓 Common learnings
Learnt from: yhakbar
PR: gruntwork-io/terragrunt#4169
File: cli/commands/hcl/validate/cli.go:29-60
Timestamp: 2025-04-17T13:02:28.098Z
Learning: Avoid shadowing imported packages with local variables in Go code, such as using a variable named `flags` when the `github.com/gruntwork-io/terragrunt/cli/flags` package is imported. Use more specific variable names like `flagSet` instead.
Learnt from: yhakbar
PR: gruntwork-io/terragrunt#3868
File: docs-starlight/patches/@astrojs%[email protected]:33-33
Timestamp: 2025-02-10T23:20:04.295Z
Learning: In Terragrunt projects, all `.hcl` files can be assumed to be Terragrunt configurations by default, with specific exceptions like `.terraform.lock.hcl` that need explicit handling.
test/integration_test.go (2)

undefined

<retrieved_learning>
Learnt from: yhakbar
PR: #4169
File: cli/commands/hcl/validate/cli.go:29-60
Timestamp: 2025-04-17T13:02:28.098Z
Learning: Avoid shadowing imported packages with local variables in Go code, such as using a variable named flags when the github.com/gruntwork-io/terragrunt/cli/flags package is imported. Use more specific variable names like flagSet instead.
</retrieved_learning>

<retrieved_learning>
Learnt from: levkohimins
PR: #3723
File: cli/commands/stack/action.go:160-160
Timestamp: 2025-02-10T13:36:19.542Z
Learning: The project uses a custom error package github.com/gruntwork-io/terragrunt/internal/errors which provides similar functionality to fmt.Errorf but includes stack traces. Prefer using this package's error functions (e.g., errors.Errorf, errors.New) over the standard library's error handling.
</retrieved_learning>

🧬 Code Graph Analysis (1)
test/integration_test.go (2)
test/helpers/package.go (7)
  • CleanupTerraformFolder (820-827)
  • CopyEnvironment (86-99)
  • IsTerraformInstalled (755-757)
  • IsOpenTofuInstalled (750-752)
  • TofuBinary (64-64)
  • TerraformBinary (63-63)
  • RunTerragruntCommandWithOutput (935-939)
util/file.go (1)
  • JoinPath (481-483)
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: Test (Windows)
  • GitHub Check: License Check
  • GitHub Check: Test OIDC (GHA AWS)
  • GitHub Check: Test (macos)
  • GitHub Check: build-and-test
  • GitHub Check: Pull Request has non-contributor approval
🔇 Additional comments (3)
test/integration_test.go (3)

114-115: LGTM! Clear constant naming for test fixtures.

The split of the original constant into two more specific ones (testFixtureTfPathBasic and testFixtureTfPathTofuTerraform) improves clarity and supports the test restructuring.


4152-4166: LGTM! Clean fixture path update.

The update to use testFixtureTfPathBasic is consistent with the test refactoring and maintains the existing test logic.


4168-4226: Excellent comprehensive test for tf-path binary overrides.

This test effectively validates the interaction between feature flags and tf-path overrides with both Terraform and OpenTofu. Key strengths:

  • Proper prerequisite checking with binary availability
  • Comprehensive test cases covering various flag/binary combinations
  • Clear expected outcomes for each scenario
  • Good use of table-driven test pattern
  • Follows established testing patterns in the codebase

The test ensures that tf-path overrides work correctly regardless of feature flag settings, which is important for user experience.

Comment on lines +4140 to +4144
if tfPath := os.Getenv("TG_TF_PATH"); tfPath != "" {
// Unset after using t.Setenv so that it'll be reset after the test.
t.Setenv("TG_TF_PATH", "")
os.Unsetenv("TG_TF_PATH")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix inconsistent environment variable handling.

The current pattern is problematic:

t.Setenv("TG_TF_PATH", "")      // Sets to empty string, schedules restoration
os.Unsetenv("TG_TF_PATH")       // Immediately unsets completely

This creates undefined behavior. Choose one approach:

  • Option 1 (Recommended): If you need the variable to be empty:
-t.Setenv("TG_TF_PATH", "")
-os.Unsetenv("TG_TF_PATH")
+t.Setenv("TG_TF_PATH", "")
  • Option 2: If you need the variable completely unset, don't use t.Setenv which schedules restoration:
-t.Setenv("TG_TF_PATH", "")
-os.Unsetenv("TG_TF_PATH")
+os.Unsetenv("TG_TF_PATH")
+t.Cleanup(func() { os.Setenv("TG_TF_PATH", tfPath) })
🤖 Prompt for AI Agents
In test/integration_test.go around lines 4140 to 4144, the environment variable
TG_TF_PATH is being set to an empty string using t.Setenv and then immediately
unset with os.Unsetenv, causing inconsistent behavior. To fix this, either use
only t.Setenv("TG_TF_PATH", "") if you want the variable to be empty and
restored after the test, or use only os.Unsetenv("TG_TF_PATH") if you want it
completely unset without restoration. Remove the redundant call to ensure
consistent environment variable handling.

@yhakbar yhakbar merged commit 650ecfc into main Jul 2, 2025
71 of 75 checks passed
@yhakbar yhakbar deleted the fix/fixing-tf-path-again branch July 2, 2025 15:43
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.

terraform_binary overrides TG_TF_PATH and --tf-path
2 participants