fs.realpathSync converts backslash to slash on Linux, throws ENOENT for existing paths
What version of Bun is running?
1.3.14+0d9b296af — also reproduced on canary 1.4.0-canary.1+d37f52067
What platform is your computer?
Linux x64
What steps can reproduce the bug?
// repro-backslash.js
const fs = require("fs");
const dir = "/tmp/back\\slash"; // one literal backslash in the name
fs.mkdirSync(dir, { recursive: true });
console.log("existsSync: ", fs.existsSync(dir));
console.log("lstatSync: ", fs.lstatSync(dir).isDirectory());
console.log("realpathSync:", fs.realpathSync(dir));$ bun repro-backslash.js
existsSync: true
lstatSync: true
ENOENT: no such file or directory, lstat '/tmp/back\slash'fs.realpathSync.native fails identically. Node v22 prints /tmp/back\slash
as expected.
What is the expected behavior?
On POSIX, \ is an ordinary filename character. realpathSync should resolve
the path exactly as existsSync/lstatSync already do (both return true for
this directory in the same process).
What actually happened?
strace shows realpath rewrites the backslash to a forward slash before touching the filesystem, then opens the wrong path:
$ strace -f -e trace=%file bun repro-backslash.js 2>&1 | grep '/tmp/back'
mkdir("/tmp/back\\slash", 0777) = -1 EEXIST (File exists)
newfstatat(AT_FDCWD, "/tmp/back\\slash", {st_mode=S_IFDIR|0755, ...}, 0) = 0
access("/tmp/back\\slash", F_OK) = 0
statx(AT_FDCWD, "/tmp/back\\slash", ...) = 0
openat(AT_FDCWD, "/tmp/back/slash", O_RDONLY|O_PATH) = -1 ENOENTEvery other fs call passes the backslash through byte-perfect; only realpath's
resolver applies the (Windows-only) \ → / normalization. Two smaller
inaccuracies in the resulting error object: syscall is reported as "lstat"
(the failing syscall is openat), and path is the original input, which
makes the error look impossible next to a successful lstatSync of the same
string.
path.normalize / path.resolve / path.join are all correct (backslash
preserved), so the stray normalization is specific to the realpath
implementation.
Real-world impact
Claude Code (a Bun single-file executable) realpath-validates its working directory at startup, so it cannot launch in any directory whose absolute path contains a backslash:
$ mkdir 'a\b' && cd 'a\b' && claude
Error: Can't access working directory /home/user/a\b: Path "/home/user/a\b" does not existSource: oven-sh/bun