Skip to content

Commit

Permalink
fix: diags error when parse remote_state (#3268)
Browse files Browse the repository at this point in the history
  • Loading branch information
levkohimins committed Jul 16, 2024
1 parent a7948a6 commit a2f630e
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 13 deletions.
4 changes: 3 additions & 1 deletion cli/commands/hclfmt/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
Expand Down
5 changes: 3 additions & 2 deletions config/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
9 changes: 5 additions & 4 deletions config/hclparse/options.go
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions config/hclparse/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 2 additions & 3 deletions util/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a2f630e

Please sign in to comment.