Skip to content

fs: passing '~' to realpathSync causes error in the REPL #36493

Description

@RaisinTen

What is the bug?

Every time in the REPL:

> console.log(require('fs').realpathSync('~'));
Uncaught:
Error: ENOENT: no such file or directory, lstat '/home/raisinten/Desktop/universe/temp/project/~'
    at Object.realpathSync (node:fs:1753:7) {
  errno: -2,
  syscall: 'lstat',
  code: 'ENOENT',
  path: '/home/raisinten/Desktop/universe/temp/project/~'
}

What is the expected behavior?

> console.log(require('fs').realpathSync('~'));
/home/raisinten
undefined

Activity

  1. aduh95 commented on Dec 12, 2020

    @aduh95
    Contributor

    Shouldn't you use path.resolve for that kind of thing? fs.realpath only resolves ., .. and symbolic links according to the docs, ~ is neither of those.

  2. RaisinTen commented on Dec 12, 2020

    @RaisinTen
    MemberAuthor

    The docs also mention this after that:

    This function behaves like realpath(3)

    and realpath does resolve ~ correctly.

    What feels weird about this is that it causes this error only in the REPL and works perfectly when run using node <file>.

    I didn't try path.resolve with '~' yet.

  3. added
    fsIssues and PRs related to file-system APIs and the fs module.
    pathIssues and PRs related to the path subsystem.
    on Dec 12, 2020
  4. mscdex commented on Dec 12, 2020

    @mscdex
    Contributor

    and realpath does resolve ~ correctly.

    If you're checking that via your shell, you're using realpath(1), not realpath(3) directly. The latter does not support tilde expansion.

  5. addaleax commented on Dec 12, 2020

    @addaleax
    Member

    Yes, ~ is a shell feature, not a file system one. Not supporting it is the right thing to do for Node.js.

  6. RaisinTen commented on Dec 13, 2020

    @RaisinTen
    MemberAuthor

    Thanks for the explanation! The reason why it worked when I ran it outside the REPL is that I passed in the path from the command line and the shell expanded it without my knowledge. 🤦

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

    fsIssues and PRs related to file-system APIs and the fs module.pathIssues and PRs related to the path subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions