{"record":{"id":"e9a8f853924c68f0","repo":"JetBrains/intellij-community","slug":"error-module-already-exists","errorCode":null,"errorMessage":"error.module.already.exists","messagePattern":"error\\.module\\.already\\.exists","errorType":"validation","errorClass":"ConfigurationException","httpStatus":null,"severity":"error","filePath":"java/java-impl-refactorings/src/com/intellij/refactoring/rename/RenameModuleAndDirectoryHandler.java","lineNumber":144,"sourceCode":"            }\n\n            @Override\n            protected @NotNull String getCommandName() {\n              return JavaRefactoringBundle.message(\"rename.module.directory.command\", newName);\n            }\n          };\n        }\n      };\n    }\n  }\n\n  private static void checkRenameModule(@NotNull Module module, @NotNull String newName) throws ConfigurationException {\n    final ModifiableModuleModel modifiableModel = ModuleManager.getInstance(module.getProject()).getModifiableModel();\n    try {\n      modifiableModel.renameModule(module, newName);\n    }\n    catch (ModuleWithNameAlreadyExists moduleWithNameAlreadyExists) {\n      throw new ConfigurationException(IdeBundle.message(\"error.module.already.exists\", newName));\n    }\n  }\n\n  private static void renameModule(@NotNull Module module, @NotNull String newName) {\n    if (module.isDisposed()) {\n      // happens if module gets removed while Usages Preview is shown\n    }\n    else {\n      try {\n        final ModifiableModuleModel model = ModuleManager.getInstance(module.getProject()).getModifiableModel();\n        model.renameModule(module, newName);\n        model.commit();\n      }\n      catch (ModuleWithNameAlreadyExists ignored) {\n        // happens if another module gets renamed while Usages Preview is shown\n      }\n    }\n  }","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/JetBrains/intellij-community/blob/be881553f2a76ac8b4ea53950d93f0de78295c19/java/java-impl-refactorings/src/com/intellij/refactoring/rename/RenameModuleAndDirectoryHandler.java#L126-L162","documentation":"Thrown by RenameModuleAndDirectoryHandler.checkRenameModule (RenameModuleAndDirectoryHandler.java:144) when renaming a module (Rename on an IDEA module node) and ModuleManager's ModifiableModuleModel.renameModule throws ModuleWithNameAlreadyExists — another module in the project already carries the target name. Module names must be unique project-wide because they key run configurations, facets, and inter-module dependencies.","triggerScenarios":"Rename (Shift+F6) an IntelliJ module to a name that matches any existing module in the same project. The handler does a dry-run rename on a throwaway ModifiableModel purely to detect the collision; the model is deliberately not committed on failure.","commonSituations":"Renaming a forked/duplicated module (e.g. 'app' -> 'app2' when 'app2' exists), unifying names after copying a module directory, or renaming to a name differing only by case on case-insensitive filesystems where the old module still registers.","solutions":["Choose a module name not used by any other module in the project (check Project Structure | Modules list first).","Rename or remove the conflicting module before retrying.","On case-only renames, rename to a temporary name first, then to the final case, to avoid collision with the old registration."],"exampleFix":"// before\n// project has modules: [app, app2]\n// rename 'app' -> 'app2'  -> ModuleWithNameAlreadyExists -> ConfigurationException\n\n// after\n// rename 'app' -> 'appLegacy' (unused) -> succeeds","handlingStrategy":"validation","validationCode":"boolean nameTaken = ModuleManager.getInstance(project).findModuleByName(newName) != null;\nif (nameTaken) {\n  // pick another module name before triggering Rename on the module node\n}","typeGuard":null,"tryCatchPattern":"catch (ModuleWithNameAlreadyExists e) when scripting ModifiableModuleModel.renameModule directly {\n  // report the collision and do NOT commit the modifiable model\n}","preventionTips":["Check ModuleManager.findModuleByName(newName) before any programmatic module rename.","For case-only renames on macOS/Windows, rename via a temporary intermediate name."],"tags":["intellij","refactoring","rename","module","name-collision"],"backgroundTag":null,"analyzedSha":"be881553f2a76ac8b4ea53950d93f0de78295c19","analyzedAt":"2026-08-14T14:13:06.425Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}