This blog's package.json lists twenty packages, and one of them sits on the
postinstall hook:

"scripts": { "postinstall": "patch-package" } 

There is a single file in patches/: rettiwt-api+7.0.3.patch, 8,971 bytes, five
hunks spread across five files. It fixes the library's compiled output so I can
upload short videos to X. Both the hook and the patch landed in the repo on
11 June 2026 (ed32072f), and they have run on every npm ci since, with nobody
watching. Going back to look at them, one question stuck with me: does that
applier tell me where it put the patch?

The rig that answers it is small, and it led somewhere more interesting than
expected. One patch, four appliers: two of them wrote it over a block that merely
resembled the one it was aimed at, the file stayed valid, and the exit code came
back zero. The real surprise arrived afterwards. This behaviour isn't waiting to
be discovered by anyone. It is written down, word for word, under the BUGS heading
of patch's own manual.

The rig: two similar blocks, one drifted target

Applying a patch is really a search problem. The patch says "find these lines,
change the one in the middle"; if the file has moved on since, the applier guesses.
To make that guess visible, the plainest possible file will do:

cat > pools.conf <<'EOF' # primary pool label = pool-a max = 64 retries = 3 timeout = 30 label_end = pool-a # end primary pool # backup pool label = pool-b max = 64 retries = 3 timeout = 30 label_end = pool-b # end backup pool EOF 

Two pools, nearly identical. The patch raises the primary pool's retries
from 3 to 9, and diff -u generates it with the default three lines of context:

--- pools.conf +++ pools.conf.new @@ -1,7 +1,7 @@ # primary pool label = pool-a max = 64 -retries = 3 +retries = 9 timeout = 30 label_end = pool-a # end primary pool 

Eleven lines. Now imitate real life: after the patch was written, the primary pool
gets renamed and grows. The labels become pool-main, max goes to 128, the
header line becomes # main pool. The backup pool is left alone — max = 64 and
retries = 3 included.

The patch can no longer find its target. But there is a wide gap between "cannot
find" and "will not look", and that gap is the whole story.

Four appliers, four policies

On the Mac in front of me (macOS 26.6.2, arm64) there were four appliers. Three ship
with the system or arrive with one command; the fourth already runs in my own repo:

/usr/bin/patch --version # patch 2.0-12u11-Apple brew install gpatch gpatch --version # GNU patch 2.8.1 git --version # git version 2.48.1 node -p "require('patch-package/package.json').version" # 8.0.1 

The target file was reset before every run and each applier got the same patch,
with LC_ALL=C so the output stays comparable. The intent was to change line 4.

Applier Exit code Line changed What it printed
/usr/bin/patch (default) 0 12 one line, no warning
/usr/bin/patch --verbose 0 12 with fuzz 2 (offset 8 lines)
/usr/bin/patch -F0 1 — hunk rejected, .rej written
gpatch 2.8.1 (default) 0 12 with fuzz 2 (offset 8 lines)
gpatch -F0 1 — Hunk #1 FAILED at 1.
git apply (default) 1 — patch does not apply
git apply -C1 0 12 Context reduced to (1/1)
patch-package 8.0.1 — — could not apply the hunk, threw

Four runs changed line 12. Line 12 is the backup pool's retries value. In
production that reads as: the patch you committed as "raised the primary pool's
retry count" goes off and tunes the backup pool. Valid file, wrong behaviour.

The bug is already on record — on this very machine

At this point it seemed strange that a trap this clean had never been discussed.
It has been. And not on some distant GNU page: on this Mac's own disk.
man -w patch → /usr/share/man/man1/patch.1, roff header Copyright 1986,
Larry Wall
, page dated 27 April 2022. BUGS section, line 532:

If code has been duplicated (for instance with #ifdef OLDCODE ... #else ...

endif), patch is incapable of patching both versions, and, if it works at all,

will likely patch the wrong one, and tell you that it succeeded to boot.

"Writes to the wrong one of two lookalike blocks and reports success" sits in the
tool's own documentation as a known bug. The rig didn't discover anything; it
reproduced something already filed.

Line 355 of the same page is worse, because it makes a promise:

You will also be told if a fuzz factor was used to make the match, in which case
you should also be slightly suspicious.

"You will be told." It didn't tell me. On the default run /usr/bin/patch printed
one line: patching file pools.conf. The binary knows what it knows, though — with
--verbose it prints the same sentence GNU does, word for word:

Patching file pools.conf using Plan A... Hunk #1 succeeded at 9 with fuzz 2 (offset 8 lines). 

Here is the funny part: that page has no --verbose option at all. Its -v is
short for --version. The binary accepts the flag, but its own manual never
mentions it. The one route the page does offer sits in the ENVIRONMENT section:

