diff --git a/command/format/diagnostic.go b/command/format/diagnostic.go index fa7584a4b..6ceb30894 100644 --- a/command/format/diagnostic.go +++ b/command/format/diagnostic.go @@ -175,11 +175,17 @@ func Diagnostic(diag tfdiags.Diagnostic, sources map[string][]byte, color *color } if desc.Detail != "" { - detail := desc.Detail if width != 0 { - detail = wordwrap.WrapString(detail, uint(width)) + lines := strings.Split(desc.Detail, "\n") + for _, line := range lines { + if !strings.HasPrefix(line, " ") { + line = wordwrap.WrapString(line, uint(width)) + } + fmt.Fprintf(&buf, "%s\n", line) + } + } else { + fmt.Fprintf(&buf, "%s\n", desc.Detail) } - fmt.Fprintf(&buf, "%s\n", detail) } return buf.String() diff --git a/command/format/diagnostic_test.go b/command/format/diagnostic_test.go index 98b879d38..2062f633c 100644 --- a/command/format/diagnostic_test.go +++ b/command/format/diagnostic_test.go @@ -167,3 +167,35 @@ Error: Some error t.Fatalf("unexpected output: got:\n%s\nwant\n%s\n", output, expected) } } + +func TestDiagnostic_wrapDetailIncludingCommand(t *testing.T) { + var diags tfdiags.Diagnostics + + diags = diags.Append(&hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: "Everything went wrong", + Detail: "This is a very long sentence about whatever went wrong which is supposed to wrap onto multiple lines. Thank-you very much for listening.\n\nTo fix this, run this very long command:\n terraform read-my-mind -please -thanks -but-do-not-wrap-this-line-because-it-is-prefixed-with-spaces\n\nHere is a coda which is also long enough to wrap and so it should eventually make it onto multiple lines. THE END", + }) + color := &colorstring.Colorize{ + Colors: colorstring.DefaultColors, + Reset: true, + Disable: true, + } + expected := ` +Error: Everything went wrong + +This is a very long sentence about whatever went wrong which is supposed to +wrap onto multiple lines. Thank-you very much for listening. + +To fix this, run this very long command: + terraform read-my-mind -please -thanks -but-do-not-wrap-this-line-because-it-is-prefixed-with-spaces + +Here is a coda which is also long enough to wrap and so it should eventually +make it onto multiple lines. THE END +` + output := Diagnostic(diags[0], nil, color, 76) + + if output != expected { + t.Fatalf("unexpected output: got:\n%s\nwant\n%s\n", output, expected) + } +}