Skip to content

We have zero-line text files that aren't empty #9802

Closed
@philderbeast

Description

@philderbeast

Describe the bug
Investigating #9798, I wanted to find the number of single line project files. In doing this I found some were reported as having 0 lines.

On review, @geekosaur is catching the mistake I'm often making of dropping or forgetting these newlines.

Therefore, “lines” not ending in a newline character aren't considered actual lines. That's why some programs have problems processing the last line of a file if it isn't newline terminated.

The advantage of following this convention is that all POSIX tools expect and use it.
SOURCE: Why should text files end with a newline?

I have a script to add newlines when missing and can use it to fix up some files that don't conform, for instance *.hs and *.project files. I didn't find any non-conforming *.cabal files. There are only two legitimate zero line (empty) projects;

  • ./cabal-install/tests/fixtures/project-root/cabal.project
  • ./cabal-install/tests/fixtures/project-root/nix/cabal.project

To Reproduce

$ find -name '*.project' -exec wc -l {} \; | sort -k 1
0 ./cabal-install/tests/fixtures/project-root/cabal.project
0 ./cabal-install/tests/fixtures/project-root/nix/cabal.project
0 ./cabal-testsuite/PackageTests/ExtraProgPath/cabal.project
0 ./cabal-testsuite/PackageTests/HaddockArgs/cabal.project
0 ./cabal-testsuite/PackageTests/HaddockWarn/cabal.project
0 ./cabal-testsuite/PackageTests/NewBuild/CmdRun/Script/cabal.project
0 ./cabal-testsuite/PackageTests/NewBuild/CmdRun/ScriptLiterate/cabal.project
0 ./cabal-testsuite/PackageTests/NewHaddock/Fails/cabal.project
0 ./cabal-testsuite/PackageTests/NewSdist/DeterministicTrivial/cabal.project
0 ./cabal-testsuite/PackageTests/PkgConfigParse/cabal.project
0 ./cabal-testsuite/PackageTests/Regression/T5782Diamond/cabal.project
0 ./cabal-testsuite/PackageTests/SDist/Respect-Project-File/cabal.project
0 ./cabal-testsuite/PackageTests/SDist/T5195and5349/cabal.project
0 ./cabal-testsuite/PackageTests/WarnEarlyOverwrite/cabal.project
...

$ cat ./cabal-testsuite/PackageTests/WarnEarlyOverwrite/cabal.project
packages: .⏎

Expected behavior
We add the missing newlines (I have a script for this).

#!/bin/bash
# SEE: https://unix.stackexchange.com/questions/31947/how-to-add-a-newline-to-the-end-of-a-file
for f in $(git grep -Il '' -- "$1"); do
    echo "$f";
    tail -c1 < "$f" | read -r _ || echo >> "$f";
done

Stretch goal: We stop relying on @geekosaur spotting these mistakes and add a check for this in CI.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions