Skip to content

diff - -u prints a - line after + lines when the last line differs only by its newline #405

Description

@sylvestre

When the last line of both files has the same text but only one of them ends with a
newline, -u must show it as a deletion plus an insertion. uutils appends that - line
at the point where the line is matched, so if the hunk already has insertions before it,
the - line comes out in the middle of the + lines. GNU always prints all deletions of
a change before its insertions. patch still accepts the output, but it differs from
GNU's byte for byte.

The GNU test no-newline-at-eof (second -U1 run) exposes it once
--suppress-blank-empty (separate draft) is accepted; the CI log does not show it yet.
-c and the normal format are not affected.

Reproduction

printf '1\n' > n1; printf '0\n1' > n2
printf '1' > n3; printf '0\n1\n' > n4
touch -d 2026-01-01 n1 n2 n3 n4

Rust (incorrect)

$ TZ=UTC0 ./target/release/diffutils diff -u n1 n2
--- n1	2026-01-01 00:00:00.000000000 +0000
+++ n2	2026-01-01 00:00:00.000000000 +0000
@@ -1 +1,2 @@
+0
-1
+1
\ No newline at end of file
# Exit code: 1
$ TZ=UTC0 ./target/release/diffutils diff -u n3 n4
--- n3	2026-01-01 00:00:00.000000000 +0000
+++ n4	2026-01-01 00:00:00.000000000 +0000
@@ -1 +1,2 @@
+0
-1
\ No newline at end of file
+1
# Exit code: 1

GNU (correct)

$ LC_ALL=C TZ=UTC0 diff -u n1 n2
--- n1	2026-01-01 00:00:00.000000000 +0000
+++ n2	2026-01-01 00:00:00.000000000 +0000
@@ -1 +1,2 @@
-1
+0
+1
\ No newline at end of file
# Exit code: 1
$ LC_ALL=C TZ=UTC0 diff -u n3 n4
--- n3	2026-01-01 00:00:00.000000000 +0000
+++ n4	2026-01-01 00:00:00.000000000 +0000
@@ -1 +1,2 @@
-1
\ No newline at end of file
+0
+1
# Exit code: 1

Test

Add to tests/by-util/test_diff.rs:

#[test]
fn unified_missing_newline_deletions_first() -> Result<(), Box<dyn std::error::Error>> {
    let mut a = NamedTempFile::new()?;
    a.write_all(b"1\n")?;
    let mut b = NamedTempFile::new()?;
    b.write_all(b"0\n1")?;
    let mut cmd = cargo_bin_cmd!("diffutils");
    cmd.arg("diff").arg("-u").arg(a.path()).arg(b.path());
    cmd.assert().code(predicate::eq(1)).stdout(predicate::str::ends_with(
        "@@ -1 +1,2 @@\n-1\n+0\n+1\n\\ No newline at end of file\n",
    ));

    let mut c = NamedTempFile::new()?;
    c.write_all(b"1")?;
    let mut d = NamedTempFile::new()?;
    d.write_all(b"0\n1\n")?;
    let mut cmd = cargo_bin_cmd!("diffutils");
    cmd.arg("diff").arg("-u").arg(c.path()).arg(d.path());
    cmd.assert().code(predicate::eq(1)).stdout(predicate::str::ends_with(
        "@@ -1 +1,2 @@\n-1\n\\ No newline at end of file\n+0\n+1\n",
    ));
    Ok(())
}

Exposed by the GNU diffutils test suite: no-newline-at-eof.

Activity

  1. added
    bug
    good first issue
    gnu-compat
    on Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buggnu-compatgood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions