Skip to content

Commit 9e3853e

Browse files
Fix the inconsistent behaviour of get_terragrunt_dir function (#4443)
Co-authored-by: James Kwon <[email protected]>
1 parent e7bce62 commit 9e3853e

File tree

7 files changed

+53
-1
lines changed

7 files changed

+53
-1
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)
@@ -687,6 +691,7 @@ func (u *Unit) ReadOutputs(ctx context.Context, l log.Logger, opts *options.Terr
687691
func ReadStackConfigFile(ctx context.Context, l log.Logger, opts *options.TerragruntOptions, filePath string, values *cty.Value) (*StackConfig, error) {
688692
l.Debugf("Reading Terragrunt stack config file at %s", filePath)
689693

694+
ctx = context.WithValue(ctx, stackParserContext{}, stackParserContext{stackConfigFile: filePath})
690695
parser := NewParsingContext(ctx, l, opts)
691696

692697
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 & 0 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) {
@@ -1296,3 +1297,20 @@ func validateStackDir(t *testing.T, path string) {
12961297

12971298
assert.True(t, hasSubdirectories, "The .terragrunt-stack directory should contain at least one subdirectory")
12981299
}
1300+
1301+
func TestStackTerragruntDir(t *testing.T) {
1302+
t.Parallel()
1303+
1304+
helpers.CleanupTerraformFolder(t, testFixtureStackTerragruntDir)
1305+
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureStackTerragruntDir)
1306+
gitPath := util.JoinPath(tmpEnvPath, testFixtureStackTerragruntDir)
1307+
helpers.CreateGitRepo(t, gitPath)
1308+
rootPath := util.JoinPath(gitPath, "live")
1309+
1310+
_, _, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt stack generate --no-stack-validate --working-dir "+rootPath)
1311+
require.NoError(t, err)
1312+
1313+
out, _, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt apply --all --non-interactive --working-dir "+rootPath)
1314+
require.NoError(t, err)
1315+
assert.Contains(t, out, `terragrunt_dir = "./tennant_1"`)
1316+
}

0 commit comments

Comments
 (0)