Skip to content

Commit

Permalink
Enhanced clarity of error messages for missing mock outputs when usin…
Browse files Browse the repository at this point in the history
…g the dependencies block, fix #3567 (#3692)
  • Loading branch information
wakeful authored Dec 20, 2024
1 parent e7abbd0 commit d227125
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions config/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,8 @@ func getTerragruntOutputIfAppliedElseConfiguredDefault(ctx *ParsingContext, depe
// returning mocks is not allowed. So return a useful error message indicating that we expected outputs, but they
// did not exist.
err := TerragruntOutputTargetNoOutputs{
targetName: dependencyConfig.Name,
targetPath: dependencyConfig.ConfigPath.AsString(),
targetConfig: targetConfig,
currentConfig: ctx.TerragruntOptions.TerragruntConfigPath,
}
Expand Down
32 changes: 31 additions & 1 deletion config/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,45 @@ func (err TerragruntOutputListEncodingError) Error() string {
}

type TerragruntOutputTargetNoOutputs struct {
targetName string
targetPath string
targetConfig string
currentConfig string
}

func (err TerragruntOutputTargetNoOutputs) ExitCode() int {
return 1
}

func (err TerragruntOutputTargetNoOutputs) Unwrap() error {
return nil
}

func (err TerragruntOutputTargetNoOutputs) Error() string {
msg := `
If this dependency is accessed before the outputs are ready (which can happen during the planning phase of an unapplied stack), consider using mock_outputs.
e.g.
dependency "` + err.targetName + `" {
config_path = "` + err.targetPath + `"
mock_outputs = {
` + err.targetName + `_output = "mock-` + err.targetName + `-output"
}
}
For further details, refer to this resource:
https://terragrunt.gruntwork.io/docs/features/stacks/#unapplied-dependency-and-mock-outputs
If you do not require outputs from your dependency, consider using the dependencies block instead:
https://terragrunt.gruntwork.io/docs/reference/config-blocks-and-attributes/#dependencies
`

return fmt.Sprintf(
"%s is a dependency of %s but detected no outputs. Either the target module has not been applied yet, or the module has no outputs. If this is expected, set the skip_outputs flag to true on the dependency block.",
"%s is a dependency of %s but detected no outputs. Either the target module has not been applied yet, or the module has no outputs. If this is expected, set the skip_outputs flag to true on the dependency block.\n%s",
err.targetConfig,
err.currentConfig,
msg,
)
}

Expand Down

0 comments on commit d227125

Please sign in to comment.