Skip to content

Commit 86d3697

Browse files
committed
fix: Using better designed reset for dependency config path
1 parent 46b76e8 commit 86d3697

2 files changed

Lines changed: 24 additions & 11 deletions

File tree

pkg/config/dependency.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,11 @@ func decodeDependencies(ctx context.Context, pctx *ParsingContext, l log.Logger,
307307
l.Debugf("Reading Terragrunt config file at %s", util.RelPathForLog(pctx.RootWorkingDir, depPath, pctx.Writers.LogShowAbsPaths))
308308
}
309309

310-
_, depCtx, err := pctx.WithConfigPath(l, depPath)
310+
_, depCtx, err := pctx.WithDependencyConfigPath(l, depPath)
311311
if err != nil {
312312
return nil, err
313313
}
314314

315-
depCtx.OriginalTerragruntConfigPath = depPath
316315
depCtx.DownloadDir = filepath.Join(filepath.Dir(depPath), util.TerragruntCacheDir)
317316

318317
if depCtx.IAMRoleOptions != depCtx.OriginalIAMRoleOptions {
@@ -394,13 +393,11 @@ func checkForDependencyBlockCycles(ctx context.Context, pctx *ParsingContext, l
394393

395394
dependencyPath := getCleanedTargetConfigPath(dependency.ConfigPath.AsString(), configPath)
396395

397-
l, dependencyContext, err := pctx.WithConfigPath(l, dependencyPath)
396+
l, dependencyContext, err := pctx.WithDependencyConfigPath(l, dependencyPath)
398397
if err != nil {
399398
return err
400399
}
401400

402-
dependencyContext.OriginalTerragruntConfigPath = dependencyPath
403-
404401
if err := checkForDependencyBlockCyclesUsingDFS(ctx, dependencyContext, l, dependencyPath, &visitedPaths, &currentTraversalPaths); err != nil {
405402
return err
406403
}
@@ -439,13 +436,11 @@ func checkForDependencyBlockCyclesUsingDFS(
439436
for _, dependency := range dependencyPaths {
440437
dependencyPath := getCleanedTargetConfigPath(dependency, dependencyPath)
441438

442-
l, dependencyContext, err := pctx.WithConfigPath(l, dependencyPath)
439+
l, dependencyContext, err := pctx.WithDependencyConfigPath(l, dependencyPath)
443440
if err != nil {
444441
return err
445442
}
446443

447-
dependencyContext.OriginalTerragruntConfigPath = dependencyPath
448-
449444
if err := checkForDependencyBlockCyclesUsingDFS(ctx, dependencyContext, l, dependencyPath, visitedPaths, currentTraversalPaths); err != nil {
450445
return err
451446
}
@@ -765,14 +760,12 @@ func getOutputJSONWithCaching(ctx context.Context, pctx *ParsingContext, l log.L
765760
// by directly pulling down the state file. Otherwise, terragrunt will fallback to running `terragrunt output` on the
766761
// target module.
767762
func getTerragruntOutputJSON(ctx context.Context, pctx *ParsingContext, l log.Logger, targetConfig string) ([]byte, error) {
768-
// Create dependency context using WithConfigPath
769-
l, pctx, err := pctx.WithConfigPath(l, targetConfig)
763+
l, pctx, err := pctx.WithDependencyConfigPath(l, targetConfig)
770764
if err != nil {
771765
return nil, err
772766
}
773767

774768
// Set dependency-specific fields
775-
pctx.OriginalTerragruntConfigPath = targetConfig
776769
pctx.ForwardTFStdout = false
777770
pctx.CheckDependentUnits = false
778771
pctx.TerraformCommand = "output"

pkg/config/parsing_context.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ func (ctx *ParsingContext) WithIncrementedDepth() (*ParsingContext, error) {
227227
// WithConfigPath returns a new ParsingContext with the config path updated.
228228
// It normalizes the path to an absolute path, updates WorkingDir to the directory
229229
// containing the config, and adjusts the logger's working directory field if it changed.
230+
//
231+
// OriginalTerragruntConfigPath is intentionally NOT updated — this is correct
232+
// when reading a config from another (e.g. read_terragrunt_config), where the
233+
// "original" caller should be preserved. For parsing a dependency as a standalone
234+
// unit, use WithDependencyConfigPath instead.
230235
func (ctx *ParsingContext) WithConfigPath(l log.Logger, configPath string) (log.Logger, *ParsingContext, error) {
231236
configPath = filepath.Clean(configPath)
232237
if !filepath.IsAbs(configPath) {
@@ -245,3 +250,18 @@ func (ctx *ParsingContext) WithConfigPath(l log.Logger, configPath string) (log.
245250

246251
return l, c, nil
247252
}
253+
254+
// WithDependencyConfigPath returns a new ParsingContext for parsing a dependency
255+
// as a standalone unit. Unlike WithConfigPath, this also resets
256+
// OriginalTerragruntConfigPath so that get_original_terragrunt_dir() resolves
257+
// relative to the dependency, not the caller.
258+
func (ctx *ParsingContext) WithDependencyConfigPath(l log.Logger, configPath string) (log.Logger, *ParsingContext, error) {
259+
l, c, err := ctx.WithConfigPath(l, configPath)
260+
if err != nil {
261+
return l, nil, err
262+
}
263+
264+
c.OriginalTerragruntConfigPath = c.TerragruntConfigPath
265+
266+
return l, c, nil
267+
}

0 commit comments

Comments
 (0)