Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs.stat(filepath).isSymbolicLink() always returns false #37456

Closed
AlttiRi opened this issue Feb 20, 2021 · 2 comments
Closed

fs.stat(filepath).isSymbolicLink() always returns false #37456

AlttiRi opened this issue Feb 20, 2021 · 2 comments

Comments

@AlttiRi
Copy link

AlttiRi commented Feb 20, 2021

  • Version: v15.9.0, v14.15.5
  • Platform: Windows 10 x64 2004

What steps will reproduce the bug?

Run this script in a folder with a symbolic link to a file/directory.

Use mklink file_slink.js file.js to create a slink for a file.
Use mklink /D dir_slink dir to create a slink for a directory.

#!/usr/bin/env node

const fs = require("fs").promises;
const path = require("path");

(async function() {
    const workDirPath = process.cwd();

    /** @type {Dirent[]} */
    const dirents = await fs.readdir(workDirPath, {withFileTypes: true});
    for (const dirent of dirents) {
        if (dirent.isSymbolicLink()) {
            console.log("Symbolic Link found with dirent.isSymbolicLink!");
        }
    }

    /** @type {string[]} */
    const files = await fs.readdir(workDirPath);
    for (const file of files) {
        const filepath = path.resolve(workDirPath, file);
        /** @type {Stats} */
        const stat = await fs.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.

@AlttiRi
Copy link
Author

AlttiRi commented 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.stat to fs.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 stat method follows by a symbolic link and isSymbolicLink() will always return false. Use lstat to prevent following by a symbolic link.

@RaisinTen
Copy link
Contributor

Did you see this: https://nodejs.org/api/fs.html#fs_stats_issymboliclink

stats.isSymbolicLink()

This method is only valid when using fs.lstat().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants