The carriage return that breaks the deploy, not the diff
We write this site's code on Windows. It runs on Linux. Between the two sits the oldest, quietest landmine in cross-platform work: the carriage return.
A file written on Windows ends each line with \r\n. Read by Linux
tools, every line carries a stray \r. No diff viewer renders it, no
editor shows it by default, and no code review catches it. The byte is
invisible, so the diff looks identical. Hence the title: the CR doesn't break the diff. It
breaks the deploy.
Our defense is 35 lines of .gitattributes that pin LF endings on the
files the server executes. The fix is right. This note tests the
reasoning behind it, because part of that reasoning held up on a real
Linux machine, and part of it didn't.
The accident being prevented
Before those rules, nothing in the repo enforced line endings. The
deploy files (shell scripts, the systemd unit, the nginx config) were
stored with LF only because whoever committed them had
core.autocrlf=true set. That's an accident of one machine's git
config. A contributor without it would commit CRLF files, and the server
would run them.
The exposure is real. Counted on the Windows dev machine, with the rules in place:
- 53 text files are CRLF on disk: every text file without a rule,
converted at checkout by
core.autocrlf=true. - 71 files are LF on disk: the pinned set, plus files written by LF-only tools since they were last checked out.
The rules make that split deliberate. The set of files that's LF on a Windows disk is now the set the server executes. Before the rules, that match was luck.
You can watch git doing this work. While we wrote this note, two files
ended up with mixed endings after some edits, and git normalized them
on commit. The only thing that showed it happening was
git ls-files --eol.
What the CR actually does, tool by tool
The reasoning we wrote down for the rules named three ways a CR would break things: a script's shebang line, a GitHub Actions step, and systemd, with nginx "similarly unforgiving". Each claim is testable, so we tested each one on real Linux (Ubuntu under WSL2, systemd 255, nginx) with byte-exact files.
The results sort into a spectrum, and the spectrum is the point: the same bytes are harmless or fatal depending on who reads them.
Fatal: the shebang
The kernel parses #! itself, inside execve, before bash exists to be
lenient. It reads the interpreter path up to the LF, so a CRLF script's
interpreter is /bin/bash\r. execve returns ENOENT for a file that
differs from /bin/bash by one invisible byte. The classic message is
the famous one:
/bin/bash^M: bad interpreter: No such file or directory
Modern bash words the same ENOENT as cannot execute: required file not
found, which is arguably worse, since nothing in that sentence suggests
line endings. Confirmed, exactly as claimed.
Chaos: the bash body, as in an Actions run: block
GitHub Actions writes each run: block to a file and executes it with
bash -eo pipefail. We ran a fully CRLF script under those flags. What
happens isn't one failure but a cascade:
- A bare word at the end of a line becomes a command name with a CR on
it.
false\risn't the builtinfalse. It's a lookup for a command that doesn't exist:command not found, status 127. set +e\rprintsset: +: invalid option, and then returns 0 and still disables errexit. The+eis applied before the parser chokes on the CR. The line meant to turn errexit off does turn it off, while printing an error that makes you think it failed entirely.case "$status" in\ris a syntax error (bash reports the token as$'in\r'), and the script aborts there.- The abort's exit status is whatever the last completed command left
behind. The last completed command was the broken
set, which returned - The script dies mid-work and exits 0.
Then we ran a real step that way: the one in our weekly job that checks
whether this site's vendored copy of pforex, our open-source currency
library, has drifted from upstream. The check runs, and status picks up
a CR in its value (0\r). We had expected the variable's name to end
in a carriage return. Measured, the name never gets one; the value does.
Then the case statement that would write drifted=true or
drifted=false to $GITHUB_OUTPUT never runs at all.
The step is green. The ::error:: line never fires. The next step,
gated on steps.check.outputs.drifted == 'true', compares an empty
string and quietly skips. The weekly job does nothing, says nothing, and
the log shows three errors on a green step.
We had predicted this would fail "in a way that looks like a logic bug rather than an encoding one". Measured, it's worse: it fails in a way that looks like success. That's the five silent failures class again: a safety check whose own failure mode is silence.
Tolerated: systemd 255, and nginx
We had said systemd units were "similarly unforgiving". On the systemd
our deploy platform ships (255, on Ubuntu LTS), a CRLF unit starts
fine. The parser strips the CR as trailing whitespace. ExecStart runs
clean, and an EnvironmentFile with CRLF endings delivers FOO=bar to
the process with no CR in it. nginx -t on a CRLF config: successful.
The pin for *.service and *.conf is still correct. Rules are cheap,
older parsers exist, and defense in depth costs nothing here. But the
failure we described doesn't happen on the platform this repo deploys
to, and that distinction is the point of this note.
Harmless, and drift-sensitive
This site reads its notes with open(path, encoding='utf-8'). That's
text mode, so Python's universal newlines turn \r\n into \n on the
way in. The notes are deliberately not pinned, because a rule there
would only cause churn. Same for the Python sources, the Svelte
components and the stylesheets: every tool that reads them normalizes or
ignores the byte.
The vendored pforex file is the special case, and the subtlest. It must stay byte-identical to upstream. Its checker reads with universal newlines, so a CRLF copy would compare equal and report "in sync" while the bytes the site serves differ from upstream. That's the one file in the repo with an exact-bytes requirement. The checker's tolerance is itself a small silence. The pin closes it: the file's line endings stop depending on how each machine happens to check it out.
Targeted, not blanket
The obvious alternative was * text=auto eol=lf: one line, total
coverage. We rejected it on purpose, and the numbers above show why. A
blanket rule rewrites the whole working tree the moment it lands. On this
machine that's 53 files of churn in one commit, plus a permanent claim on
files nobody executes (the notes, a marketing doc, the Svelte sources),
none of which need protecting.
The targeted rules name only what the server runs:
*.sh text eol=lf
*.service text eol=lf
*.conf text eol=lf
*.yml text eol=lf
*.yaml text eol=lf
Plus one line each for the production env file and the vendored library. Two sets now coincide, LF on disk and what the server executes, and nothing else is touched.
git add --renormalize . proved the rules only bind the future. It
staged nothing but .gitattributes itself, because every file was
already stored with LF. Thirty-five lines, zero churn, and they prevent
an accident that's one git config away on any contributor's machine.
Test the reasons, not just the fix
The fix was right. Half its reasoning was folklore: "systemd and nginx
are similarly unforgiving" doesn't survive a systemd-analyze and an
nginx -t on the platform that runs this site.
That's not a knock on the fix. It's the normal condition of preventive work. A preventive fix is justified by claims about failures that haven't happened yet, and claims about failures are testable. This note is the test.
The rules stay. The shebang alone would justify them, and so would the green step with missing outputs. But next time a defense comes with three failure modes, the honest count is however many survive an attempt to reproduce them.
Three notes on failures whose only signature is silence:
- Five ways to fail without an error message: five bugs, one fix, and not one error message between them.
- The gitignore that ate a source file: the silence that was manufactured. The tool that would warn you is the one told not to look.
- The carriage return that breaks the deploy, not the diff (this note): the invisible byte, harmless or fatal depending on who reads it.