{"record":{"id":"6c080757634f2b96","repo":"nodejs/node","slug":"cannot-determine-safe-temp-directory-you-need-to","errorCode":null,"errorMessage":"Cannot determine safe temp directory.  You need to explicitly provide one.","messagePattern":"Cannot determine safe temp directory\\.  You need to explicitly provide one\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"tools/inspector_protocol/jinja2/bccache.py","lineNumber":221,"sourceCode":"\n    The pattern can be used to have multiple separate caches operate on the\n    same directory.  The default pattern is ``'__jinja2_%s.cache'``.  ``%s``\n    is replaced with the cache key.\n\n    >>> bcc = FileSystemBytecodeCache('/tmp/jinja_cache', '%s.cache')\n\n    This bytecode cache supports clearing of the cache using the clear method.\n    \"\"\"\n\n    def __init__(self, directory=None, pattern='__jinja2_%s.cache'):\n        if directory is None:\n            directory = self._get_default_cache_dir()\n        self.directory = directory\n        self.pattern = pattern\n\n    def _get_default_cache_dir(self):\n        def _unsafe_dir():\n            raise RuntimeError('Cannot determine safe temp directory.  You '\n                               'need to explicitly provide one.')\n\n        tmpdir = tempfile.gettempdir()\n\n        # On windows the temporary directory is used specific unless\n        # explicitly forced otherwise.  We can just use that.\n        if os.name == 'nt':\n            return tmpdir\n        if not hasattr(os, 'getuid'):\n            _unsafe_dir()\n\n        dirname = '_jinja2-cache-%d' % os.getuid()\n        actual_dir = os.path.join(tmpdir, dirname)\n\n        try:\n            os.mkdir(actual_dir, stat.S_IRWXU)\n        except OSError as e:\n            if e.errno != errno.EEXIST:","sourceCodeStart":203,"sourceCodeEnd":239,"githubUrl":"https://github.com/nodejs/node/blob/1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e/tools/inspector_protocol/jinja2/bccache.py#L203-L239","documentation":"Raised by FileSystemBytecodeCache._get_default_cache_dir (jinja2 bccache.py) when no directory was passed to the constructor and the code cannot find a safe default temp directory. On non-Witness platforms it computes a per-user dir under tempfile.gettempdir() keyed by os.getuid(); if the platform lacks os.getuid (some sandboxed/embedded/older interpreters) it bails out.","triggerScenarios":"Constructing FileSystemBytecodeCache() with directory=None on a platform without os.getuid and not on Windows; running under a restricted/embedded Python where gettempdir returns a shared world-writable location and getuid is absent so the per-user subdir trick is unavailable.","commonSituations":"Cross-platform deployment where the cache worked on Linux dev but fails on a minimal container/embedded runtime; enabling bytecode caching without reading the 'provide a directory' requirement.","solutions":["Pass an explicit directory: `FileSystemBytecodeCache(directory='/var/cache/myapp-jinja')`.","Ensure the chosen directory is writable only by the app user (the safety check exists to avoid world-shared temp dirs).","On platforms without getuid, always set directory explicitly — never rely on autodetection."],"exampleFix":"# before\ncache = FileSystemBytecodeCache()\n# after\nimport os\ncache = FileSystemBytecodeCache(directory=os.path.expanduser('~/.cache/myapp-jinja'))","handlingStrategy":"validation","validationCode":"import os, tempfile\nfrom jinja2.bccache import FileSystemBytecodeCache\n\ndef make_cache(directory=None):\n    if directory is None and not hasattr(os, 'getuid') and os.name != 'nt':\n        directory = os.path.join(tempfile.gettempdir(), 'myapp-jinja')\n    os.makedirs(directory or '', exist_ok=True)\n    return FileSystemBytecodeCache(directory=directory)","typeGuard":null,"tryCatchPattern":"try:\n    cache = FileSystemBytecodeCache(directory=directory)\nexcept RuntimeError as e:\n    if 'safe temp directory' in str(e):\n        cache = FileSystemBytecodeCache(directory='/var/cache/myapp-jinja')\n    else:\n        raise","preventionTips":["Always pass an explicit, app-owned directory to FileSystemBytecodeCache.","Never rely on autodetection in cross-platform deployments.","Create the directory with restrictive perms at install time."],"tags":["jinja2","cache","environment","platform"],"backgroundTag":null,"analyzedSha":"1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e","analyzedAt":"2026-08-13T00:53:24.642Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}