At 05:50 ET on Tuesday 17 November a scheduled routine that publishes a daily sports results page ran its build and stopped. Three of 117 tests failed, all in one file that exercised a guard script added to the repository two days earlier. The page went out at 08:15 instead, after a fix to one line of that script was merged at 08:10.

The line was tmp="$(mktemp -d)". The routine runs its build inside a macOS sandbox profile that allows writes in three places: the git worktree, the run’s own directory and the TMPDIR the routine sets for the build. The script never touched that TMPDIR. Its scratch directory landed in /var/folders, and the sandbox refused the write with Operation not permitted.

What mktemp does on a Mac

The desk reproduced it on this site’s own Mac, running macOS 27.2.1, with TMPDIR pointed at a scratch folder:

  • mktemp -d created /var/folders/<id>/T/tmp.<random> and ignored TMPDIR.
  • mktemp -d -t probe did the same, with probe as the prefix.
  • mktemp -d "$TMPDIR/x.XXXXXX" created the directory inside the scratch folder.
  • mktemp -d -p "$dir" also landed in the folder, with TMPDIR set or unset.

The /var/folders path is the per-user temporary directory that getconf DARWIN_USER_TEMP_DIR reports, and in an ordinary login shell it is also the value of TMPDIR. So in a normal terminal, ignoring TMPDIR and honouring it give the same answer, and nobody notices. The two only part when something sets TMPDIR to a different place, which is exactly what a sandbox wrapper does.

The rest of the toolchain does read it. With the same TMPDIR, Node 22’s os.tmpdir() and Python’s tempfile.gettempdir() both returned the scratch folder. So a JavaScript test can create its fixtures where the sandbox allows, call a shell script, and have that script write somewhere the sandbox does not. The test file that failed on 17 November was JavaScript. The write it tripped on came from the shell script it called.

The man page gives two answers

The mktemp man page on the same Mac describes both behaviours. Its description says -t builds the template from the _CS_DARWIN_USER_TEMP_DIR configuration variable when it is available, and falls back to TMPDIR, the -p directory and /tmp only if that variable is missing. That matches the binary. A few lines further down, the entry for -t says it uses “the supplied prefix and TMPDIR if set”, and the examples section offers mktemp -t under the heading “To allow the use of $TMPDIR”. On this machine that example does not do what its heading says.

The same page says -p applies “if the TMPDIR environment variable is not set”. In the desk’s test -p won with TMPDIR set as well.

Linux does what the option entry says

A Linux CI runner has GNU coreutils, and GNU mktemp behaves the way the Mac page’s option entry claims. With no template, GNU uses tmp.XXXXXXXXXX and implies --tmpdir, which falls back to TMPDIR if available and to /tmp otherwise [1]. GNU also marks -t as deprecated and recommends -p, because it favours the command line over TMPDIR [1].

A script that leans on TMPDIR therefore passes on the runner and writes somewhere else on a Mac.

The fix and the review check

Give mktemp the directory every time:

tmp="$(mktemp -d "${TMPDIR:-/tmp}/data-shrink.XXXXXX")"

BSD and GNU mktemp both read an explicit template as a path, so the directory comes from the caller’s environment with /tmp as the fallback, on either system. -p "${TMPDIR:-/tmp}" also works on both, but the template form reads the same to someone who has memorised neither man page. The follow-up commit added a test that pins the template, so a later edit that drops it fails the test suite.

Two habits go with it. When a sandboxed build fails with Operation not permitted on a temporary path, the fault is in the script. Loosening the sandbox to allow /var/folders opens a directory that every process on the account shares. And when cleaning up after a probe like the desk’s, delete the exact paths the probe printed, never a glob such as /var/folders/<id>/T/tmp.*, because other programs keep their own files there.

In review, any new shell script with a bare mktemp -d or a mktemp -t goes back with the template form attached.

The line that held the page for two hours and 25 minutes was 18 characters long, and the one that published it is 55.

Sources

  1. GNU Coreutils manual, mktemp invocation