← Back to Pull requests

fix(utils): avoid module import binding conflicts #2498

Open
unknown opened this pull request with 286 lines diff across 3 files
View on GitHub
unknown unknown commented on Oct 15, 2026 13:48 UTC
Author

PR Checklist

  • The commit message follows our [guidelines](https://github.com/nestjs/nest/blob/master/CONTRIBUTING.md).

  • Tests for the changes have been added.

  • Docs have been added / updated (not applicable; no public API changes).
  • PR Type

  • Bugfix
  • What is the current behavior?

    Generating a service or another module declaration can add an import whose local identifier is already bound, resulting in duplicate identifier errors or module metadata referencing the wrong class.

    Previous attempts in #2486 and #2495 addressed import duplication but were closed after review identified problems with aliases, type-only imports, different module paths, and comments.

    This PR follows the AST-based approach suggested in the [review of #2495](https://github.com/nestjs/schematics/pull/2495#issuecomment-6141699618).

    Issue Number: N/A (follow-up to #2486 and #2495)

    What is the new behavior?

    ModuleImportDeclarator uses the TypeScript AST to inspect existing imports and local declarations before adding a generated import.

  • Reuses an existing matching value import when its local binding is unambiguous.

  • Generates a unique alias when the requested identifier conflicts with an existing declaration.

  • Propagates the selected local identifier to module metadata, preventing references to the wrong class.

  • Distinguishes type-only imports from runtime value imports.

  • Uses AST positions to insert imports, avoiding false matches inside comments.

  • Preserves existing metadata handling and idempotence.
  • Import reuse is based on the generated module path and safe local bindings, without introducing cross-module symbol resolution.

    Does this PR introduce a breaking change?

  • Yes

  • No
  • Other information

    Added regression coverage for import variants, conflicting and shadowed bindings, comments, idempotence, and schematic rollback on generation failure.

    Tests also compile generated TypeScript and execute module decorators to verify that the intended provider constructor is registered.

    Validation: 884 tests across 45 files, typecheck, build, lint, Prettier, and git diff --check passed.

    Diff: 286 lines (+214 / -72) across 3 files Reviewer: kamilmysliwiec Review duration: 63464m
    All Vouch review confidence checks passed
    Residual risk 0.26 = Change Risk (0.47) × (1 − Review Confidence (0.45)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.47 · 286 lines diff across 3 files
    0.47
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    44%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Shallow Review Depth shallow_review_depth
    0.344
    Diff Size diff_size
    0.339
    Real PR Conversation Timeline
    1 comments
    "Review active on #2498"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 44.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 46.8% (MEDIUM) · Higher = larger blast radius
    46.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 44.7% · Lower = rushed or shallow review
    44.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 25.9% · Safe to merge
    25.9%