| 1 | # import/no-cycle
|
|---|
| 2 |
|
|---|
| 3 | <!-- end auto-generated rule header -->
|
|---|
| 4 |
|
|---|
| 5 | Ensures that there is no resolvable path back to this module via its dependencies.
|
|---|
| 6 |
|
|---|
| 7 | This includes cycles of depth 1 (imported module imports me) to `"∞"` (or `Infinity`), if the
|
|---|
| 8 | [`maxDepth`](#maxdepth) option is not set.
|
|---|
| 9 |
|
|---|
| 10 | ```js
|
|---|
| 11 | // dep-b.js
|
|---|
| 12 | import './dep-a.js'
|
|---|
| 13 |
|
|---|
| 14 | export function b() { /* ... */ }
|
|---|
| 15 | ```
|
|---|
| 16 |
|
|---|
| 17 | ```js
|
|---|
| 18 | // dep-a.js
|
|---|
| 19 | import { b } from './dep-b.js' // reported: Dependency cycle detected.
|
|---|
| 20 | ```
|
|---|
| 21 |
|
|---|
| 22 | This rule does _not_ detect imports that resolve directly to the linted module;
|
|---|
| 23 | for that, see [`no-self-import`].
|
|---|
| 24 |
|
|---|
| 25 | This rule ignores type-only imports in Flow and TypeScript syntax (`import type` and `import typeof`), which have no runtime effect.
|
|---|
| 26 |
|
|---|
| 27 | ## Rule Details
|
|---|
| 28 |
|
|---|
| 29 | ### Options
|
|---|
| 30 |
|
|---|
| 31 | By default, this rule only detects cycles for ES6 imports, but see the [`no-unresolved` options](./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
|
|---|
| 32 | import/export infrastructure only registers `import` statements in dependencies, so
|
|---|
| 33 | cycles created by `require` within imported modules may not be detected.
|
|---|
| 34 |
|
|---|
| 35 | #### `maxDepth`
|
|---|
| 36 |
|
|---|
| 37 | There is a `maxDepth` option available to prevent full expansion of very deep dependency trees:
|
|---|
| 38 |
|
|---|
| 39 | ```js
|
|---|
| 40 | /*eslint import/no-cycle: [2, { maxDepth: 1 }]*/
|
|---|
| 41 |
|
|---|
| 42 | // dep-c.js
|
|---|
| 43 | import './dep-a.js'
|
|---|
| 44 | ```
|
|---|
| 45 |
|
|---|
| 46 | ```js
|
|---|
| 47 | // dep-b.js
|
|---|
| 48 | import './dep-c.js'
|
|---|
| 49 |
|
|---|
| 50 | export function b() { /* ... */ }
|
|---|
| 51 | ```
|
|---|
| 52 |
|
|---|
| 53 | ```js
|
|---|
| 54 | // dep-a.js
|
|---|
| 55 | import { b } from './dep-b.js' // not reported as the cycle is at depth 2
|
|---|
| 56 | ```
|
|---|
| 57 |
|
|---|
| 58 | This is not necessarily recommended, but available as a cost/benefit tradeoff mechanism
|
|---|
| 59 | for reducing total project lint time, if needed.
|
|---|
| 60 |
|
|---|
| 61 | #### `ignoreExternal`
|
|---|
| 62 |
|
|---|
| 63 | An `ignoreExternal` option is available to prevent the cycle detection to expand to external modules:
|
|---|
| 64 |
|
|---|
| 65 | ```js
|
|---|
| 66 | /*eslint import/no-cycle: [2, { ignoreExternal: true }]*/
|
|---|
| 67 |
|
|---|
| 68 | // dep-a.js
|
|---|
| 69 | import 'module-b/dep-b.js'
|
|---|
| 70 |
|
|---|
| 71 | export function a() { /* ... */ }
|
|---|
| 72 | ```
|
|---|
| 73 |
|
|---|
| 74 | ```js
|
|---|
| 75 | // node_modules/module-b/dep-b.js
|
|---|
| 76 | import { a } from './dep-a.js' // not reported as this module is external
|
|---|
| 77 | ```
|
|---|
| 78 |
|
|---|
| 79 | Its value is `false` by default, but can be set to `true` for reducing total project lint time, if needed.
|
|---|
| 80 |
|
|---|
| 81 | #### `allowUnsafeDynamicCyclicDependency`
|
|---|
| 82 |
|
|---|
| 83 | This option disable reporting of errors if a cycle is detected with at least one dynamic import.
|
|---|
| 84 |
|
|---|
| 85 | ```js
|
|---|
| 86 | // bar.js
|
|---|
| 87 | import { foo } from './foo';
|
|---|
| 88 | export const bar = foo;
|
|---|
| 89 |
|
|---|
| 90 | // foo.js
|
|---|
| 91 | export const foo = 'Foo';
|
|---|
| 92 | export function getBar() { return import('./bar'); }
|
|---|
| 93 | ```
|
|---|
| 94 |
|
|---|
| 95 | > Cyclic dependency are **always** a dangerous anti-pattern as discussed extensively in [#2265](https://github.com/import-js/eslint-plugin-import/issues/2265). Please be extra careful about using this option.
|
|---|
| 96 |
|
|---|
| 97 | #### `disableScc`
|
|---|
| 98 |
|
|---|
| 99 | This option disables a pre-processing step that calculates [Strongly Connected Components](https://en.wikipedia.org/wiki/Strongly_connected_component), which are used for avoiding unnecessary work checking files in different SCCs for cycles.
|
|---|
| 100 |
|
|---|
| 101 | However, under some configurations, this pre-processing may be more expensive than the time it saves.
|
|---|
| 102 |
|
|---|
| 103 | When this option is `true`, we don't calculate any SCC graph, and check all files for cycles (leading to higher time-complexity). Default is `false`.
|
|---|
| 104 |
|
|---|
| 105 | ## When Not To Use It
|
|---|
| 106 |
|
|---|
| 107 | This rule is comparatively computationally expensive. If you are pressed for lint
|
|---|
| 108 | time, or don't think you have an issue with dependency cycles, you may not want
|
|---|
| 109 | this rule enabled.
|
|---|
| 110 |
|
|---|
| 111 | ## Further Reading
|
|---|
| 112 |
|
|---|
| 113 | - [Original inspiring issue](https://github.com/import-js/eslint-plugin-import/issues/941)
|
|---|
| 114 | - Rule to detect that module imports itself: [`no-self-import`]
|
|---|
| 115 | - [`import/external-module-folders`] setting
|
|---|
| 116 |
|
|---|
| 117 | [`no-self-import`]: ./no-self-import.md
|
|---|
| 118 |
|
|---|
| 119 | [`import/external-module-folders`]: ../../README.md#importexternal-module-folders
|
|---|