nodejs/node · error · RuntimeError

Cannot determine safe temp directory. You need to explicitl

Error message

Cannot determine safe temp directory.  You need to explicitly provide one.

What it means

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.

Source

Thrown at tools/inspector_protocol/jinja2/bccache.py:221

    The pattern can be used to have multiple separate caches operate on the
    same directory.  The default pattern is ``'__jinja2_%s.cache'``.  ``%s``
    is replaced with the cache key.

    >>> bcc = FileSystemBytecodeCache('/tmp/jinja_cache', '%s.cache')

    This bytecode cache supports clearing of the cache using the clear method.
    """

    def __init__(self, directory=None, pattern='__jinja2_%s.cache'):
        if directory is None:
            directory = self._get_default_cache_dir()
        self.directory = directory
        self.pattern = pattern

    def _get_default_cache_dir(self):
        def _unsafe_dir():
            raise RuntimeError('Cannot determine safe temp directory.  You '
                               'need to explicitly provide one.')

        tmpdir = tempfile.gettempdir()

        # On windows the temporary directory is used specific unless
        # explicitly forced otherwise.  We can just use that.
        if os.name == 'nt':
            return tmpdir
        if not hasattr(os, 'getuid'):
            _unsafe_dir()

        dirname = '_jinja2-cache-%d' % os.getuid()
        actual_dir = os.path.join(tmpdir, dirname)

        try:
            os.mkdir(actual_dir, stat.S_IRWXU)
        except OSError as e:
            if e.errno != errno.EEXIST:

View on GitHub (pinned to 1b2de5e052)

Solutions

  1. Pass an explicit directory: `FileSystemBytecodeCache(directory='/var/cache/myapp-jinja')`.
  2. Ensure the chosen directory is writable only by the app user (the safety check exists to avoid world-shared temp dirs).
  3. On platforms without getuid, always set directory explicitly — never rely on autodetection.

Example fix

# before
cache = FileSystemBytecodeCache()
# after
import os
cache = FileSystemBytecodeCache(directory=os.path.expanduser('~/.cache/myapp-jinja'))
Defensive patterns

Strategy: validation

Validate before calling

import os, tempfile
from jinja2.bccache import FileSystemBytecodeCache

def make_cache(directory=None):
    if directory is None and not hasattr(os, 'getuid') and os.name != 'nt':
        directory = os.path.join(tempfile.gettempdir(), 'myapp-jinja')
    os.makedirs(directory or '', exist_ok=True)
    return FileSystemBytecodeCache(directory=directory)

Try / catch

try:
    cache = FileSystemBytecodeCache(directory=directory)
except RuntimeError as e:
    if 'safe temp directory' in str(e):
        cache = FileSystemBytecodeCache(directory='/var/cache/myapp-jinja')
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of nodejs/node@1b2de5e052 (2026-08-13). Data as JSON: /api/errors/6c080757634f2b96. Report an issue: GitHub.