{"record":{"id":"319fad5c36c05d0f","repo":"sebastianbergmann/phpunit","slug":"unable-to-write-temporary-file","errorCode":null,"errorMessage":"Unable to write temporary file","messagePattern":"Unable to write temporary file","errorType":"exception","errorClass":"PHPUnit\\Util\\PHP\\PhpProcessException","httpStatus":null,"severity":"error","filePath":"src/Util/PHP/JobRunner.php","lineNumber":102,"sourceCode":"        );\n\n        EventFacade::emitter()->childProcessFinished($job->reason(), $result->stdout(), $result->stderr());\n    }\n\n    /**\n     * @throws PhpProcessException\n     */\n    public function run(Job $job): Result\n    {\n        $temporaryFile = null;\n\n        if ($job->hasInput()) {\n            $temporaryFile = tempnam(sys_get_temp_dir(), 'phpunit_');\n\n            if ($temporaryFile === false ||\n                file_put_contents($temporaryFile, $job->code()) === false) {\n                // @codeCoverageIgnoreStart\n                throw new PhpProcessException(\n                    'Unable to write temporary file',\n                );\n                // @codeCoverageIgnoreEnd\n            }\n\n            $job = new Job(\n                $job->input(),\n                $job->reason(),\n                $job->phpSettings(),\n                $job->environmentVariables(),\n                $job->arguments(),\n                null,\n                $job->redirectErrors(),\n                $job->requiresXdebug(),\n            );\n        }\n\n        assert($temporaryFile !== '');","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/sebastianbergmann/phpunit/blob/f123cdb2a2d49f15025794166cfed8bda8627dd2/src/Util/PHP/JobRunner.php#L84-L120","documentation":"JobRunner::run() executes a test in a separate PHP process (#[RunInSeparateProcess], process isolation). It first writes the generated job code to a temporary file using tempnam(sys_get_temp_dir(), 'phpunit_') plus file_put_contents(); if either step fails it throws PhpProcessException('Unable to write temporary file'). This is an environment failure in the temp directory, not a problem with your test code.","triggerScenarios":"Running any test marked #[RunInSeparateProcess] (or with process isolation enabled globally) on a host where the temp directory is not writable: TMPDIR points somewhere unwritable, /tmp is a read-only filesystem, the disk is full, or open_basedir excludes the temp path.","commonSituations":"Hardened Docker images with a read-only root filesystem and no tmpfs on /tmp; CI runners with exhausted disk; misconfigured TMPDIR in systemd services or cron contexts; open_basedir shared-hosting style restrictions.","solutions":["Point TMPDIR at a writable directory before running PHPUnit: `TMPDIR=/workspace/tmp vendor/bin/phpunit`","In containers, mount a writable tmpfs on /tmp (for example `--tmpfs /tmp:rw` in docker run)","Free disk space or raise the quota if the volume is full","If process isolation is not strictly required for that test, remove #[RunInSeparateProcess] / the isolation setting so no temp job file is needed"],"exampleFix":"# before\n$ vendor/bin/phpunit --process-isolation\n# PhpProcessException: Unable to write temporary file (read-only /tmp)\n\n# after\n$ mkdir -p /workspace/tmp && TMPDIR=/workspace/tmp vendor/bin/phpunit --process-isolation","handlingStrategy":"try-catch","validationCode":"if (!is_writable(sys_get_temp_dir())) {\n    throw new RuntimeException('Temp directory ' . sys_get_temp_dir() . ' is not writable; set TMPDIR');\n}","typeGuard":null,"tryCatchPattern":"use PHPUnit\\Util\\PHP\\PhpProcessException;\n\ntry {\n    $runner->run($job);\n} catch (PhpProcessException $e) {\n    if ($e->getMessage() === 'Unable to write temporary file') {\n        // re-run with TMPDIR pointed at a writable dir, or skip process isolation\n    }\n}","preventionTips":["Add a smoke check to the bootstrap: fail fast when is_writable(sys_get_temp_dir()) is false","Export TMPDIR to a workspace-local directory in CI job definitions","Keep a tmpfs mounted on /tmp in read-only containers","Only use #[RunInSeparateProcess] where isolation is genuinely required"],"tags":["phpunit","process-isolation","temp-file","filesystem","environment"],"backgroundTag":"temp-file-creation-failed","analyzedSha":"f123cdb2a2d49f15025794166cfed8bda8627dd2","analyzedAt":"2026-08-23T01:20:58.058Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}