Skip to content

On macOS, os.tmpdir() is not a real path #11422

@novemberborn

Description

@novemberborn
  • v7.4.0:
  • macOS Sierra 10.12.3:
  • os:

os.tmpdir() returns a path like /var/folders/_6/p8qxp_3n62zg9081tvb0lcc80000gn/T. Per

node/lib/os.js

Line 43 in 00c86cc

path = process.env.TMPDIR ||
that's the same path as the $TMPDIR environment variable. However on macOS this is a symlink. The real path is /private/var/folders/_6/p8qxp_3n62zg9081tvb0lcc80000gn/T:

❯ node -pe 'os.tmpdir()'
/var/folders/_6/p8qxp_3n62zg9081tvb0lcc80000gn/T
❯ node -pe 'fs.realpathSync(os.tmpdir())'
/private/var/folders/_6/p8qxp_3n62zg9081tvb0lcc80000gn/T

On the face of it the current behavior seems correct, and it's just macOS that is annoying. On the other hand it makes for surprising failures when writing cross-platform code. See sindresorhus/temp-write#8 for example.

At the very least I think this pitfall should be documented (happy to open a PR), but that just means users need to always wrap os.tmpdir() with fs.realpathSync(). Should os.tmpdir() do this by itself? On macOS only, or all platforms?

(Note that this is different from #7545. When your shell is in a directory like /var/folders/_6/p8qxp_3n62zg9081tvb0lcc80000gn/T then process.env.PWD is correctly set to that path.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    osIssues and PRs related to the os subsystem.questionIssues that look for answers.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions