Skip to content

Commit

Permalink
flush deferred warnings before writing to stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Mar 5, 2021
1 parent ea3242a commit 15b36c5
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
59 changes: 41 additions & 18 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ const defaultTerminalWidth = 80
type Log struct {
AddMsg func(Msg)
HasErrors func() bool
Done func() []Msg

// This is called after the build has finished but before writing to stdout.
// It exists to ensure that deferred warning messages end up in the terminal
// before the data written to stdout.
AlmostDone func()

Done func() []Msg
}

type LogLevel int8
Expand Down Expand Up @@ -351,6 +357,31 @@ func NewStderrLog(options OutputOptions) Log {
remainingMessagesBeforeLimit = 0x7FFFFFFF
}
var deferredWarnings []Msg
didFinalizeLog := false

finalizeLog := func() {
if didFinalizeLog {
return
}
didFinalizeLog = true

// Print the deferred warning now if there was no error after all
for remainingMessagesBeforeLimit > 0 && len(deferredWarnings) > 0 {
shownWarnings++
writeStringWithColor(os.Stderr, deferredWarnings[0].String(options, terminalInfo))
deferredWarnings = deferredWarnings[1:]
remainingMessagesBeforeLimit--
}

// Print out a summary
if options.MessageLimit > 0 && errors+warnings > options.MessageLimit {
writeStringWithColor(os.Stderr, fmt.Sprintf("%s shown (disable the message limit with --error-limit=0)\n",
errorAndWarningSummary(errors, warnings, shownErrors, shownWarnings)))
} else if options.LogLevel <= LevelInfo && (warnings != 0 || errors != 0) {
writeStringWithColor(os.Stderr, fmt.Sprintf("%s\n",
errorAndWarningSummary(errors, warnings, shownErrors, shownWarnings)))
}
}

switch options.Color {
case ColorNever:
Expand Down Expand Up @@ -411,27 +442,17 @@ func NewStderrLog(options OutputOptions) Log {
defer mutex.Unlock()
return hasErrors
},
Done: func() []Msg {
AlmostDone: func() {
mutex.Lock()
defer mutex.Unlock()

// Print the deferred warning now if there was no error after all
for remainingMessagesBeforeLimit > 0 && len(deferredWarnings) > 0 {
shownWarnings++
writeStringWithColor(os.Stderr, deferredWarnings[0].String(options, terminalInfo))
deferredWarnings = deferredWarnings[1:]
remainingMessagesBeforeLimit--
}

// Print out a summary
if options.MessageLimit > 0 && errors+warnings > options.MessageLimit {
writeStringWithColor(os.Stderr, fmt.Sprintf("%s shown (disable the message limit with --error-limit=0)\n",
errorAndWarningSummary(errors, warnings, shownErrors, shownWarnings)))
} else if options.LogLevel <= LevelInfo && (warnings != 0 || errors != 0) {
writeStringWithColor(os.Stderr, fmt.Sprintf("%s\n",
errorAndWarningSummary(errors, warnings, shownErrors, shownWarnings)))
}
finalizeLog()
},
Done: func() []Msg {
mutex.Lock()
defer mutex.Unlock()

finalizeLog()
sort.Stable(msgs)
return msgs
},
Expand Down Expand Up @@ -759,6 +780,8 @@ func NewDeferLog() Log {
defer mutex.Unlock()
return hasErrors
},
AlmostDone: func() {
},
Done: func() []Msg {
mutex.Lock()
defer mutex.Unlock()
Expand Down
3 changes: 3 additions & 0 deletions pkg/api/api_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,9 @@ func rebuildImpl(

// Stop now if there were errors
if !log.HasErrors() {
// Flush any deferred warnings now
log.AlmostDone()

if buildOpts.Write {
// Special-case writing to stdout
if options.WriteToStdout {
Expand Down

0 comments on commit 15b36c5

Please sign in to comment.