[workspace model] Fix module library remove after creation
[idea/community.git] / platform / projectModel-impl / src / com / intellij / workspaceModel / ide / impl / legacyBridge / module / roots / ModifiableModuleLibraryTableBridge.kt
index c66c480bcd888d53cf6fbe75e17094a7de288d45..ee306d689cfc5354a375b5bbb490c90150918312 100644 (file)
@@ -8,6 +8,7 @@ import com.intellij.openapi.roots.impl.ModuleLibraryTableBase
 import com.intellij.openapi.roots.libraries.Library
 import com.intellij.openapi.roots.libraries.PersistentLibraryKind
 import com.intellij.openapi.util.Disposer
+import com.intellij.util.containers.BidirectionalMap
 import com.intellij.workspaceModel.ide.WorkspaceModel
 import com.intellij.workspaceModel.ide.impl.legacyBridge.LegacyBridgeModifiableBase
 import com.intellij.workspaceModel.ide.impl.legacyBridge.library.LibraryBridge
@@ -21,7 +22,7 @@ import org.jetbrains.jps.model.serialization.library.JpsLibraryTableSerializer
 
 internal class ModifiableModuleLibraryTableBridge(private val modifiableModel: ModifiableRootModelBridgeImpl)
   : ModuleLibraryTableBase(), ModuleLibraryTableBridge {
-  private val copyToOriginal = HashMap<LibraryBridge, LibraryBridge>()
+  private val copyToOriginal = BidirectionalMap<LibraryBridge, LibraryBridge>()
 
   init {
     val storage = modifiableModel.moduleBridge.entityStorage.current
@@ -136,7 +137,10 @@ internal class ModifiableModuleLibraryTableBridge(private val modifiableModel: M
     modifiableModel.assertModelIsLive()
     library as LibraryBridge
 
-    val libraryEntity = modifiableModel.diff.findLibraryEntity(library)
+    val libraryEntity = modifiableModel.diff.findLibraryEntity(library) ?: run {
+      copyToOriginal.getKeysByValue(library)?.first()?.let { libraryCopy ->  modifiableModel.diff.findLibraryEntity(libraryCopy) }
+    }
+
     if (libraryEntity == null) {
       LOG.error("Cannot find entity for library ${library.name}")
       return