import-js / import-js/eslint-plugin-import
no-cycle does not detect require()-based cycles
- Dominant language
- JavaScript
- Stars
- 5.9k
- Forks
- 1.5k
- PR merge metrics
- No merged PRs in 30d
Description
Following up on #941, as requested [here](https://github.com/import-js/eslint-plugin-import/issues/941#issuecomment-1412834365).
**From my testing, the `no-cycle` rule DOES NOT DETECT circular dependencies if they are imported via `require()` calls**.
This seems to be the intended behavior, however. As mentioned in [this comment](https://github.com/import-js/eslint-plugin-import/issues/941#issuecomment-973670709), I found this quote from the [no-cycle docs](https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-cycle.md):
> By default, this rule only detects cycles for ES6 imports, but see the [no-unresolved options](https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-unresolved.md#options) as this rule also supports the same commonjs and amd flags. However, these flags only impact which import types are linted; the import/export infrastructure only registers import statements in dependencies, so cycles created by require within imported modules may not be detected.
However, a [follow-up comment](https://github.com/import-js/eslint-plugin-import/issues/941#issuecomment-1412826051) says that `require()` cycles are in fact supported, and suggested I open a new issue.
As far as I can tell, the original commenter's repo on that issue shows the problem quite well:
https://github.com/simonbuchan/eslint-import-cycle-example
Clone that, then:
```
$ npm install
$ npm run lint
> eslint-import-cycle@1.0.0 lint
> eslint .
/eslint-import-cycle-example/import-a.js
1:1 error Dependency cycle detected import/no-cycle
/eslint-import-cycle-example/import-b.js
1:1 error Dependency cycle detected import/no-cycle
✖ 2 problems (2 errors, 0 warnings)
```
It should also be detecting the cycle in `require-a.js`.
Am I misusing something?
Contributor guide
Research direction
Start with the require-a.js reproduction from eslint-import-cycle-example and compare its lint output with import-a.js; run npm install and npm run lint. Read docs/rules/no-cycle.md and trace the no-cycle handling for CommonJS require() dependencies. Done means the require-based cycle is reported as a dependency cycle without breaking the existing import-cycle behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- eslint, javascript
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100