PATCH_VERBOSE — Specifies the default verbosity of patch. Setting PATCH_VERBOSE
to a non-zero integer will enable verbosity.

It works: PATCH_VERBOSE=1 patch pools.conf retry.patch prints the fuzz line. So
on a Mac, keeping the promise costs one line in your shell profile. For comparison
I checked GNU 2.8.1's page too, where the same warning is described as default
behaviour and --verbose's job is specified as announcing "hunks that match
exactly." Both manuals promise the same thing; one keeps it, the other leaves it
waiting behind an environment variable.

Why fuzz 2 lands on the twin

The mechanism is spelled out on that same page, in the same detail. The -F
max-fuzz
entry says it causes patch to ignore "up to that many lines in looking
for places to install a hunk", then adds two sentences: "Note that a larger fuzz
factor increases the odds of a faulty patch." and "The default fuzz factor is 2."
The DESCRIPTION section walks through the scan: first it looks for a place where
every context line matches; failing that, it rescans ignoring the first and last
context line; failing that, it skips the first two and last two.

My hunk has three lines of context on each side. With fuzz 2, two go from each end;
the remaining match criteria are max = 64 and timeout = 30. In the primary pool
max is now 128, so no match. In the backup pool it is still 64, so it matches.
The rest is arithmetic.

How far will patch look? The first guess is the line number in the hunk, and if
that fails, in the page's words, "patch will scan both forwards and backwards." A
maximum scan distance appears nowhere. When something isn't documented, measure it,
so the twin block went to the bottom of the file:

# second block sits behind 100,000 lines of filler gpatch t.conf t.patch # Hunk #1 succeeded at 100009 with fuzz 2 (offset 100008 lines). 

A hundred thousand lines away. Four different distances, no ceiling in sight; the
search ends when the file does.

Diagram

Why git apply refuses

git apply refused the same patch, and refused it without touching the file:

error: patch failed: pools.conf:1 error: pools.conf: patch does not apply 

The reason shows up as a subordinate clause under the -C<n> option in the official
docs: "By default no context is ever ignored." Git has no concept of fuzz. The source
agrees: the word "fuzz" does appear in apply.c, but only in fuzzy_matchlines,
which tolerates whitespace differences. Reducing context is governed by p_context,
and init_apply_state initialises it to UINT_MAX, so by default the reduction
loop breaks immediately.

Passing -C1 opens that door yourself. The option means "ensure at least n lines
of surrounding context match before and after each change", so -C1 grants git
permission to trim context down to a single line. The result is predictable (the
last two lines need --verbose; -C1 alone prints only the middle one):

$ git apply -p0 -C1 --verbose retry.patch Checking patch pools.conf... Hunk #1 succeeded at 11 (offset 12 lines). Context reduced to (1/1) to apply fragment at 11 Applied patch pools.conf cleanly. 

You'll appreciate how optimistic the word "cleanly" looks there.

The hunk nailed to line 1

Git has an asymmetry I ran into by accident. Without disturbing the context at all,
the block moved 50 lines down, intact. GNU patch applied it without complaint:

Hunk #1 succeeded at 51 (offset 50 lines). 

git apply refused — even though the context sits right there, byte for byte. The
reason is this decision in apply.c:

match_beginning = (!frag->oldpos || (frag->oldpos == 1 && !state->unidiff_zero)); 

If the hunk header reads @@ -1,7, meaning the patch starts at the file's first
line, git nails that hunk to the beginning of the file and will not slide it. Moving
the same block three lines down and regenerating the patch with an @@ -4,7 header
was enough; the target file in that run is named target.conf:

$ git apply -p0 --verbose b.patch Checking patch target.conf... Hunk #1 succeeded at 54 (offset 50 lines). Applied patch target.conf cleanly. 

Same file, same change, same drift. The only difference is whether the hunk starts
at the top of the file. There is no rule saying "git apply never slides a hunk";
there is a rule saying "git apply never slides a hunk nailed to line 1."

The applier that runs in my CI

The fourth one — the one running on every install via my own postinstall — strikes
a different balance. Version 8.0.1's applier has a variable called fuzzingOffset,
but that is a line shift rather than context fuzz: it widens outward as 0, −1, +1,
−2, +2, and stops at this line:

if (Math.abs(fuzzingOffset) > 20) { /* cannot apply hunk -> error */ } 

Twenty lines. This time the limit is written in the code rather than a manual. A
small runner that calls the package's own dist/patch/ modules, sliding the block
down a step at a time, gives this:

Shift Result
0 · 5 · 19 · 20 lines applied
21 · 25 · 100 lines Cannot apply hunk 0 for file …

