diff --git a/internal/runner/common/unit.go b/internal/runner/common/unit.go index 670cd42947..ddbdb5063c 100644 --- a/internal/runner/common/unit.go +++ b/internal/runner/common/unit.go @@ -90,7 +90,13 @@ func (unit *Unit) getPlanFilePath(l log.Logger, opts *options.TerragruntOptions, return "" } - path, err := filepath.Rel(opts.WorkingDir, unit.Path) + // module path will be an absolute path so working dir should absolute too so that Rel() can work + wdir, err := filepath.Abs(opts.WorkingDir) + if err != nil { + wdir = opts.WorkingDir + } + + path, err := filepath.Rel(wdir, unit.Path) if err != nil { l.Warnf("Failed to get relative path for %s: %v", unit.Path, err) path = unit.Path diff --git a/test/fixtures/plan-output/private-dns-zone/main.tf b/test/fixtures/plan-output/private-dns-zone/main.tf new file mode 100644 index 0000000000..d312e7d6af --- /dev/null +++ b/test/fixtures/plan-output/private-dns-zone/main.tf @@ -0,0 +1,5 @@ +variable "terragrunt_dir" {} + +output "terragrunt_dir" { + value = var.terragrunt_dir +} \ No newline at end of file diff --git a/test/fixtures/plan-output/private-dns-zone/terragrunt.hcl b/test/fixtures/plan-output/private-dns-zone/terragrunt.hcl new file mode 100644 index 0000000000..7ea2328275 --- /dev/null +++ b/test/fixtures/plan-output/private-dns-zone/terragrunt.hcl @@ -0,0 +1,7 @@ +terraform { + source = "." +} + +inputs = { + terragrunt_dir = "." +} diff --git a/test/fixtures/plan-output/resource-group/main.tf b/test/fixtures/plan-output/resource-group/main.tf new file mode 100644 index 0000000000..d312e7d6af --- /dev/null +++ b/test/fixtures/plan-output/resource-group/main.tf @@ -0,0 +1,5 @@ +variable "terragrunt_dir" {} + +output "terragrunt_dir" { + value = var.terragrunt_dir +} \ No newline at end of file diff --git a/test/fixtures/plan-output/resource-group/terragrunt.hcl b/test/fixtures/plan-output/resource-group/terragrunt.hcl new file mode 100644 index 0000000000..7ea2328275 --- /dev/null +++ b/test/fixtures/plan-output/resource-group/terragrunt.hcl @@ -0,0 +1,7 @@ +terraform { + source = "." +} + +inputs = { + terragrunt_dir = "." +} diff --git a/test/fixtures/plan-output/vnet/main.tf b/test/fixtures/plan-output/vnet/main.tf new file mode 100644 index 0000000000..d312e7d6af --- /dev/null +++ b/test/fixtures/plan-output/vnet/main.tf @@ -0,0 +1,5 @@ +variable "terragrunt_dir" {} + +output "terragrunt_dir" { + value = var.terragrunt_dir +} \ No newline at end of file diff --git a/test/fixtures/plan-output/vnet/terragrunt.hcl b/test/fixtures/plan-output/vnet/terragrunt.hcl new file mode 100644 index 0000000000..7ea2328275 --- /dev/null +++ b/test/fixtures/plan-output/vnet/terragrunt.hcl @@ -0,0 +1,7 @@ +terraform { + source = "." +} + +inputs = { + terragrunt_dir = "." +} diff --git a/test/integration_test.go b/test/integration_test.go index 158fe6cd83..af750d6db0 100644 --- a/test/integration_test.go +++ b/test/integration_test.go @@ -92,6 +92,7 @@ const ( testFixtureParallelism = "fixtures/parallelism" testFixturePath = "fixtures/terragrunt/" testFixturePlanfileOrder = "fixtures/planfile-order-test" + testFixturePlanOutput = "fixtures/plan-output" testFixtureProviderCacheDirect = "fixtures/provider-cache/direct" testFixtureProviderCacheFilesystemMirror = "fixtures/provider-cache/filesystem-mirror" testFixtureProviderCacheMultiplePlatforms = "fixtures/provider-cache/multiple-platforms" @@ -4259,6 +4260,31 @@ func TestVersionIsInvokedInDifferentDirectory(t *testing.T) { assert.Contains(t, stderr, "prefix=dependency-with-custom-version msg=Running command: "+wrappedBinary()+" -version") } +func TestTerragruntPlanAllOutput(t *testing.T) { + t.Parallel() + + helpers.CleanupTerraformFolder(t, testFixturePlanOutput) + tmpEnvPath := helpers.CopyEnvironment(t, testFixturePlanOutput) + + outDir := filepath.Join(tmpEnvPath, "plans") + cmd := fmt.Sprintf("terragrunt plan --all --non-interactive --out-dir %s --working-dir ./%s ", outDir, testFixturePlanOutput) + var ( + stdout bytes.Buffer + stderr bytes.Buffer + ) + // Call helpers.RunTerragruntCommand directly because this command contains failures (which causes helpers.RunTerragruntRedirectOutput to abort) but we don't care. + err := helpers.RunTerragruntCommand(t, cmd, &stdout, &stderr) + require.NoError(t, err) + + output := stdout.String() + errOutput := stderr.String() + fmt.Printf("STDERR is %s.\n STDOUT is %s", errOutput, output) + + assert.FileExists(t, filepath.Join(outDir, "vnet", "tfplan.tfplan")) + assert.FileExists(t, filepath.Join(outDir, "resource-group", "tfplan.tfplan")) + assert.FileExists(t, filepath.Join(outDir, "private-dns-zone", "tfplan.tfplan")) +} + func TestMixedStackConfigIgnored(t *testing.T) { t.Parallel()