bug(fs): fs.exists/fs.existsSync throws when querying subpath of existing file.
See original GitHub issueDescribe the bug A clear and concise description of what the bug is.
fs.exists/fs.existsSync throws when querying subpath of existing file.
To Reproduce Steps to reproduce the behavior:
- In any directory,
touch a.txt
deno --unstable
- >
import * as fs from "https://deno.land/std@0.106.0/fs/mod.ts";
- >
await fs.exists("a.txt/b");
- Error is thrown. See error
Expected behavior A clear and concise description of what you expected to happen.
Return false
.
Screenshots If applicable, add screenshots to help explain your problem.
Desktop (please complete the following information):
- OS: Ubuntu 20.04 with WSL on Windows11
- Version
deno 1.13.2 (release, x86_64-unknown-linux-gnu)
v8 9.3.345.11
typescript 4.3.5
Additional context Add any other context about the problem here.
Other implementations:
- Python3
os.path.exists
: Return false. - Node.js v14
require("fs").exists
: Resolves false. - Ruby 2.7
File.exist?
: Return false.
Simple alternative implementation suggest:
const exists = async (filePath: string): Promise<boolean> => {
try {
await Deno.lstat(filePath);
return true;
} catch (_e: unknown) {
return false;
}
};
fs/exists_test.ts doesn’t include any throws/rejects test.
Issue Analytics
- State:
- Created 2 years ago
- Comments:29 (13 by maintainers)
Top Results From Across the Web
Issue - GitHub
After noticing in the node docs that fs.exists and fs. ... But I only want to allow the file to successfully open if...
Read more >NodeJS - fs.exists() throws error but file is there?
Try adding __dirname infront of you path: var pathname = url.parse(req.url).pathname; fs.exists(__dirname + pathname, function (exist) ...
Read more >File system | Node.js v19.3.0 Documentation
An error will be thrown if this method is called more than once or is called after the FileHandle is closed or closing....
Read more >xfun source: R/paths.R - Rdrr.io
Find file paths that exist #' #' This is a shorthand of \code{x[file.exists(x)]}, and optionally returns the #' first existing file path.
Read more >Releases.md | deno@v1.9.1
File (#10130)- feat(runtime): stabilize Deno.fstat and Deno. ... fs: ensure exists file/dir must be the same type or it will throw error (deno_std#294)### ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Of course, people write problematic code from time to time, myself included, but I don’t understand why
exists
is the only victim. For example, I don’t think anyone would deprecatedelay()
even if the following code cause TOCTOU issues.I think
fs.exists()
in node.js is deprecated because of inconsistencies in the callback design, not because of TOCTOU issues, according to the node.js documentationIn addition, the above link also stated the following
I think this paragraph is the correct way to alert users that improper use of the function can cause TOCTOU issues. In my opinion, there should be a distinction between documentation and deprecation.
I totally agree with this.
If
lstat
is used to check whether a file or directory exists, then I think it is the same.Since git itself does not care whether it is a file or a directory, in order to mimic it’s behavior, it must not care wheter it is a file or a directory.
People like me would touch the contents in
.git
directory to make git related tools.Hi.
I feel that “making
fs.exists()
deprecated” is too aggressive. You all talking about file operations afterfs.exists()
and some race condition caused by that but sometime there is a situation that just want to know if a file or directory exist. In that case, I anyway need to usefs.exists()
and deal deprecation warning messages always.For example, I need to check presence of a
.git
directory to roughly found a git working tree root directory. I will spawngit rev-parse
or whatever after that so the first detection can be rough. This rough step is required because spawning a process on Windows is cost a bit. In this case, I don’t perform any further file operations on Deno itself so “race condition” is not good reason to me for makingfs.exists()
deprecated (becausegit
process would handle that situation outside of Deno).So please re-consider this deprecation again.
P.S.
I totally agree that documenting “file operations after
fs.exists()
may cause some race condition” on documentation offs.exists()
but I believe that documentation and deprecation is different and should be distinctive.