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 mock incorrectly throws ENOTDIR when the real fs.existsSync returns false #400

Closed
vlukashov opened this issue Oct 30, 2024 · 4 comments · Fixed by #401
Closed

Comments

@vlukashov
Copy link

It looks like the fs.existsSync mock added in #387 is in some cases inconsistent with the actual fs.existsSync behaviour.

Example (index.js file):

import { default as mock } from "mock-fs";
import * as fs from "node:fs";
import * as path from "node:path";
import { fileURLToPath } from "node:url";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

try {
  console.log(`native fs.existsSync('index.js/path'):`);
  console.log(`>`, fs.existsSync("index.js/path"));
  console.log();

  mock({ "index.js": mock.load(`${__dirname}/index.js`) });
  console.log(`mocked fs.existsSync('index.js/path'):`);
  console.log(`>`, fs.existsSync("index.js/path"));
  mock.restore();
} catch (e) {
  console.error(e);
}

Expected output

native fs.existsSync('index.js/path'):
> false

mocked fs.existsSync('index.js/path'):
> false

Actual output

native fs.existsSync('index.js/path'):
> false

mocked fs.existsSync('index.js/path'):
Error: ENOTDIR, not a directory 'index.js/path'
    at FileSystem.getItem (/Users/viktor/code/sandbox/mock-fs-exists-sync-issue/node_modules/mock-fs/lib/filesystem.js:128:15)
    at Binding.<anonymous> (/Users/viktor/code/sandbox/mock-fs-exists-sync-issue/node_modules/mock-fs/lib/binding.js:1488:31)
    at maybeCallback (/Users/viktor/code/sandbox/mock-fs-exists-sync-issue/node_modules/mock-fs/lib/binding.js:85:17)
    at Binding.exists (/Users/viktor/code/sandbox/mock-fs-exists-sync-issue/node_modules/mock-fs/lib/binding.js:1486:10)
    at Binding.existsSync (/Users/viktor/code/sandbox/mock-fs-exists-sync-issue/node_modules/mock-fs/lib/binding.js:1507:15)
    at Object.<anonymous> (/Users/viktor/code/sandbox/mock-fs-exists-sync-issue/node_modules/mock-fs/lib/index.js:39:39)
    at Module.existsSync (node:fs:292:18)
    at file:///Users/viktor/code/sandbox/mock-fs-exists-sync-issue/index.js:16:23
    at ModuleJob.run (node:internal/modules/esm/module_job:222:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:316:24) {
  code: 'ENOTDIR',
  errno: -20,
  path: 'index.js/path'
}
@vlukashov
Copy link
Author

I come across a library (oclif) that uses fs.existsSync in such way in its internals. This issue makes using the latest versions of mock-fs with code using that library impossible.

Workaround: use an older version of mock-fs (pre-#387, e.g. 5.2.0) and an older version of NodeJS (e.g. 18).

@tschaub
Copy link
Owner

tschaub commented Oct 31, 2024

Thanks for the report. This should be addressed in [email protected].

@vlukashov
Copy link
Author

Awesome to see such a quick fix! 🎉
I'm looking forward to trying it out. 5.4.1 is not on npm yet, but I'll give it a try as soon as it appears there.

@tschaub
Copy link
Owner

tschaub commented Oct 31, 2024

5.4.1 is not on npm yet

Oops. That was an oversight. You should find it there now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants