{"record":{"id":"90f3621011146599","repo":"dnoegel/php-xdg-base-dir","slug":"xdg-runtime-dir-was-not-set","errorCode":null,"errorMessage":"XDG_RUNTIME_DIR was not set","messagePattern":"XDG_RUNTIME_DIR was not set","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/Xdg.php","lineNumber":93,"sourceCode":"    /**\n     * @return string\n     */\n    public function getHomeCacheDir()\n    {\n        $path = getenv('XDG_CACHE_HOME') ?: $this->getHomeDir() . DIRECTORY_SEPARATOR . '.cache';\n\n        return $path;\n\n    }\n\n    public function getRuntimeDir($strict=true)\n    {\n        if ($runtimeDir = getenv('XDG_RUNTIME_DIR')) {\n            return $runtimeDir;\n        }\n\n        if ($strict) {\n            throw new \\RuntimeException('XDG_RUNTIME_DIR was not set');\n        }\n\n        $fallback = sys_get_temp_dir() . DIRECTORY_SEPARATOR . self::RUNTIME_DIR_FALLBACK . getenv('USER');\n\n        $create = false;\n\n        if (!is_dir($fallback)) {\n            mkdir($fallback, 0700, true);\n        }\n\n        $st = lstat($fallback);\n\n        # The fallback must be a directory\n        if (!$st['mode'] & self::S_IFDIR) {\n            rmdir($fallback);\n            $create = true;\n        } elseif ($st['uid'] != $this->getUid() ||\n            $st['mode'] & (self::S_IRWXG | self::S_IRWXO)","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/dnoegel/php-xdg-base-dir/blob/12f5b94710c8f5b504432d57ce353075fc434339/src/Xdg.php#L75-L111","documentation":"This RuntimeException is thrown by DcborgDev\\Xdg\\Xdg::getRuntimeDir() when the XDG_RUNTIME_DIR environment variable is not set and the $strict argument is true. XDG_RUNTIME_DIR is where per-user runtime files (sockets, locks, pids) should live, and the XDG Base Directory spec requires it to be set by the session/login manager. In non-strict mode the library silently falls back to a temp-directory path instead.","triggerScenarios":"Calling (new \\DcborgDev\\Xdg\\Xdg())->getRuntimeDir(true) (or any call that defaults $strict to true) via getenv('XDG_RUNTIME_DIR') when that env var is unset in the PHP process environment.","commonSituations":"Running PHP outside a full desktop session (cron jobs, systemd units without RuntimeDirectory, Docker containers, SSH non-login shells, web-server/PHP-FPM processes) where no login manager exports XDG_RUNTIME_DIR; also CLI workers or CI environments that stripped environment variables.","solutions":["Export XDG_RUNTIME_DIR before running PHP (e.g. export XDG_RUNTIME_DIR=/run/user/$(id -u)) or set it via putenv() in the bootstrap.","Call getRuntimeDir(false) to use the library's /tmp fallback instead of throwing.","On systemd services add RuntimeDirectory= or Environment=XDG_RUNTIME_DIR=/run/user/%U to the unit.","In containers/Docker, set the env var explicitly (ENV XDG_RUNTIME_DIR=/tmp/runtime) and create the directory with correct 0700 permissions."],"exampleFix":"// before\n$dir = $xdg->getRuntimeDir(true); // throws if XDG_RUNTIME_DIR unset\n\n// after\n$dir = $xdg->getRuntimeDir(false); // falls back to sys_get_temp_dir()/...","handlingStrategy":"fallback","validationCode":"if (getenv('XDG_RUNTIME_DIR') === false || getenv('XDG_RUNTIME_DIR') === '') {\n    putenv('XDG_RUNTIME_DIR=' . sys_get_temp_dir() . '/runtime-' . getmypid());\n}\n$dir = $xdg->getRuntimeDir(true);","typeGuard":"function hasXdgRuntimeDir(): bool {\n    $v = getenv('XDG_RUNTIME_DIR');\n    return is_string($v) && $v !== '';\n}","tryCatchPattern":"try {\n    $dir = $xdg->getRuntimeDir(true);\n} catch (\\RuntimeException $e) {\n    if ($e->getMessage() === 'XDG_RUNTIME_DIR was not set') {\n        $dir = sys_get_temp_dir() . '/myapp-runtime';\n        if (!is_dir($dir)) { mkdir($dir, 0700, true); }\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Check getenv('XDG_RUNTIME_DIR') early at application bootstrap and fail fast with a clear message.","Prefer getRuntimeDir(false) in non-interactive contexts (cron, containers, CI).","Set Environment=XDG_RUNTIME_DIR or RuntimeDirectory= in systemd unit files for services.","Set the variable explicitly in Dockerfile/CI env so workers inherit it."],"tags":["php","xdg","environment","runtime-directory"],"backgroundTag":"missing-env-var","analyzedSha":"12f5b94710c8f5b504432d57ce353075fc434339","analyzedAt":"2026-09-16T03:12:34.388Z","contentChangedAt":"2026-09-16T03:12:34.388Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}