Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Formatting Changes and Bug Fixes

- sqlfmt no longer includes an extra trailing newline when writing formatted code to stdout.
([#729](https://github.com/tconbeer/sqlfmt/issues/729), [#730](https://github.com/tconbeer/sqlfmt/pull/730))

## [0.28.2] - 2025-10-24

### Formatting Changes and Bug Fixes
Expand Down
9 changes: 5 additions & 4 deletions src/sqlfmt/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ def unstyle_output(msg: str) -> str:
return s


def display_output(msg: str, err: bool = True) -> None:
def display_output(msg: str, err: bool = True, nl: bool = True) -> None:
"""
A thin wrapper around click.echo; defaults to printing to stderr.
A thin wrapper around click.echo; defaults to stderr with trailing newline.
"""
click.echo(msg, err=err)
click.echo(msg, err=err, nl=nl)


@dataclass
Expand All @@ -61,9 +61,10 @@ def __post_init__(self) -> None:
def maybe_print_to_stdout(self) -> None:
"""
If sqlfmt received a query via stdin, print the formatted string to stdout
without an additional trailing newline
"""
if self.source_path == STDIN_PATH:
display_output(self.formatted_string, err=False)
display_output(self.formatted_string, err=False, nl=False)

@property
def has_changed(self) -> bool:
Expand Down
4 changes: 2 additions & 2 deletions tests/functional_tests/test_end_to_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ def test_end_to_end_errors(
@pytest.mark.parametrize(
"options,stream_input,expected_stdout,expected_exit",
[
("-", "select 1\n", "select 1\n\n", 0),
("-", "select 1\n", "select 1\n\n", 0),
("-", "select 1\n", "select 1\n", 0), # removes excess space
("-", "select 1\n\n", "select 1\n", 0), # one trailing newline
("- --check", "select 1\n", "", 1),
("- --diff", "select 1\n", "", 1),
("- --check", "select 1\n", "", 0),
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_stdin(sqlfmt_runner: CliRunner) -> None:
stream_input = "select 1"
results = sqlfmt_runner.invoke(sqlfmt_main, args="-", input=stream_input)
assert results.exit_code == 0
assert results.stdout == "select 1\n\n"
assert results.stdout == "select 1\n"


def test_preformatted_check(sqlfmt_runner: CliRunner, preformatted_dir: Path) -> None:
Expand Down