|
| 1 | +# Dual Package Hazard Diagnostics |
| 2 | + |
| 3 | +This tool can warn or error when a file mixes specifiers that may trigger the dual package hazard (ESM vs CJS instances of the same package). |
| 4 | + |
| 5 | +## Option |
| 6 | + |
| 7 | +- `detectDualPackageHazard`: `off` | `warn` (default) | `error` |
| 8 | + - CLI: `--detect-dual-package-hazard`, short `-H`. |
| 9 | + - `warn`: emit diagnostics but continue. |
| 10 | + - `error`: diagnostics are emitted and the transform exits non-zero. |
| 11 | + - `off`: skip detection. |
| 12 | + |
| 13 | +## What we detect (per file) |
| 14 | + |
| 15 | +- Mixed import/require of the same bare package (including subpaths). |
| 16 | + - Diagnostic: `dual-package-mixed-specifiers`. |
| 17 | +- Root vs subpath specifiers of the same package (e.g., `pkg` and `pkg/module`). |
| 18 | + - Diagnostic: `dual-package-subpath`. |
| 19 | +- When both import and require occur, and package.json shows divergent entrypoints (conditional exports, module/main disagreements, or type: module with CJS main). |
| 20 | + - Diagnostic: `dual-package-conditional-exports`. |
| 21 | + |
| 22 | +## How it works |
| 23 | + |
| 24 | +- Static string specifiers only (import/export-from, import(), require literals). |
| 25 | +- Computes the package root from bare specifiers (ignores relative/absolute, node: builtins, URLs). |
| 26 | +- Looks up package.json under `node_modules/<pkg>` relative to the current file/cwd when available. |
| 27 | +- Best-effort: if the manifest cannot be read, the manifest-based diagnostic is skipped. |
| 28 | + |
| 29 | +## What is not covered |
| 30 | + |
| 31 | +- Cross-file or whole-project graph analysis; detection is per file only. |
| 32 | +- Dynamic or template specifiers; non-literal specifiers are ignored. |
| 33 | +- Loader/bundler resolution differences (pnpm linking, aliases, custom conditions). |
| 34 | +- Exact equality of root vs subpath targets; we do not stat/resolve to see if they point to the same file, so a root/subpath warning may be conservative. |
| 35 | + |
| 36 | +## Guidance |
| 37 | + |
| 38 | +- Prefer a single specifier form for a given package: either all import or all require, and avoid mixing root and subpath unless you know they share the same build. |
| 39 | +- Use `-H error` (or `detectDualPackageHazard: 'error'`) in CI to block new hazards once noise is acceptable for your codebase. |
| 40 | +- If you need to suppress noise temporarily, set the option to `warn` while you align specifiers or package metadata. |
0 commit comments