Skip to content

Commit a22611c

Browse files
committed
Fix the inconsistent behaviour of get_terragrunt_dir function
1 parent d291c0c commit a22611c

File tree

7 files changed

+53
-27
lines changed

7 files changed

+53
-27
lines changed

config/config_helpers.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,12 @@ func getPathToRepoRoot(ctx *ParsingContext, l log.Logger) (string, error) {
271271

272272
// GetTerragruntDir returns the directory where the Terragrunt configuration file lives.
273273
func GetTerragruntDir(ctx *ParsingContext, l log.Logger) (string, error) {
274-
terragruntConfigFileAbsPath, err := filepath.Abs(ctx.TerragruntOptions.TerragruntConfigPath)
274+
path := ctx.TerragruntOptions.TerragruntConfigPath
275+
if val, ok := ctx.Context.Value(stackParserContext{}).(stackParserContext); ok {
276+
path = val.stackConfigFile
277+
}
278+
279+
terragruntConfigFileAbsPath, err := filepath.Abs(path)
275280
if err != nil {
276281
return "", errors.New(err)
277282
}

config/stack.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ type Stack struct {
7272
Path string `hcl:"path,attr"`
7373
}
7474

75+
type stackParserContext struct {
76+
stackConfigFile string
77+
}
78+
7579
// GenerateStacks generates the stack files.
7680
func GenerateStacks(ctx context.Context, l log.Logger, opts *options.TerragruntOptions) error {
7781
processedFiles := make(map[string]bool)
@@ -674,6 +678,7 @@ func (u *Unit) ReadOutputs(ctx context.Context, l log.Logger, opts *options.Terr
674678
func ReadStackConfigFile(ctx context.Context, l log.Logger, opts *options.TerragruntOptions, filePath string, values *cty.Value) (*StackConfig, error) {
675679
l.Debugf("Reading Terragrunt stack config file at %s", filePath)
676680

681+
ctx = context.WithValue(ctx, stackParserContext{}, stackParserContext{stackConfigFile: filePath})
677682
parser := NewParsingContext(ctx, l, opts)
678683

679684
file, err := hclparse.NewParser(parser.ParserOptions...).ParseFromFile(filePath)

test/fixtures/stacks/terragrunt-dir/live/root.hcl

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
unit "unit_a" {
2+
source = "../../unit_a"
3+
path = "unit_a"
4+
values = {
5+
terragrunt_dir = get_terragrunt_dir()
6+
}
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
variable "terragrunt_dir" {}
2+
3+
output "terragrunt_dir" {
4+
value = var.terragrunt_dir
5+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
include "root" {
3+
path = find_in_parent_folders("root.hcl")
4+
}
5+
6+
terraform {
7+
source = "."
8+
}
9+
10+
inputs = {
11+
terragrunt_dir = values.terragrunt_dir
12+
}

test/integration_stacks_test.go

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const (
4747
testFixtureStackSelfInclude = "fixtures/stacks/self-include"
4848
testFixtureStackNestedOutputs = "fixtures/stacks/nested-outputs"
4949
testFixtureStackNoValidation = "fixtures/stacks/no-validation"
50+
testFixtureStackTerragruntDir = "fixtures/stacks/terragrunt-dir"
5051
)
5152

5253
func TestStacksGenerateBasic(t *testing.T) {
@@ -249,32 +250,6 @@ func TestStacksApplyClean(t *testing.T) {
249250
assert.NoDirExists(t, path)
250251
}
251252

252-
func TestStackCleanRecursively(t *testing.T) {
253-
t.Parallel()
254-
255-
helpers.CleanupTerraformFolder(t, testFixtureNestedStacks)
256-
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureNestedStacks)
257-
gitPath := util.JoinPath(tmpEnvPath, testFixtureNestedStacks)
258-
helpers.CreateGitRepo(t, gitPath)
259-
live := util.JoinPath(gitPath, "live")
260-
261-
_, _, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt stack generate --working-dir "+live)
262-
require.NoError(t, err)
263-
264-
liveV2 := util.JoinPath(gitPath, "live-v2")
265-
_, _, err = helpers.RunTerragruntCommandWithOutput(t, "terragrunt stack generate --working-dir "+liveV2)
266-
require.NoError(t, err)
267-
268-
_, stderr, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt stack clean --working-dir "+gitPath)
269-
require.NoError(t, err)
270-
271-
assert.NoDirExists(t, util.JoinPath(live, ".terragrunt-stack"))
272-
assert.NoDirExists(t, util.JoinPath(liveV2, ".terragrunt-stack"))
273-
274-
assert.Contains(t, stderr, "Deleting stack directory: live/.terragrunt-stack")
275-
assert.Contains(t, stderr, "Deleting stack directory: live-v2/.terragrunt-stack")
276-
}
277-
278253
func TestStacksDestroy(t *testing.T) {
279254
t.Parallel()
280255

@@ -1275,3 +1250,20 @@ func validateStackDir(t *testing.T, path string) {
12751250

12761251
assert.True(t, hasSubdirectories, "The .terragrunt-stack directory should contain at least one subdirectory")
12771252
}
1253+
1254+
func TestStackTerragruntDir(t *testing.T) {
1255+
t.Parallel()
1256+
1257+
helpers.CleanupTerraformFolder(t, testFixtureStackTerragruntDir)
1258+
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureStackTerragruntDir)
1259+
gitPath := util.JoinPath(tmpEnvPath, testFixtureStackTerragruntDir)
1260+
helpers.CreateGitRepo(t, gitPath)
1261+
rootPath := util.JoinPath(gitPath, "live")
1262+
1263+
_, _, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt stack generate --no-stack-validate --working-dir "+rootPath)
1264+
require.NoError(t, err)
1265+
1266+
out, _, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt apply --all --non-interactive --working-dir "+rootPath)
1267+
require.NoError(t, err)
1268+
assert.Contains(t, out, `terragrunt_dir = "./tennant_1"`)
1269+
}

0 commit comments

Comments
 (0)