diff --git a/cli/commands/hclfmt/action.go b/cli/commands/hclfmt/action.go index 32a919275..88c48ebea 100644 --- a/cli/commands/hclfmt/action.go +++ b/cli/commands/hclfmt/action.go @@ -131,7 +131,9 @@ func formatTgHCL(opts *options.TerragruntOptions, tgHclFile string) error { func checkErrors(logger *logrus.Entry, disableColor bool, contents []byte, tgHclFile string) error { parser := hclparse.NewParser() _, diags := parser.ParseHCL(contents, tgHclFile) - diagWriter := util.GetDiagnosticsWriter(logger, parser, disableColor) + + writer := &util.LogWriter{Logger: logger, Level: logrus.ErrorLevel} + diagWriter := util.GetDiagnosticsWriter(writer, parser, disableColor) err := diagWriter.WriteDiagnostics(diags) if err != nil { return errors.WithStackTrace(err) diff --git a/config/config.go b/config/config.go index 678f0d73b..56328d6d7 100644 --- a/config/config.go +++ b/config/config.go @@ -73,8 +73,10 @@ var ( } DefaultParserOptions = func(opts *options.TerragruntOptions) []hclparse.Option { + writer := &util.LogWriter{Logger: opts.Logger, Level: logrus.ErrorLevel} + return []hclparse.Option{ - hclparse.WithLogger(opts.Logger, opts.DisableLogColors), + hclparse.WithDiagnosticsWriter(writer, opts.DisableLogColors), hclparse.WithFileUpdate(updateBareIncludeBlock), } } diff --git a/config/dependency.go b/config/dependency.go index 546794a99..b841bc201 100644 --- a/config/dependency.go +++ b/config/dependency.go @@ -642,8 +642,9 @@ func getTerragruntOutputJson(ctx *ParsingContext, targetConfig string) ([]byte, // First attempt to parse the `remote_state` blocks without parsing/getting dependency outputs. If this is possible, // proceed to routine that fetches remote state directly. Otherwise, fallback to calling `terragrunt output` - // directly. - remoteStateTGConfig, err := PartialParseConfigFile(ctx.WithDecodeList(RemoteStateBlock, TerragruntFlags), targetConfig, nil) + // directly. We need to suspend logging diagnostic errors on this attempt. + parseOptions := append(ctx.ParserOptions, hclparse.WithDiagnosticsWriter(io.Discard, true)) + remoteStateTGConfig, err := PartialParseConfigFile(ctx.WithParseOption(parseOptions).WithDecodeList(RemoteStateBlock, TerragruntFlags), targetConfig, nil) if err != nil || !canGetRemoteState(remoteStateTGConfig.RemoteState) { ctx.TerragruntOptions.Logger.Debugf("Could not parse remote_state block from target config %s", targetConfig) ctx.TerragruntOptions.Logger.Debugf("Falling back to terragrunt output.") diff --git a/config/hclparse/options.go b/config/hclparse/options.go index e8a4843af..67e9bae01 100644 --- a/config/hclparse/options.go +++ b/config/hclparse/options.go @@ -1,19 +1,20 @@ package hclparse import ( + "io" + "github.com/gruntwork-io/go-commons/errors" "github.com/gruntwork-io/terragrunt/util" "github.com/hashicorp/hcl/v2" - "github.com/sirupsen/logrus" ) type Option func(*Parser) *Parser -func WithLogger(logger *logrus.Entry, disableColor bool) Option { +func WithDiagnosticsWriter(writer io.Writer, disableColor bool) Option { return func(parser *Parser) *Parser { - diagsWriter := util.GetDiagnosticsWriter(logger, parser.Parser, disableColor) + diagsWriter := util.GetDiagnosticsWriter(writer, parser.Parser, disableColor) - parser.loggerFunc = func(diags hcl.Diagnostics) error { + parser.diagsWriterFunc = func(diags hcl.Diagnostics) error { if !diags.HasErrors() { return nil } diff --git a/config/hclparse/parser.go b/config/hclparse/parser.go index e0c28644d..7d8ab8313 100644 --- a/config/hclparse/parser.go +++ b/config/hclparse/parser.go @@ -15,7 +15,7 @@ import ( type Parser struct { *hclparse.Parser - loggerFunc func(hcl.Diagnostics) error + diagsWriterFunc func(hcl.Diagnostics) error handleDiagnosticsFunc func(*File, hcl.Diagnostics) (hcl.Diagnostics, error) fileUpdateHandlerFunc func(*File) error } @@ -95,7 +95,7 @@ func (parser *Parser) handleDiagnostics(file *File, diags hcl.Diagnostics) error } } - if fn := parser.loggerFunc; fn != nil { + if fn := parser.diagsWriterFunc; fn != nil { if err := fn(diags); err != nil { return err } diff --git a/util/logger.go b/util/logger.go index 2a855e235..31f9162cd 100644 --- a/util/logger.go +++ b/util/logger.go @@ -96,14 +96,13 @@ func CreateLogEntryWithWriter(writer io.Writer, prefix string, level logrus.Leve } // GetDiagnosticsWriter returns a hcl2 parsing diagnostics emitter for the current terminal. -func GetDiagnosticsWriter(logger *logrus.Entry, parser *hclparse.Parser, disableColor bool) hcl.DiagnosticWriter { +func GetDiagnosticsWriter(writer io.Writer, parser *hclparse.Parser, disableColor bool) hcl.DiagnosticWriter { termColor := !disableColor && term.IsTerminal(int(os.Stderr.Fd())) termWidth, _, err := term.GetSize(int(os.Stdout.Fd())) if err != nil { termWidth = 80 } - var writer = LogWriter{Logger: logger, Level: logrus.ErrorLevel} - return hcl.NewDiagnosticTextWriter(&writer, parser.Files(), uint(termWidth), termColor) + return hcl.NewDiagnosticTextWriter(writer, parser.Files(), uint(termWidth), termColor) } // GetDefaultLogLevel returns the default log level to use. The log level is resolved based on the environment variable