diff --git a/cli/azd/pkg/input/console.go b/cli/azd/pkg/input/console.go index d93d3643c44..b5cfcb444ad 100644 --- a/cli/azd/pkg/input/console.go +++ b/cli/azd/pkg/input/console.go @@ -218,6 +218,12 @@ func (c *AskerConsole) IsUnformatted() bool { func (c *AskerConsole) Message(ctx context.Context, message string) { // Disable output when formatting is enabled if c.formatter != nil && c.formatter.Kind() == output.JsonFormat { + // Empty messages are visual separators (blank lines) in text mode. + // In JSON mode they have no semantic value, so skip them. + if message == "" { + return + } + // we call json.Marshal directly, because the formatter marshalls using indentation, and we would prefer // these objects be written on a single line. var obj any = output.EventForMessage(message) diff --git a/cli/azd/pkg/input/console_test.go b/cli/azd/pkg/input/console_test.go index 48b9e595b01..c89789cc1ec 100644 --- a/cli/azd/pkg/input/console_test.go +++ b/cli/azd/pkg/input/console_test.go @@ -368,6 +368,34 @@ func TestAskerConsole_Message_InvalidQuery_FallsBack(t *testing.T) { "invalid query should fall back to full envelope") } +func TestAskerConsole_Message_EmptySkippedInJson(t *testing.T) { + buf := &strings.Builder{} + formatter := &output.JsonFormatter{} + + c := NewConsole( + true, + false, + false, + Writers{Output: writerAdapter{buf}}, + ConsoleHandles{ + Stderr: os.Stderr, + Stdin: os.Stdin, + Stdout: writerAdapter{buf}, + }, + formatter, + nil, + ) + + // An empty message should produce no JSON output (it's just a visual separator in text mode) + c.Message(context.Background(), "") + require.Empty(t, buf.String(), "empty message should not emit any JSON output") + + // A non-empty message should still produce JSON output + c.Message(context.Background(), "hello") + require.NotEmpty(t, buf.String(), "non-empty message should emit JSON output") + require.Contains(t, buf.String(), `"consoleMessage"`) +} + // writerAdapter wraps *strings.Builder to satisfy io.Writer for test purposes. type writerAdapter struct { *strings.Builder