Skip to content

statSync fails in detecting link #47633

Description

@FStefanni

Version

v18.15.0

Platform

Linux debian 6.1.0-7-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.20-2 (2023-04-08) x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

Just call statSync() on node_modules/.bin/mocha:

import fs from "node:fs"

const mocha = "<abs path>/node_modules/.bin/mocha";
const stat = fs.statSync(mocha);
console.log(`File: ${stat.isFile()}, Link: ${stat.isSymbolicLink()}`);

Result:

File: true, Link: false

But if I run in bash:

ls -l <abs path>/node_modules/.bin/mocha
stat <abs path>/node_modules/.bin/mocha

Result:

lrwxrwxrwx 1 user group 21 20 apr 08.33 <abs path>/node_modules/.bin/mocha -> ../mocha/bin/mocha.js

File: <abs path>/node_modules/.bin/mocha -> ../mocha/bin/mocha.js
Size: 21        	Blocks: 0          IO Block: 4096   symbolic link
Device: 259,2	Inode: 14118017    Links: 1
Access: (0777/lrwxrwxrwx)  Uid: ( 1000/   user)   Gid: ( 1000/   group)
Access: 2023-04-20 08:54:29.337525872 +0200
Modify: 2023-04-20 08:33:01.270951671 +0200
Change: 2023-04-20 08:33:01.270951671 +0200
 Birth: 2023-04-20 08:33:01.270951671 +0200

So it is truly a symbolic link.

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior? Why is that the expected behavior?

it should say that it is a link and not a regular file

What do you see instead?

It says it is a regular file

Additional information

No response

Activity

  1. targos commented on Apr 20, 2023

    @targos
    Member

    fs.stat follows symlinks. You need to call lstat if you don't want this behavior

  2. added
    docIssues and PRs related to Node.js documentation.
    fsIssues and PRs related to file-system APIs and the fs module.
    on Apr 20, 2023
  3. FStefanni commented on Apr 20, 2023

    @FStefanni
    Author

    Hi,

    yes you are right I missed it in the doc.
    Maybe it is possible to explicitly write this in the documentation of all stat() function variants (stat, promises.stat(), etc.), so it will be clearer? (I suppose the "doc" tag of this issue means exactly this :) )

    Regards

  4. targos commented on Apr 20, 2023

    @targos
    Member

    I think it would be helpful to mention it directly in the documentation, yes.

  5. added
    good first issueIssues that are suitable for first-time contributors.
    on Apr 20, 2023
  6. amitanshusahu commented on Apr 20, 2023

    @amitanshusahu

    hey @targos , i would like to work on it if its open, can i git this assigned :)

  7. mertcanaltin commented on Apr 20, 2023

    @mertcanaltin
    Member

    @targos I tried to make an edit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to Node.js documentation.fsIssues and PRs related to file-system APIs and the fs module.good first issueIssues that are suitable for first-time contributors.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions