Root CauseWhat broke, why, and the fix.

The log line that only broke when a human ran it

· shell, locale, debugging
ⓘ Operated by TechAthletes. Every post here is a bug we hit in our own work — symptom, root cause, fix. Nothing is sponsored and we are not paid to mention any tool.

We fixed a script so that it would explain why a publish was held back. The line we added to do the explaining was the line that broke — and it broke only for the people reading it.

The symptom

A patrol script flagged one line in video-build.sh:

hold "出す前の関門で止まった($VS)— 理由はこのログの HOLD: 行"

Run from a terminal, the message stopped mid-sentence. The variable's value was gone, and so was every character after it. Run from the scheduler, the same line printed correctly, including the value. Nothing in the automated logs ever showed the truncation.

The cause

$VS is followed by ), a full-width parenthesis. The shell has to decide where the variable name ends. In a UTF-8 locale it treats the bytes of that full-width character as part of the name, looks up a variable that does not exist, and substitutes nothing — taking the rest of the string with it.

We measured it with a clean environment rather than guessing:

$ env -i LANG=C          bash -c 'VS=long_context_llm; echo "止まった($VS)"'
止まった(long_context_llm)

$ env -i LANG=en_US.UTF-8 bash -c 'VS=long_context_llm; echo "止まった($VS)"'
止まった(

$ env -i LANG=ja_JP.UTF-8 bash -c 'VS=long_context_llm; echo "止まった($VS)"'
止まった(

With braces, every locale prints the full string:

$ env -i LANG=ja_JP.UTF-8 bash -c 'VS=long_context_llm; echo "止まった(${VS})"'
止まった(long_context_llm)

Why it hid for so long

launchd does not pass LANG. We checked: launchctl getenv LANG returns nothing, and the job's default environment has no LANG entry. So every scheduled run executes in the C locale, where the line is correct. The only way to see the bug was to run the script yourself in a normal interactive shell.

That inverts the usual asymmetry. We are used to bugs that appear in automation and not on a developer's machine — a missing PATH, a missing environment variable. This one appears on the developer's machine and not in automation, which means:

  • it never shows up in the logs you archive and grep,
  • it is not an error, just a sentence that stops early,
  • and the part that disappears is the explanation of why the job stopped.

We had already been bitten by the same shape in the other direction. A release script contained $VAR) in a condition and had therefore never done anything, for seven weeks, while its log still printed the lines that made it look like it had run.

The fix

  • Always write ${VAR}. Do not decide case by case whether a full-width character follows; that is exactly the judgement that fails.
  • Let a checker find them, not your eyes. Ours runs over the repository and reports every $VAR immediately followed by a full-width character.
  • Reproduce before and after in a clean locale. env -i LANG=… costs one command and is the difference between "I changed the line" and "I know the old line was broken and the new one is not."

It is not about full-width parentheses

Our first checker looked for a variable followed by a full-width parenthesis, because that was the character in the line that bit us. That is too narrow. We measured what the shell actually does, one character at a time, in a UTF-8 locale:

$VS)y  → broken        $VS→y  → broken
$VS。y  → broken        $VS・y  → broken
$VSあy  → broken        $VSéy  → broken
$VS y  → broken        $VS×y  → broken
$VS y   → expands correctly

Any character that is not ASCII breaks it. Not just full-width punctuation — an accented letter does it, a multiplication sign does it, an ideographic space does it. The shell takes the first byte of that character into the variable name, fails the lookup, and the remaining bytes of the character are what you see in the output. That is why the output looks like mojibake rather than like a missing word:

$ VS=VALUE; echo "x$VS)y"
x¼‰y

So the rule for the checker is the broad one: a $NAME immediately followed by any non-ASCII byte. A narrower class built from the characters you have already been bitten by will keep missing the next one. We had to widen ours after writing it, which is the second time on this bug that the thing we wrote to catch the problem had the problem's shape in it.

What we took from it

The most dangerous moment for a trap is while you are fixing that same trap. The line we wrote to explain a failure contained the failure. Re-run the check on the line you just changed, not only on the code you set out to fix.

All posts →

Get new posts by email

We email you only when a new post goes up here. You can unsubscribe at any time.

Privacy policy