rust-lang / rust-lang/rust

LineWriter is broken

Open
#148,098 7 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-docs C-bug T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Summary

std::io::LineWriter presents the inner writer with partial lines.

The behaviour is bizarre. It is also contrary to a careful reading of the (rather poor) documentation, and makes it unsuitable for most intended use cases.

Steps

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=ad71a92706e77761dcefef3658e6b12c

use std::io::{self, LineWriter, Write as _};

struct LineCallWriter(LineWriter<EprintlnDebugWriter>);
impl LineCallWriter {
    pub fn new() -> Self {
        LineCallWriter(LineWriter::new(EprintlnDebugWriter))
    }
}
struct EprintlnDebugWriter;
impl io::Write for LineCallWriter {
    fn write(&mut self, i: &[u8]) -> io::Result<usize> {
        eprintln!("I {:?}", String::from_utf8_lossy(i));
        self.0.write(i)
    }
    fn flush(&mut self) -> io::Result<()> {
        eprintln!("I FLUSH");
        Ok(())
    }
}
impl Drop for LineCallWriter {
    fn drop(&mut self) {
        eprintln!("W DROP");
    }
}
impl io::Write for EprintlnDebugWriter {
    fn write(&mut self, i: &[u8]) -> io::Result<usize> {
        eprintln!("W {:?}", String::from_utf8_lossy(i));
        Ok(i.len())
    }
    fn flush(&mut self) -> io::Result<()> {
        eprintln!("W FLUSH");
        Ok(())
    }
}

fn main() -> Result<(), io::Error> {
    let mut lcw = LineCallWriter::new();
    write!(lcw, "one line at a go, ok\n")?;
    write!(lcw, "in parts, ")?;
    write!(lcw, "bad.\n")?;
    write!(lcw, "multiple lines\nall in one go\n")?;
    write!(lcw, "multiple lines\nwith last one\nsplit, ")?;
    write!(lcw, "wow!\n")?;
    Ok(())
}
Actual output
I "one line at a go, ok\n"
W "one line at a go, ok\n"
I "in parts, "
I "bad.\n"
W "in parts, "
W "bad.\n"
I "multiple lines\nall in one go\n"
W "multiple lines\nall in one go\n"
I "multiple lines\nwith last one\nsplit, "
W "multiple lines\nwith last one\n"
I "wow!\n"
W "split, "
W "wow!\n"
W DROP
Expected output
I "one line at a go, ok\n"
W "one line at a go, ok\n"
I "in parts, "
I "bad.\n"
W "in parts, bad.\n"
I "multiple lines\nall in one go\n"
W "multiple lines\nall in one go\n"
I "multiple lines\nwith last one\nsplit, "
W "multiple lines\nwith last one\n"
I "wow!\n"
W "split, wow!\n"
W DROP
Discussion

The documentation is rather vague, but it does seem to promise that the inner writer will only see whole lines (except at the end). "This batched write" implies a single write to the inner writer.

IMO:

  • Writing multiple lines in one write to the inner writer is perhaps OK. An inner writer that wants to split them can do so easily enough. On the other hand, some use cases would find it more helpful to receive one line at a time. Anyway, the behaviour should be documented.
  • Each write should end with a newline, except during drop and maybe flush. The documentation should promise this explicitly.
  • The behaviour on flush should be documented.
Meta

I saw this with this compiler, but the latest stable on the playground does just the same.

rustc --version --verbose:

rustc 1.85.0 (4d91de4e4 2025-02-17)
binary: rustc
commit-hash: 4d91de4e48198da2e33413efdcd9cd2cc0c46688
commit-date: 2025-02-17
host: x86_64-unknown-linux-gnu
release: 1.85.0
LLVM version: 19.1.7

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with std::io::LineWriter and the linked Rust playground reproducer, then inspect its existing implementation and tests. Compare writes split across calls, multi-line writes, and final partial lines against the expected output; completion also requires settling and documenting flush and drop behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.