{"record":{"id":"1d343d2621a9a879","repo":"oxc-project/oxc","slug":"a-module-importing-itself-is-not-allowed","errorCode":null,"errorMessage":"A module importing itself is not allowed","messagePattern":"A module importing itself is not allowed","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/import/no_self_import.rs","lineNumber":8,"sourceCode":"use oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::Span;\n\nuse crate::{context::LintContext, rule::Rule};\n\nfn no_self_import_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"A module importing itself is not allowed\")\n        .with_help(\"Remove this import. A module should not import itself.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct NoSelfImport;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Forbids a module from importing itself. This can sometimes happen accidentally,\n    /// especially during refactoring.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// Importing a module into itself creates a circular dependency, which can cause\n    /// runtime issues, including infinite loops, unresolved imports, or `undefined` values.\n    ///","sourceCodeStart":1,"sourceCodeEnd":26,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/import/no_self_import.rs#L1-L26","documentation":"Diagnostic from the oxlint rule import/no-self-import (suspicious category). It fires when a module imports its own resolved path — `import x from './this-same-file.js'`. A module already has direct access to its own declarations, so the import is always redundant, usually a rename/copy-paste leftover, and in CJS interop can yield an incomplete exports object. The help is blunt: remove the import.","triggerScenarios":"The module record's own loaded modules contain one whose resolved absolute path equals the importing file's path — reported from no_self_import_diagnostic at crates/oxc_linter/src/rules/import/no_self_import.rs:8. Typical producers: `import { foo } from './index'` inside index.ts, stale filenames after a rename, and duplicated files that kept their original imports.","commonSituations":"File copies used as templates (`cp user.dto.ts account.dto.ts` keeping `import { userSchema } from './user.dto'`); renames where the old name still resolves to the same file via alias or case-insensitive filesystems; barrel files importing themselves.","solutions":["Delete the self-importing statement; reference the local declarations directly","After renames, search the file for its own old basename and clean the leftovers","Audit tsconfig/bundler aliases for mappings that resolve a specifier back onto the same file"],"exampleFix":"// before (config.ts)\nimport { defaultConfig } from './config'; // same file\nexport const defaultConfig = { env: 'prod' };\n\n// after (config.ts)\nexport const defaultConfig = { env: 'prod' };","handlingStrategy":"validation","validationCode":"// npx madge --circular --extensions js,ts src/\n// rg -n \"from\\\\s+['\\\"]\\\\./index\" -g 'index.*'  (barrel self-imports)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["When copying a file as a template, delete its import block first","After renames, grep the file for its own old basename","Keep barrel index files free of imports from modules that import the barrel"],"tags":["lint","oxlint","imports","self-import","circular-dependency","refactoring","dead-code"],"backgroundTag":"module-self-import","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}