Skip to content

Commit 46b76e8

Browse files
committed
fix: Fixing get_original_terragrunt_dir()
1 parent 9b8da9b commit 46b76e8

6 files changed

Lines changed: 63 additions & 0 deletions

File tree

pkg/config/dependency.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ func decodeDependencies(ctx context.Context, pctx *ParsingContext, l log.Logger,
312312
return nil, err
313313
}
314314

315+
depCtx.OriginalTerragruntConfigPath = depPath
315316
depCtx.DownloadDir = filepath.Join(filepath.Dir(depPath), util.TerragruntCacheDir)
316317

317318
if depCtx.IAMRoleOptions != depCtx.OriginalIAMRoleOptions {
@@ -398,6 +399,8 @@ func checkForDependencyBlockCycles(ctx context.Context, pctx *ParsingContext, l
398399
return err
399400
}
400401

402+
dependencyContext.OriginalTerragruntConfigPath = dependencyPath
403+
401404
if err := checkForDependencyBlockCyclesUsingDFS(ctx, dependencyContext, l, dependencyPath, &visitedPaths, &currentTraversalPaths); err != nil {
402405
return err
403406
}
@@ -441,6 +444,8 @@ func checkForDependencyBlockCyclesUsingDFS(
441444
return err
442445
}
443446

447+
dependencyContext.OriginalTerragruntConfigPath = dependencyPath
448+
444449
if err := checkForDependencyBlockCyclesUsingDFS(ctx, dependencyContext, l, dependencyPath, visitedPaths, currentTraversalPaths); err != nil {
445450
return err
446451
}

pkg/config/dependency_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,37 @@ dependency "enabled" {
188188

189189
// TestDisabledDependencyWithEmptyConfigPath verifies that disabled dependencies
190190
// with empty config_path don't cause errors.
191+
// TestDependencyOriginalTerragruntDir verifies that when parsing a dependency's
192+
// config during cycle detection, get_original_terragrunt_dir() returns the
193+
// dependency's directory, not the caller's directory.
194+
//
195+
// Regression test: when unit-a depends on unit-b, and unit-b's config chain
196+
// calls get_original_terragrunt_dir(), it must resolve to unit-b's directory so
197+
// that paths constructed from it point to files that exist alongside unit-b.
198+
func TestDependencyOriginalTerragruntDir(t *testing.T) {
199+
t.Parallel()
200+
201+
filename, err := filepath.Abs(
202+
filepath.Join(
203+
"../..",
204+
"test",
205+
"fixtures",
206+
"regressions",
207+
"dependency-original-terragrunt-dir",
208+
"unit-a",
209+
"terragrunt.hcl",
210+
),
211+
)
212+
require.NoError(t, err)
213+
214+
ctx, pctx := newTestParsingContext(t, filename)
215+
pctx.OriginalTerragruntConfigPath = filename
216+
pctx.SkipOutput = true
217+
218+
_, err = config.ParseConfigFile(ctx, pctx, logger.CreateLogger(), filename, nil)
219+
require.NoError(t, err)
220+
}
221+
191222
func TestDisabledDependencyWithEmptyConfigPath(t *testing.T) {
192223
t.Parallel()
193224

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
dependency "unit_b" {
2+
config_path = "../unit-b"
3+
skip_outputs = true
4+
mock_outputs = {
5+
app_name = "mock"
6+
}
7+
}
8+
9+
inputs = {
10+
dep_name = dependency.unit_b.outputs.app_name
11+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
locals {
2+
app_name = "myapp"
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
locals {
2+
original_dir = get_original_terragrunt_dir()
3+
common_config = read_terragrunt_config("${local.original_dir}/_common.hcl")
4+
app_name = local.common_config.locals.app_name
5+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
locals {
2+
common = read_terragrunt_config("common.hcl")
3+
app_name = local.common.locals.app_name
4+
}
5+
6+
inputs = {
7+
app_name = local.app_name
8+
}

0 commit comments

Comments
 (0)