On context it is as strict as git: every context line has to match. Its one bit of
give is trailing whitespace, because the comparison function runs trimRight — with
three spaces appended to a context line, the patch applied without complaint.

Keeping the context still isn't protection. With the twin block made byte-identical
and placed inside that twenty-line window, patch-package wrote to the wrong block
as well:

Distance to twin block Result
0 · 3 · 10 · 13 lines applied to the twin, silently
14 · 20 lines threw

The cut-off at 13 is consistent with the window: the twin block starts after N lines
of filler and the hunk expects line 2, so the shift required is 7 + N. At N = 13
that is exactly 20; at N = 14 it is 21. A narrower window shortens the range without
changing the kind of danger.

A practical recipe

Seeing these four appliers on one table changed two of my habits: patches now go
through git apply rather than patch, and after applying one I look at the file
instead of the exit code. The checklist I'm left with:

  • Don't treat the exit code as evidence. The manual is explicit: 0 means "all hunks are applied successfully". Where they were applied is a separate question, and the file answers it.
  • Reach for git apply first when applying a patch by hand. If the context doesn't match, it stops. Inside a repo, with blob identities recorded in the patch, git apply --3way is better still: in my rig it refused to guess and left conflict markers instead. It asked me rather than fixing the wrong file.
  • If you must use patch, write -F0. That turns fuzz off, a non-matching hunk lands in a .rej file, and the exit code becomes 1. A rejected hunk beats a misplaced one by a margin not worth measuring.
  • Never use --dry-run without --verbose — on macOS's patch, at least. The dry run leaves the file alone, but at default verbosity it won't tell you where the hunk would have landed either.
  • On a Mac, make PATCH_VERBOSE=1 permanent. The variable is documented on the man page and beats remembering a flag: one line in your shell profile and every patch run starts reporting offset and fuzz again. The only cost is a few extra lines of output.
  • If you use patch-package, turn the flag on. Its README says --error-on-fail "Forces patch-package to exit with code 1 after failing", and notes the flag is already on under CI. In the installed 8.0.1, dist/index.js decides it here:
 const shouldExitWithError = !!argv["error-on-fail"] || (process_1.default.env.NODE_ENV === "production" && ci_info_1.isCI) || (ci_info_1.isCI && !process_1.default.env.PATCH_PACKAGE_INTEGRATION_TEST) || process_1.default.env.NODE_ENV === "test"; 

Exit 1 on CI, not locally. So on my own machine npm ci runs to completion with
an unpatched library and the red line can get lost in the install output. Writing
"postinstall": "patch-package --error-on-fail" closes that gap, and it is going
into my own package.json after this piece.

  • If you plan to read the logs, mind the stream and the language. patch writes its warnings to standard output, git apply writes them to standard error; one escapes 2>/dev/null, the other escapes | grep. Streams play their own games besides — I've chased when output entering a pipe actually appears before. The text isn't fixed either. On this Mac LANG and every LC_* variable are empty and locale reports LC_MESSAGES="C", yet git apply prints its messages in Turkish, because it takes the language from the system preference (defaults read -g AppleLanguages → ("tr-TR", "en-TR")). In the same session, Homebrew's gpatch stayed English under the tr, de, fr and es locales; the binary has no libintl linked. A CI gate built on grep "with fuzz" quietly goes blind depending on whose machine it runs on. LC_ALL=C first, then grep.
  • Look at the version once. macOS's patch introduces itself as 2.0-12u11-Apple; GNU's current release is 2.8.1. That release shipped on 23 October 2026 to prevent out-of-tree files from being created: according to the announcement, reverse-applying a patch that deletes a file didn't check whether the pathname was safe (CVE-2026-115207). If you're thinking about feeding an untrusted patch file to patch, it's worth remembering that git apply rejects patches reaching outside the working area by default.

Who tells you about a documented bug

There isn't much to worry about with patches/rettiwt-api+7.0.3.patch specifically:
patch-package doesn't drop context, and the library version is pinned in the patch's
filename. But that guarantee comes from the package maintainer's choices rather than
from any caution of mine. If tomorrow I wrote a shell script applying those five
hunks with patch -p1, the same patch could quietly settle in the wrong place and
all I would see is the line patching file ....

What stays with me isn't that tools behave differently; of course they do. It's
that the warning was never missing. "You will also be told" sat on a page carrying
a 1986 copyright line, on my own machine, as close as typing man patch. The only
thing missing was that promise being kept at the moment the command ran — and
PATCH_VERBOSE, the switch that keeps it, was written on the same page.

That gap between documentation and behaviour isn't hiding anywhere; it just sits
where nobody reads. What usually builds the habit of reading is having changed the
wrong block once. This time the looking came first.

Official Sources