Skip to content

Commit 90126c1

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

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)
@@ -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 & 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) {
@@ -1275,3 +1276,20 @@ func validateStackDir(t *testing.T, path string) {
12751276

12761277
assert.True(t, hasSubdirectories, "The .terragrunt-stack directory should contain at least one subdirectory")
12771278
}
1279+
1280+
func TestStackTerragruntDir(t *testing.T) {
1281+
t.Parallel()
1282+
1283+
helpers.CleanupTerraformFolder(t, testFixtureStackTerragruntDir)
1284+
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureStackTerragruntDir)
1285+
gitPath := util.JoinPath(tmpEnvPath, testFixtureStackTerragruntDir)
1286+
helpers.CreateGitRepo(t, gitPath)
1287+
rootPath := util.JoinPath(gitPath, "live")
1288+
1289+
_, _, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt stack generate --no-stack-validate --working-dir "+rootPath)
1290+
require.NoError(t, err)
1291+
1292+
out, _, err := helpers.RunTerragruntCommandWithOutput(t, "terragrunt apply --all --non-interactive --working-dir "+rootPath)
1293+
require.NoError(t, err)
1294+
assert.Contains(t, out, `terragrunt_dir = "./tennant_1"`)
1295+
}

0 commit comments

Comments
 (0)