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

import/no-cycle no such file or directory, stat 'path' #2418

Closed
thepassle opened this issue Mar 29, 2022 · 3 comments
Closed

import/no-cycle no such file or directory, stat 'path' #2418

thepassle opened this issue Mar 29, 2022 · 3 comments

Comments

@thepassle
Copy link

Using the latest version of [email protected] together with [email protected] results in:

> eslint --ext .js,.html . --ignore-path .lintignore


Oops! Something went wrong! :(

ESLint: 8.12.0

Error: ENOENT: no such file or directory, stat 'path'
Occurred while linting /Users/blank/my-project/index.js:1
Rule: "import/no-cycle"
    at Object.statSync (node:fs:1536:3)
    at Function.ExportMap.for (/Users/blank/my-project/node_modules/eslint-plugin-import/lib/ExportMap.js:792:67)
    at /Users/blank/my-project/node_modules/eslint-plugin-import/lib/ExportMap.js:830:142
    at detectCycle (/Users/blank/my-project/node_modules/eslint-plugin-import/lib/rules/no-cycle.js:80:19)
    at checkSourceValue (/Users/blank/my-project/node_modules/eslint-plugin-import/lib/rules/no-cycle.js:113:15)
    at checkSourceValue (/Users/blank/my-project/node_modules/eslint-module-utils/moduleVisitor.js:29:5)
    at checkSource (/Users/blank/my-project/node_modules/eslint-module-utils/moduleVisitor.js:34:5)
    at ruleErrorHandler (/Users/blank/my-project/node_modules/eslint/lib/linter/linter.js:1114:28)
    at /Users/blank/my-project/node_modules/eslint/lib/linter/safe-emitter.js:45:58
    at Array.forEach (<anonymous>)
@ljharb
Copy link
Member

ljharb commented Mar 29, 2022

(I'm not sure what .lintignore is; eslint's typical ignore path is .eslintignore and in modern eslint it's done with ignorePatterns in the config)

Are you perhaps importing a file that doesn't exist, and that's what's causing this crash? What's the contents of /Users/blank/my-project/index.js?

@thepassle
Copy link
Author

I dont think im able to share the contents of the project unfortunately, but its just exporting things from other local modules, e.g.:

export { foo } from './src/bar.js';
// etc

I can confirm all the files that its reexporting exist

@thepassle
Copy link
Author

Right, figured out the issue. I'm using the custom resolver that was posted here, because package export maps were not being respected correctly. Using that resolver correctly resolved my issues, until it tried to resolve node built-in modules.

I've updated the custom resolver to the following:

const path = require('path');
const { resolve: resolveExports } = require('resolve.exports');
+const { builtinModules } = require('module');

/**
 * @param {string} source source
 * @param {string} file file
 * @param {Object} _config config
 */
const resolve = (source, file, _config) => {
  try {
    const moduleId = require.resolve(source, { paths: [path.dirname(file)] });

+    if (builtinModules.includes(moduleId)) {
+      return { found: false };
+    }

    return { found: true, path: moduleId };
  } catch (/** @type {any} */ err) {
    if (err.code === 'MODULE_NOT_FOUND' && err.path?.endsWith('/package.json')) {
      const { name, module, main, exports } = require(err.path);
      const resolved = resolveExports({ name, module, main, exports }, source);
      const moduleId = path.join(path.dirname(err.path), resolved);
      return { found: true, path: moduleId };
    }
    return { found: false };
  }
};

module.exports = {
  interfaceVersion: 2,
  resolve,
};

And that resolved the issue. I've also posted this fix on the gist in case anybody else runs into the same issue.

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

No branches or pull requests

2 participants