You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Run this script in a folder with a symbolic link to a file/directory.
Usemklink file_slink.js file.jsto create a slink for a file. Usemklink /D dir_slink dirto create a slink for a directory.
#!/usr/bin/env node
constfs=require("fs").promises;constpath=require("path");(asyncfunction(){constworkDirPath=process.cwd();/** @type {Dirent[]} */constdirents=awaitfs.readdir(workDirPath,{withFileTypes: true});for(constdirentofdirents){if(dirent.isSymbolicLink()){console.log("Symbolic Link found with dirent.isSymbolicLink!");}}/** @type {string[]} */constfiles=awaitfs.readdir(workDirPath);for(constfileoffiles){constfilepath=path.resolve(workDirPath,file);/** @type {Stats} */conststat=awaitfs.stat(filepath);if(stat.isSymbolicLink()){console.log("Symbolic Link found with fs.stat.isSymbolicLink!");}}})();
How often does it reproduce? Is there a required condition?
Always. No.
What is the expected behavior?
stat.isSymbolicLink() returns true for a symbolic link.
Output:
Symbolic Link found with dirent.isSymbolicLink!
Symbolic Link found with fs.stat.isSymbolicLink!
What do you see instead?
Symbolic Link found with dirent.isSymbolicLink!
Additional information
fs.stat(filepath).isSymbolicLink() should return true for a symbolic link.
So, although it is not a bug, but it's not obvious behaviour for people that works with the file system the first time.
I think it should be noted in the documentation https://nodejs.org/api/fs.html#fs_fspromises_stat_path_options that stat method follows by a symbolic link and isSymbolicLink() will always return false. Use lstat to prevent following by a symbolic link.
Activity
AlttiRi commentedon Feb 20, 2021
Okay, thanks to this answer https://stackoverflow.com/a/43443328/11468937 in order to my example will work it requires to change
fs.stattofs.lstat.So, although it is not a bug, but it's not obvious behaviour for people that works with the file system the first time.
I think it should be noted in the documentation https://nodejs.org/api/fs.html#fs_fspromises_stat_path_options that
statmethod follows by a symbolic link andisSymbolicLink()will always returnfalse. Uselstatto prevent following by a symbolic link.RaisinTen commentedon Feb 21, 2021
Did you see this: https://nodejs.org/api/fs.html#fs_stats_issymboliclink