Skip to content

Commit a43b30a

Browse files
authored
fix: Fixing stack run when there are only units with no_dot_terragrunt_stack set (#4628)
* fix: Fixing `stack run` when there are only units with `no_dot_terragrunt_stack` set * fix: Removing expected failure here
1 parent a45b493 commit a43b30a

File tree

5 files changed

+59
-22
lines changed

5 files changed

+59
-22
lines changed

cli/commands/stack/stack.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package stack
22

33
import (
44
"context"
5-
"os"
65
"path/filepath"
76
"strings"
87

@@ -17,10 +16,6 @@ import (
1716
"github.com/gruntwork-io/terragrunt/options"
1817
)
1918

20-
const (
21-
stackDir = ".terragrunt-stack"
22-
)
23-
2419
// RunGenerate runs the stack command.
2520
func RunGenerate(ctx context.Context, l log.Logger, opts *options.TerragruntOptions) error {
2621
opts.TerragruntStackConfigPath = filepath.Join(opts.WorkingDir, config.DefaultStackFile)
@@ -70,11 +65,6 @@ func Run(ctx context.Context, l log.Logger, opts *options.TerragruntOptions) err
7065
return err
7166
}
7267

73-
opts.WorkingDir = filepath.Join(opts.WorkingDir, stackDir)
74-
if _, err := os.Stat(opts.WorkingDir); os.IsNotExist(err) {
75-
return errors.Errorf("Stack directory does not exist or is not accessible: %s", opts.WorkingDir)
76-
}
77-
7868
return runall.Run(ctx, l, opts)
7969
}
8070

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
unit "foo" {
2+
source = "../unit"
3+
path = "foo"
4+
no_dot_terragrunt_stack = true
5+
}
6+
7+
unit "bar" {
8+
source = "../unit"
9+
path = "bar"
10+
no_dot_terragrunt_stack = true
11+
}
12+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
output "test" {
2+
value = var.test
3+
}
4+
5+
variable "test" {
6+
type = string
7+
}
8+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
terraform {
2+
source = "."
3+
}
4+
5+
inputs = {
6+
test = "value"
7+
}
8+

test/integration_stacks_test.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const (
4848
testFixtureStackNestedOutputs = "fixtures/stacks/nested-outputs"
4949
testFixtureStackNoValidation = "fixtures/stacks/no-validation"
5050
testFixtureStackTerragruntDir = "fixtures/stacks/terragrunt-dir"
51+
testFixtureStacksAllNoStackDir = "fixtures/stacks/all-no-stack-dir"
5152
testFixtureStackNoDotTerragruntStackOutput = "fixtures/stacks/no-dot-terragrunt-stack-output"
5253
)
5354

@@ -1047,18 +1048,6 @@ func TestStacksNoStackDirDirectoryCreated(t *testing.T) {
10471048
assert.NoDirExists(t, path)
10481049
}
10491050

1050-
func TestStacksNoStackCommandFail(t *testing.T) {
1051-
t.Parallel()
1052-
1053-
helpers.CleanupTerraformFolder(t, testFixtureNoStackNoDir)
1054-
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureNoStackNoDir)
1055-
rootPath := util.JoinPath(tmpEnvPath, testFixtureNoStackNoDir, "live")
1056-
1057-
_, _, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt stack run apply --non-interactive --working-dir "+rootPath)
1058-
require.Error(t, err)
1059-
assert.Contains(t, err.Error(), "Stack directory does not exist or is not accessible")
1060-
}
1061-
10621051
func TestStacksGeneratePrintWarning(t *testing.T) {
10631052
t.Parallel()
10641053

@@ -1354,6 +1343,36 @@ func TestStackTerragruntDir(t *testing.T) {
13541343
assert.Contains(t, out, `terragrunt_dir = "./tennant_1"`)
13551344
}
13561345

1346+
func TestStackRunAllNoStackDir(t *testing.T) {
1347+
t.Parallel()
1348+
1349+
helpers.CleanupTerraformFolder(t, testFixtureStacksAllNoStackDir)
1350+
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureStacksAllNoStackDir)
1351+
1352+
rootPath := util.JoinPath(tmpEnvPath, testFixtureStacksAllNoStackDir, "live")
1353+
1354+
helpers.RunTerragrunt(t, "terragrunt stack generate --working-dir "+rootPath)
1355+
1356+
// Verify that no .terragrunt-stack directory was created since all units have no_dot_terragrunt_stack = true
1357+
stackDir := util.JoinPath(rootPath, ".terragrunt-stack")
1358+
stackDirExists := util.FileExists(stackDir)
1359+
t.Logf("Stack directory exists: %v", stackDirExists)
1360+
1361+
// Verify that units were generated in the same directory as terragrunt.stack.hcl
1362+
expectedUnits := []string{"foo", "bar"}
1363+
for _, unit := range expectedUnits {
1364+
unitPath := util.JoinPath(rootPath, unit)
1365+
assert.True(t, util.FileExists(unitPath), "Expected unit %s to exist in root directory", unit)
1366+
assert.True(t, util.FileExists(util.JoinPath(unitPath, "terragrunt.hcl")), "Expected terragrunt.hcl to exist in unit %s", unit)
1367+
}
1368+
1369+
stdout, _, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt stack run plan --non-interactive --working-dir "+rootPath)
1370+
require.NoError(t, err, "Expected stack run to succeed when all units have no_dot_terragrunt_stack = true")
1371+
1372+
assert.Contains(t, stdout, "Changes to Outputs:")
1373+
assert.Contains(t, stdout, "+ test = \"value\"")
1374+
}
1375+
13571376
func TestStackOutputWithNoDotTerragruntStack(t *testing.T) {
13581377
t.Parallel()
13591378

0 commit comments

Comments
 (0)