From 7fb445fcc02f09955736b8ee554776a5bc704541 Mon Sep 17 00:00:00 2001 From: Thomas Magalhaes Date: Wed, 21 May 2025 20:24:45 +0200 Subject: [PATCH 1/3] test: add a test to check command generation with different order 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 ``` --- cli/commands/run/run_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cli/commands/run/run_test.go b/cli/commands/run/run_test.go index 512e21c792..f46964b4a8 100644 --- a/cli/commands/run/run_test.go +++ b/cli/commands/run/run_test.go @@ -402,6 +402,12 @@ func TestFilterTerraformExtraArgs(t *testing.T) { mockExtraArgs([]string{"--foo", "-var-file=test.tfvars", "bar", "-var='key=value'", "foo"}, []string{"plan", "apply"}, []string{"required.tfvars"}, []string{temporaryFile}), []string{"--foo", "bar", "foo"}, }, + // apply with some parameters, providing a file in a different order => no var files included + { + mockCmdOptions(t, workingDir, []string{"apply", temporaryFile, "-no-color", "-foo"}), + mockExtraArgs([]string{"--foo", "-var-file=test.tfvars", "bar", "-var='key=value'", "foo"}, []string{"plan", "apply"}, []string{"required.tfvars"}, []string{temporaryFile}), + []string{"--foo", "bar", "foo"}, + }, // destroy providing a folder, var files should stay included { mockCmdOptions(t, workingDir, []string{"destroy", workingDir}), From 3b92f67f80ad1fe59c4d9e4d046c02b5e01b8333 Mon Sep 17 00:00:00 2001 From: Thomas Magalhaes Date: Wed, 21 May 2025 20:25:37 +0200 Subject: [PATCH 2/3] fix: command generation when plan is given in different order Test output: ``` > go test ./run_test.go ok command-line-arguments 0.136s ``` --- cli/commands/run/run.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/commands/run/run.go b/cli/commands/run/run.go index 84ff5f7bcf..6ec0800775 100644 --- a/cli/commands/run/run.go +++ b/cli/commands/run/run.go @@ -790,7 +790,7 @@ func FilterTerraformExtraArgs(terragruntOptions *options.TerragruntOptions, terr for _, arg := range terragruntConfig.Terraform.ExtraArgs { for _, argCmd := range arg.Commands { if cmd == argCmd { - lastArg := terragruntOptions.TerraformCliArgs.Last() + lastArg := terragruntOptions.TerraformCliArgs.CommandNameN(1) skipVars := (cmd == tf.CommandNameApply || cmd == tf.CommandNameDestroy) && util.IsFile(lastArg) // The following is a fix for GH-493. From 6c34a329bc613344a6777759354078f38309391f Mon Sep 17 00:00:00 2001 From: Thomas Magalhaes Date: Mon, 9 Jun 2025 09:45:40 +0200 Subject: [PATCH 3/3] fix(run): fix variable name after main was merged --- cli/commands/run/run.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/commands/run/run.go b/cli/commands/run/run.go index 0139ac1b3f..909253323e 100644 --- a/cli/commands/run/run.go +++ b/cli/commands/run/run.go @@ -815,7 +815,7 @@ func FilterTerraformExtraArgs(l log.Logger, opts *options.TerragruntOptions, ter for _, arg := range terragruntConfig.Terraform.ExtraArgs { for _, argCmd := range arg.Commands { if cmd == argCmd { - lastArg := terragruntOptions.TerraformCliArgs.CommandNameN(1) + lastArg := opts.TerraformCliArgs.CommandNameN(1) skipVars := (cmd == tf.CommandNameApply || cmd == tf.CommandNameDestroy) && util.IsFile(lastArg)