Skip to content

Fix plan output relative #4492

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 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion internal/runner/common/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/plan-output/private-dns-zone/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
variable "terragrunt_dir" {}

output "terragrunt_dir" {
value = var.terragrunt_dir
}
7 changes: 7 additions & 0 deletions test/fixtures/plan-output/private-dns-zone/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
terraform {
source = "."
}

inputs = {
terragrunt_dir = "."
}
5 changes: 5 additions & 0 deletions test/fixtures/plan-output/resource-group/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
variable "terragrunt_dir" {}

output "terragrunt_dir" {
value = var.terragrunt_dir
}
7 changes: 7 additions & 0 deletions test/fixtures/plan-output/resource-group/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
terraform {
source = "."
}

inputs = {
terragrunt_dir = "."
}
5 changes: 5 additions & 0 deletions test/fixtures/plan-output/vnet/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
variable "terragrunt_dir" {}

output "terragrunt_dir" {
value = var.terragrunt_dir
}
7 changes: 7 additions & 0 deletions test/fixtures/plan-output/vnet/terragrunt.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
terraform {
source = "."
}

inputs = {
terragrunt_dir = "."
}
26 changes: 26 additions & 0 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()

Expand Down
Loading