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.existsSync returns wrong value on windows if there are no permissions on the given file #17921

Closed
ghost opened this issue Dec 30, 2017 · 15 comments
Labels
doc Issues and PRs related to the documentations. fs Issues and PRs related to the fs subsystem / file system. windows Issues and PRs related to the Windows platform.

Comments

@ghost
Copy link

ghost commented Dec 30, 2017

  • Version: v9.2.0
  • Platform: windows 8.1
  • Subsystem: fs

Steps to reproduce the issue

  1. Create a file (for example 1.txt)
  2. Disable permissions inheritance and remove all inherited permissions from the file
  3. Execute fs.existsSync('1.txt')

What is the expected behavior

The file exists (and it is also listed if you readdirSync it's parent directory), so the return value should be true.

What went wrong

The return value is false, like the file doesn't exist. If it is working as intended, then consider this issue as a feature request. However, if you're not aware of this behavior, then I'm pretty sure it is a bug.

@vsemozhetbyt vsemozhetbyt added fs Issues and PRs related to the fs subsystem / file system. windows Issues and PRs related to the Windows platform. labels Dec 30, 2017
@jaulz
Copy link

jaulz commented Jan 17, 2018

I have a similar issue where existsSync returns true though the file does not exist. Happens on Windows 10 with NodeJS v8.9.1.

@Johnsavadkuhi
Copy link
Contributor

let me to see your code

@bnoordhuis bnoordhuis added the doc Issues and PRs related to the documentations. label Jan 17, 2018
@bnoordhuis
Copy link
Member

Perhaps the documentation could be improved (I'll add the label) but fs.exists() works the same way as fs.stat() in this regard: it queries the operating system for file metadata for the specified path and passes on the error that the operating system returns.

The difference is that fs.stat() would have returned an error whereas fs.exists() returns false because true or false is the only thing it's allowed to return.

You might get better results with fs.access(), depending on the situation.

@jaulz
Copy link

jaulz commented Jan 17, 2018

Following the code I use:

console.log('bin')
console.log(bin)
console.log('fs.existsSync(bin)')
console.log(fs.existsSync(bin))
try {
  console.log('fs.accessSync(bin)')
  console.log(fs.accessSync(bin))
} catch (error) {
  console.log('-')
}
while (true) { }

This is the output:

bin
C:\Users\XXX\.appcelerator\install\7.0.1\package\bin\appc
fs.existsSync(bin)
true
fs.accessSync(bin)
undefined

I put a while(true) {} to stop at that very stage but if I run >cd C:\Users\XXX\.appcelerator in parallel the output is: The system cannot find the path specified.

Any idea?

@bnoordhuis
Copy link
Member

@jaulz That's probably some ACL or permissions issue. The directory is there, it's just inaccessible.

@jaulz
Copy link

jaulz commented Jan 18, 2018

@bnoordhuis not sure... I cannot even find that folder via command nor via explorer.

@6-8-axnw1bom81v5xa3nh48c
Copy link

6-8-axnw1bom81v5xa3nh48c commented Jan 20, 2018

I am the reporter of this issue, but I accidentally deleted my account.

@bnoordhuis So, this actually works as intended? Ok, then it would be a good idea to mention it somewhere in the doc. Or, even better, change the behavior of fs.existsSync to try to read parent dir in case of error and if the file is located in the parent dir, then return true, no matter if the actual file is accessible or not.

@Johnsavadkuhi What does your thumbs down mean: you cannot reproduce the issue, or you disagree that this issue should be considered as a bug? "Let me to see your code" - my code or @jaulz's code?

@bzoz
Copy link
Contributor

bzoz commented Feb 1, 2018

In this case existsSync call to stat fails with EPERM, maybe we could make existsSync return false when the error is ENOENT? Or, is there a reason why existsSync does not use accessSync?

@bnoordhuis
Copy link
Member

In this case existsSync call to stat fails with EPERM, maybe we could make existsSync return false when the error is ENOENT?

fs.existsSync() always returns false when the stat fails.

Or, is there a reason why existsSync does not use accessSync?

Yes, see #4679 - Windows CI issue.

bzoz added a commit to JaneaSystems/node that referenced this issue Feb 12, 2018
Uses fs.access to implement fs.exists functionality. Fixes a issue,
when a file exists but user does not have privileges to do stat on the
file.

Fixes: nodejs#17921

# Conflicts:
#	lib/fs.js
@bzoz bzoz closed this as completed in d3955d1 Feb 22, 2018
bzoz added a commit to JaneaSystems/node that referenced this issue Mar 28, 2018
Uses fs.access to implement fs.exists functionality. Fixes a issue,
when a file exists but user does not have privileges to do stat on the
file.

Fixes: nodejs#17921

PR-URL: nodejs#18618
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Weijia Wang <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: James M Snell <[email protected]>
targos pushed a commit that referenced this issue Apr 4, 2018
Uses fs.access to implement fs.exists functionality. Fixes a issue,
when a file exists but user does not have privileges to do stat on the
file.

Fixes: #17921

PR-URL: #18618
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Weijia Wang <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: James M Snell <[email protected]>
targos pushed a commit that referenced this issue Apr 4, 2018
Uses fs.access to implement fs.exists functionality. Fixes a issue,
when a file exists but user does not have privileges to do stat on the
file.

Fixes: #17921

Backport-PR-URL: #19654
PR-URL: #18618
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Weijia Wang <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: James M Snell <[email protected]>
MayaLekova pushed a commit to MayaLekova/node that referenced this issue May 8, 2018
Uses fs.access to implement fs.exists functionality. Fixes a issue,
when a file exists but user does not have privileges to do stat on the
file.

Fixes: nodejs#17921

PR-URL: nodejs#18618
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Weijia Wang <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: James M Snell <[email protected]>
@cawoodm
Copy link

cawoodm commented Mar 27, 2019

I'm using NodeJS v11.10.0 on Windows 10 and having the same issue: existsSync says a file does not exist but it does and I do have permissions on it. What to do?

@BridgeAR
Copy link
Member

@cawoodm you probably want to either use fs.accessSync or you directly want to call fs.open as recommended in the documentation.

There is no guarantee that the file does not exist if fs.existsSync returns false. It only let's you know if the file is there and is accessible for you. There could be multiple reasons for the cause in case it returns false.

@cawoodm
Copy link

cawoodm commented Mar 30, 2019

Oops, I think my problem was with backslashes in the path C:\temp being interpreted as tabs. Sorry for jostling!

@ohuu
Copy link

ohuu commented May 4, 2020

Sorry to comment on a closed issue but I'm seeing the same thing.

OS: Windows 10
Node version: 13.12.0

The file in question is java installed via scoop C:\Users\Oliver\scoop\apps\openjdk11\current\bin\java. It has permissions and definitely exists but existsSync returns false!

Calling accessSync returns this error:

Error: ENOENT: no such file or directory, access 'C:/Users/Oliver/scoop/apps/openjdk11/current/bin/java'
    at Object.accessSync (fs.js:208:3) {
  errno: -4058,
  syscall: 'access',
  code: 'ENOENT',
  path: 'C:/Users/Oliver/scoop/apps/openjdk11/current/bin/java'
}

calling open returns undefined.

@ohuu
Copy link

ohuu commented May 7, 2020

Finally got to the bottom of this. Windows has to have the extension of the file in order to check that it exists so fs.existsSync('C:/Users/Oliver/scoop/apps/openjdk11/current/bin/java.exe') works but fs.existsSync('C:/Users/Oliver/scoop/apps/openjdk11/current/bin/java') doesn't

@badrelmers
Copy link

this is really a stupid language

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. fs Issues and PRs related to the fs subsystem / file system. windows Issues and PRs related to the Windows platform.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants