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: passing '~' to realpathSync causes error in the REPL #36493

Closed
RaisinTen opened this issue Dec 12, 2020 · 5 comments
Closed

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

RaisinTen opened this issue Dec 12, 2020 · 5 comments
Labels
fs Issues and PRs related to the fs subsystem / file system. path Issues and PRs related to the path subsystem.

Comments

@RaisinTen
Copy link
Contributor

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
@aduh95
Copy link
Contributor

aduh95 commented Dec 12, 2020

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.

@RaisinTen
Copy link
Contributor Author

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.

@aduh95 aduh95 added fs Issues and PRs related to the fs subsystem / file system. path Issues and PRs related to the path subsystem. labels Dec 12, 2020
@mscdex
Copy link
Contributor

mscdex commented Dec 12, 2020

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.

@addaleax
Copy link
Member

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

@RaisinTen
Copy link
Contributor Author

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
Labels
fs Issues and PRs related to the fs subsystem / file system. path Issues and PRs related to the path subsystem.
Projects
None yet
Development

No branches or pull requests

4 participants