Skip to content

Fix terraform command generation in different order #4322

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Monkeypac
Copy link

@Monkeypac Monkeypac commented May 21, 2025

Description

Fixes #4220.

When using -no-color a var setting and a plan file, the terraform command generated looks like [...] <file> -no-color which makes the usage of "Last" wrong.
Since terraform's apply doc says it can take zero or one non-optional parameter, this should do it.

I ran tests and linters as says the doc but the overall tests fail on main, even in the cli package I get failing tests if I run go test ./cli/....
For the linters, make run-lint went well, make run-strict-lint was failing on main.

Test run for `run_test.go` with the new test, before the fix
> go test ./run_test.go
20:19:30.614 DEBUG  Running command: i-dont-exist
20:19:30.614 DEBUG  Engine is not enabled, running command directly in .
20:19:30.616 ERROR  unknown invocation failed in .
20:19:30.649 DEBUG  Skipping var-file optional.tfvars as it does not exist
--- FAIL: TestFilterTerraformExtraArgs (0.06s)
    run_test.go:451:
                Error Trace:    /home/monkeypac/dev/terragrunt/cli/commands/run/run_test.go:451
                Error:          Not equal:
                                expected: []string{"--foo", "bar", "foo"}
                                actual  : []string{"--foo", "-var-file=test.tfvars", "bar", "-var='key=value'", "foo", "-var-file=required.tfvars", "-var-file=/tmp/TestFilterTerraformExtraArgs3197671377/001/3320748883"}

                                Diff:
                                --- Expected
                                +++ Actual
                                @@ -1,5 +1,9 @@
                                -([]string) (len=3) {
                                +([]string) (len=7) {
                                  (string) (len=5) "--foo",
                                + (string) (len=21) "-var-file=test.tfvars",
                                  (string) (len=3) "bar",
                                - (string) (len=3) "foo"
                                + (string) (len=16) "-var='key=value'",
                                + (string) (len=3) "foo",
                                + (string) (len=25) "-var-file=required.tfvars",
                                + (string) (len=68) "-var-file=/tmp/TestFilterTerraformExtraArgs3197671377/001/3320748883"
                                 }
                Test:           TestFilterTerraformExtraArgs
FAIL
FAIL    command-line-arguments  0.095s
FAIL
Test run for `run_test.go` with the new test, after the fix
> go test ./run_test.go
ok      command-line-arguments  0.136s
Test run for the whole `cli/commands/run` package after the fix
> go test ./cli/commands/run
ok      github.com/gruntwork-io/terragrunt/cli/commands/run     7.387s

TODOs

Read the Gruntwork contribution guidelines.

  • Update the docs. (not concerned)
  • Run the relevant tests successfully, including pre-commit checks.
  • Ensure any 3rd party code adheres with our license policy or delete this line if its not applicable.
  • Include release notes. If this PR is backward incompatible, include a migration guide.

Release Notes (draft)

Updated terraform command generation with extra args when using a plan file.

Migration Guide

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of Terraform CLI arguments to ensure correct behavior when file arguments are provided in different positions for the apply and destroy commands.
  • Tests

    • Added a new test case to verify that var-file arguments are correctly excluded when a file argument is present, regardless of its position in the argument list.

Monkeypac added 2 commits May 21, 2025 20:24
Test output:

```
> go test ./run_test.go
20:19:30.614 DEBUG  Running command: i-dont-exist
20:19:30.614 DEBUG  Engine is not enabled, running command directly in .
20:19:30.616 ERROR  unknown invocation failed in .
20:19:30.649 DEBUG  Skipping var-file optional.tfvars as it does not exist
--- FAIL: TestFilterTerraformExtraArgs (0.06s)
    run_test.go:451:
                Error Trace:    /home/monkeypac/dev/terragrunt/cli/commands/run/run_test.go:451
                Error:          Not equal:
                                expected: []string{"--foo", "bar", "foo"}
                                actual  : []string{"--foo", "-var-file=test.tfvars", "bar", "-var='key=value'", "foo", "-var-file=required.tfvars", "-var-file=/tmp/TestFilterTerraformExtraArgs3197671377/001/3320748883"}

                                Diff:
                                --- Expected
                                +++ Actual
                                @@ -1,5 +1,9 @@
                                -([]string) (len=3) {
                                +([]string) (len=7) {
                                  (string) (len=5) "--foo",
                                + (string) (len=21) "-var-file=test.tfvars",
                                  (string) (len=3) "bar",
                                - (string) (len=3) "foo"
                                + (string) (len=16) "-var='key=value'",
                                + (string) (len=3) "foo",
                                + (string) (len=25) "-var-file=required.tfvars",
                                + (string) (len=68) "-var-file=/tmp/TestFilterTerraformExtraArgs3197671377/001/3320748883"
                                 }
                Test:           TestFilterTerraformExtraArgs
FAIL
FAIL    command-line-arguments  0.095s
FAIL
```
Test output:

```
> go test ./run_test.go
ok      command-line-arguments  0.136s
```
Copy link
Contributor

coderabbitai bot commented May 21, 2025

📝 Walkthrough

"""

Walkthrough

The update changes how the last Terraform CLI argument is determined in the filtering logic for extra arguments, replacing a method that fetches the last argument with one that retrieves the first command name. Additionally, a new test case is introduced to verify correct argument filtering when file arguments are not at the end of the command list.

Changes

File(s) Change Summary
cli/commands/run/run.go Modified logic to determine the last Terraform CLI argument using CommandNameN(1) instead of Last().
cli/commands/run/run_test.go Added a new test case to ensure correct filtering when file arguments are not last in the argument list.

Assessment against linked issues

Objective Addressed Explanation
Correctly identify when applying from a plan file and remove forbidden flags, regardless of argument order (#4220)
"""

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (1.64.8)

Error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2
Failed executing command with error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 6c34a32 and bde5473.

📒 Files selected for processing (2)
  • cli/commands/run/run.go (1 hunks)
  • cli/commands/run/run_test.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • cli/commands/run/run.go
  • cli/commands/run/run_test.go
⏰ Context from checks skipped due to timeout of 90000ms (20)
  • GitHub Check: Build (linux/amd64)
  • GitHub Check: Build (windows/amd64)
  • GitHub Check: Build (darwin/arm64)
  • GitHub Check: Build (darwin/amd64)
  • GitHub Check: Build (linux/arm64)
  • GitHub Check: Build (windows/386)
  • GitHub Check: Build (darwin/arm64)
  • GitHub Check: Build (linux/386)
  • GitHub Check: Build (windows/amd64)
  • GitHub Check: Build (linux/arm64)
  • GitHub Check: Build (windows/386)
  • GitHub Check: Build (linux/amd64)
  • GitHub Check: Build (darwin/amd64)
  • GitHub Check: Test OIDC (GHA AWS)
  • GitHub Check: License Check
  • GitHub Check: Test (macos)
  • GitHub Check: Test (ubuntu)
  • GitHub Check: lint
  • GitHub Check: lint
  • GitHub Check: build-and-test
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 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.

Copy link

vercel bot commented May 21, 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 1:05pm

@Monkeypac
Copy link
Author

Global tests are still failing on some non-related code.
run package's tests are fine

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.

--no-color is incompatible with args which are not allowed when applying from plan file
2 participants