Skip to content

fs: add directory autodetection to `fsPromises.symlink()`

Expands: https://github.com/nodejs/node/pull/23724

fs.symlink(target, path[, type], callback) and fs.symlinkSync(target, path[, type]) are able to autodetect directories on win32, if typeof type !== 'string'. This PR ports that ability to fsPromises.symlink(target, path[, type]).

"Directory as file" (explicit 'file' or current null/undefined) symlink on windows works like that:

import { mkdir, writeFile, symlink, stat, readdir, readFile } from 'fs/promises';
await mkdir('./src'); await writeFile('./src/testfile', '');
await symlink('./src', './dst_dir', 'dir');
await symlink('./src', './dst_file');

stat('./dst_dir').then(console.log); // Stats { ... }
readdir('./dst_dir').then(console.log); // [ 'testfile' ]
readFile('./dst_dir').then(console.log, console.error); // EISDIR

stat('./dst_file').then(console.log, console.error); // EPERM
readdir('./dst_file').then(console.log, console.error); // EPERM
readFile('./dst_file').then(console.log, console.error); // EPERM
>dir
01.05.2022  00:26    <DIR>          .
01.05.2022  00:26    <DIR>          ..
01.05.2022  00:26    <SYMLINKD>     dst_dir [.\src]
01.05.2022  00:26    <SYMLINK>      dst_file [.\src]
01.05.2022  00:26    <DIR>          src

Can't cd into dst_file, can't dir its content, can't type "dst_file\testfile".

Merge request reports

Loading