Skip to content

os.tmpdir can return an invalid path on Windows #60582

@rhysd

Description

@rhysd

Version

v24.11.0

Platform

Microsoft Windows NT 10.0.26100.0 x64

Subsystem

No response

What steps will reproduce the bug?

Run the following code on Windows.

process.env = {};
console.log(require('os').tmpdir());

How often does it reproduce? Is there a required condition?

Frequency is 100%

What is the expected behavior? Why is that the expected behavior?

os.tmpdir() should return a correct temporary directory path. Ideally it should return a value returned from the platform-specific APIs like GetTempPath2W. Or it should raise an error that it could not make a proper path.

What do you see instead?

os.tmpdir() returns the incorrect directory path undefined\temp.

Additional information

This issue happens when the node process is run in some separate sandboxed environments (for example, testing environment which wants to avoid effects by environment variables).

Current implementation:

node/lib/os.js

Lines 182 to 184 in cfa11ba

const path = process.env.TEMP ||
process.env.TMP ||
(process.env.SystemRoot || process.env.windir) + '\\temp';

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions