oxc-project/oxc · warning · OxcDiagnostic

Modules should not be imported multiple times in the same fi

Error message

Modules should not be imported multiple times in the same file

What it means

Long-name variant of the import/no-duplicates message (style category). When the same module is imported more than once in a file and the specifier is longer than 16 characters, this generic message is used instead of embedding the module name, keeping the diagnostic readable. Imports are grouped by resolved absolute path (query strings stripped unless considerQueryString), and type imports are kept separate from value imports unless preferInline is set.

Source

Thrown at crates/oxc_linter/src/rules/import/no_duplicates.rs:34

use crate::{
    context::LintContext,
    fixer::{RuleFix, RuleFixer},
    module_record::{ImportImportName, RequestedModule},
    rule::{DefaultRuleConfig, Rule},
};

fn no_duplicates_diagnostic<I>(
    module_name: &str,
    first_import: Span,
    other_imports: I,
) -> OxcDiagnostic
where
    I: IntoIterator<Item = Span>,
{
    const MAX_MODULE_LEN: usize = 16;

    let message = if module_name.len() > MAX_MODULE_LEN {
        Cow::Borrowed("Modules should not be imported multiple times in the same file")
    } else {
        Cow::Owned(format!("Module '{module_name}' is imported more than once in this file"))
    };
    let labels = std::iter::once(first_import.primary_label("It is first imported here"))
        .chain(other_imports.into_iter().map(LabeledSpan::underline));

    OxcDiagnostic::warn(message)
        .with_labels(labels)
        .with_help("Merge these imports into a single import statement")
}

// <https://github.com/import-js/eslint-plugin-import/blob/v2.29.1/docs/rules/no-duplicates.md>
#[derive(Debug, Default, Clone, JsonSchema, Deserialize)]
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
pub struct NoDuplicates {
    /// When set to `true`, prefer inline type imports instead of separate type import
    /// statements for TypeScript code.
    ///

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Run `oxlint --fix` (the rule ships a conditional fix that merges the statements) or merge manually into one statement: `import { a, b } from './mod'`
  2. For TypeScript, enable `preferInline: true` so `import { A, type B }` style merging is suggested instead of separate type statements
  3. If query strings are meaningful (webpack loaders), enable `considerQueryString: true` so different options are not flagged

Example fix

// before
import { fetchUser } from './userRepository';
import { deleteUser } from './userRepository';

// after
import { fetchUser, deleteUser } from './userRepository';
Defensive patterns

Strategy: validation

Validate before calling

// .oxlintrc.json
{ "rules": { "import/no-duplicates": ["warn", { "preferInline": true, "considerQueryString": true }] } }

Prevention

When it happens

Trigger: Two or more import statements in one file resolving to the same absolute path where the specifier exceeds MAX_MODULE_LEN (16), e.g. `import { a } from './some-long-module-name'` and `import { b } from './some-long-module-name'`. Emitted from no_duplicates_diagnostic at crates/oxc_linter/src/rules/import/no_duplicates.rs:34.

Common situations: Large files accumulated by many authors; merging codemod output; webpack loader query strings (`./bar?optionX` vs `./bar?optionY`) being treated as the same module because considerQueryString defaults to false; separate `import type` statements (not duplicates by default).

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/cb0a8a8e6501631c. Report an issue: GitHub.