DEV Community

Cover image for `false | tail -1` Exits 0: Two Ways My Green Light Ran Somewhere Else
Renhua Wei
Renhua Wei

Posted on

`false | tail -1` Exits 0: Two Ways My Green Light Ran Somewhere Else

The hook printed ok and I believed it for six days. It was a pre-command guard in seasonedcc/seasoned-skills, one of those files that inspects a command before an agent is allowed to run it, and its whole job was to catch a cd into a directory that does not exist. On my machine it said the command was fine. On the box where it actually mattered, the command ran in /workspace — a different git repo — and every reading I took that week came back from a repository I never meant to look at.

The issue that finally made me open the thing had one line I could not get out of my head: Every fetch returned exit 0 with data from the wrong pull request. Exit 0. Data from another PR. Both were true at the same time, and for the life of me I could not see how.

The first thing I assumed, and why it was wrong

My first assumption was that the fetch code was broken — some wrong owner/repo getting parsed out of a config. I nearly shipped a fix for a parsing bug that did not exist. Took me four hours of grepping for where the repo string was read before I stopped and looked at where the command was running.

It was never read wrong. The command that read it was running in the wrong place, because the line before it — a cd into a path that was never cloned on that box — had failed, and nothing downstream cared. cd /workspace/product-app && git rev-parse --show-toplevel on a machine where product-app is not there prints the outer repo's root:

$ cd /workspace/product-app && git rev-parse --show-toplevel
/workspace
$ echo $?
0
Enter fullscreen mode Exit fullscreen mode

The cd failed. The git ran anyway, in /workspace, exited 0, and handed me a perfectly valid answer to a question I had not asked. My guard hook was supposed to stop exactly this, and here is the part that stings: it only checked what the command looked like. It matched the shape of cd X && ..., decided the shape was legal, and let it through. It never checked that X was a directory that existed where the command would actually run.

Two green lights, same lie

While I was chasing that, I walked into the second one, the little shell trap I had read about a dozen times and never once felt in my fingers. The exit code of a pipeline is the exit code of the last command in it, not the one you care about:

$ false | tail -1; echo "pipe_rc=$?"
pipe_rc=0
$ false | tail -1; echo "pipestatus0=${PIPESTATUS[0]}"
pipestatus0=1
Enter fullscreen mode Exit fullscreen mode

false | tail -1 is a green light. set -e will not save you either, because the pipeline as a whole returned 0, and in plenty of shell and sub-shell combinations pipefail is not inherited the way you assume. So any gate shaped like some-check | grep OK is decoration. I had one of those too. Silly mistake, but it was mine, and it had been sitting in my head as "known" for years without ever costing me anything until that week.

Both bugs are the same bug in different clothes. One gate asked "does this command look like a cd into a real path" and never asked "is that path there when it runs." The other asked "did the pipeline succeed" and never asked "did the command inside it succeed." Green in both cases, wrong in both cases.

What actually made it fail

For the hook, the fix was to stop matching text and start resolving the target. If the first-line cd target can be read from the command and it is not an existing directory, exit 2 and name the missing path, and say plainly that nothing ran. Anything the shell has to expand — $VAR, $(...), backticks, globs — gets a pass, because you cannot judge those without the shell. Quote and backslash escaping is resolved the way the shell would, so cd "/a b", cd '/a b', and cd /a\ b all mean /a b.

I did not trust it on my word. New test file, standard-library unittest, 20 tests. On the new hook: Ran 20 tests in 0.511s, OK. Same file against the old hook on main: FAILED (failures=7), and the failures are exactly the new refusals — missing directory, cd X && ... into nothing, a file used as a directory, quoted paths, escaped spaces, a glob treated as a literal. End to end: the old hook exits 0, the command runs, git rev-parse --show-toplevel answers /workspace; the new hook exits 2, the command never runs, and the marker file it would have created is not there.

For the pipeline, the fix is one of two lines, and neither is clever:

set -o pipefail
# or land it first, then read the real code, not tail's
cmd > /tmp/x.log 2>&1; rc=$?
tail -40 /tmp/x.log
Enter fullscreen mode Exit fullscreen mode

${PIPESTATUS[0]} is fine too if you have to keep the pipeline — as long as you actually read it instead of $?.

What I'd check first

If your CI has a guard, a linter-of-commands, a "safe deploy" wrapper, take the one command it is most proud of approving and make its target not exist. A missing directory, a file where a dir should be, a branch that was never checked out. If the wrapper still says ok and whatever runs after it still fetches your data, you have the same gate I had: it validates the shape of a command, not whether the command can run where it claims to.

Same for every check | grep OK. Feed it a false | and watch whether it goes red.

My take, and I'll take the argument: a guard that matches text is worse than no guard, because it buys you confidence you did not earn, and confidence is the expensive part. I have only measured this on a 2.8MB pure-Python hook and my own shell, not on a big CI, so treat the sample as small.

If you run a pre-command hook in your agent stack, what does it do when the directory in the command genuinely is not there — refuse and name the path, or run it and let the next command lie to you? If you think a text-matching gate is good enough because "people won't refactor around it," I want the incident where that held. Comments open.

Top comments (2)

Collapse
 
hritik_chaudhary_5db0056f profile image
Hritik Chaudhary •

Two small additions for the pipeline half. PIPESTATUS is overwritten by the very next command, so copy it on the same line: rc=("${PIPESTATUS[@]}"); then test rc[0]. Even an if or an echo in between resets it. And it is bash-only: zsh calls it pipestatus (lowercase, 1-indexed), which bites when the same snippet runs in a bash CI step and a zsh laptop.

For the cd case, git -C "$dir" rev-parse --show-toplevel fails loudly when $dir is missing, and cd "$dir" || exit 1 before the && chain does the same. A guard that checks the command's shape can never see this one, because the failure is in the environment, not the text.

Collapse
 
dafili profile image
Renhua Wei •

The pipefail angle is the one I reach for first: set -o pipefail makes false | tail -1 return 1 on its own, so the rc=("${PIPESTATUS[@]}") snapshot is only for attributing which stage failed, not whether one did — and it dies the moment any command, including the subshell wrapper, runs between the pipe and the copy. On the cd case, git -C "$dir" rev-parse --show-toplevel also fails when the dir exists but isn't a repo, which cd "$dir" || exit 1 walks straight into; that's the environment failure a shape-based guard can't see, so I run it before any git call.