Skip to content

Conversation

@lmurri
Copy link

@lmurri lmurri commented Feb 6, 2026

PR Info

New Features

N/A

Bug Fixes

  • Duplicate enum imports when regenerating entities with --expanded-format

Original Bug Analysis

When using sea-orm-cli generate entity with --expanded-format and preserve_user_modifications enabled, duplicate enum imports were created if the old file had grouped imports and the new file generated individual imports.

For example, if the old file had:

use super::sea_orm_active_enums::{Status, Category};

And the new file generated:

use super::sea_orm_active_enums::Status;
use super::sea_orm_active_enums::Category;

The merge would incorrectly produce all three import lines instead of just the two individual imports.

Root Cause

The merge logic used structural equality (HashSet<ItemUse>) to deduplicate imports. This failed to recognize that grouped imports and individual imports for the same paths are semantically equivalent and should be deduplicated.

Fix

  • Added normalize_use_paths() function to flatten use statements into canonical import paths
  • Changed from HashSet<ItemUse> to HashSet<String> for semantic path comparison
  • Use statements are now included only if they contain at least one new import path

Breaking Changes

N/A — this fix maintains backward compatibility while improving merge behavior.

Changes

  • Modified sea-orm-codegen/src/merge/mod.rs:
    • Added semantic import path deduplication
    • Replaced structural equality with path-based comparison
    • Added comprehensive test case duplicate_enum_imports_grouped_vs_individual

When regenerating entities with --expanded-format and preserve_user_modifications,
duplicate enum imports are created when the old file has grouped imports like:
  use super::sea_orm_active_enums::{Status, Category};

And the new file generates individual imports:
  use super::sea_orm_active_enums::Status;
  use super::sea_orm_active_enums::Category;

The result incorrectly includes both the grouped and individual imports.

The bug occurs because structural equality (HashSet<ItemUse>) is used to
deduplicate imports instead of semantic comparison of actual import paths.

This test is marked with #[should_panic] and will pass normally once fixed.
Previously, import deduplication used structural equality (HashSet<ItemUse>)
which failed to recognize that grouped imports like:
  use super::sea_orm_active_enums::{Status, Category};

And individual imports like:
  use super::sea_orm_active_enums::Status;
  use super::sea_orm_active_enums::Category;

represent the same import paths and should be deduplicated.

This fix:
- Adds normalize_use_paths() to flatten use statements into canonical paths
- Changes Merger.seen_use from HashSet<ItemUse> to seen_paths: HashSet<String>
- Updates deduplication logic to track individual import paths semantically
- Use statements are included only if they contain at least one new path

The implementation uses a simple nested function to recursively collect paths
from the UseTree, handling Path, Name, Rename, Glob, and Group variants.

All existing tests pass, and the new test now passes without #[should_panic].
@lmurri lmurri force-pushed the fix/duplicate-enum-imports branch from 87dacc5 to 61021c6 Compare February 6, 2026 22:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enum imports duplicated when merging grouped and individual use statements

1 participant