diff --git a/platform/lang-impl/src/com/intellij/workspaceModel/ide/impl/legacyBridge/library/ProjectModifiableLibraryTableBridgeImpl.kt b/platform/lang-impl/src/com/intellij/workspaceModel/ide/impl/legacyBridge/library/ProjectModifiableLibraryTableBridgeImpl.kt index b8a8d5af6c7b..6b6558160eb5 100644 --- a/platform/lang-impl/src/com/intellij/workspaceModel/ide/impl/legacyBridge/library/ProjectModifiableLibraryTableBridgeImpl.kt +++ b/platform/lang-impl/src/com/intellij/workspaceModel/ide/impl/legacyBridge/library/ProjectModifiableLibraryTableBridgeImpl.kt @@ -104,7 +104,16 @@ internal class ProjectModifiableLibraryTableBridgeImpl( override fun prepareForCommit() { assertModelIsLive() modelIsCommittedOrDisposed = true - myAddedLibraries.forEach { library -> library.clearTargetBuilder() } + val storage = WorkspaceModel.getInstance(project).entityStorage.current + myAddedLibraries.forEach { library -> + if (storage.resolve(library.libraryId) != null) { + // it may happen that actual library table already has a library with such name (e.g. when multiple projects are imported in parallel) + // in such case we need to skip the new library to avoid exceptions. + diff.removeEntity(diff.libraryMap.getEntities(library).first()) + Disposer.dispose(library) + } + library.clearTargetBuilder() + } } override fun getLibraryIterator(): Iterator = librariesArray.iterator() diff --git a/platform/lang-impl/testSources/com/intellij/openapi/roots/libraryTableTests.kt b/platform/lang-impl/testSources/com/intellij/openapi/roots/libraryTableTests.kt index 5947733ca720..8386ea8460f2 100644 --- a/platform/lang-impl/testSources/com/intellij/openapi/roots/libraryTableTests.kt +++ b/platform/lang-impl/testSources/com/intellij/openapi/roots/libraryTableTests.kt @@ -1,12 +1,15 @@ // Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.openapi.roots +import com.intellij.openapi.application.runWriteActionAndWait import com.intellij.openapi.extensions.ExtensionPointName import com.intellij.openapi.roots.impl.libraries.LibraryEx import com.intellij.openapi.roots.libraries.CustomLibraryTableDescription import com.intellij.openapi.roots.libraries.LibraryTable import com.intellij.openapi.roots.libraries.LibraryTablesRegistrar +import org.assertj.core.api.Assertions import org.junit.Before +import org.junit.Test class ProjectLibraryTableTest : LibraryTableTestCase() { override val libraryTable: LibraryTable @@ -15,6 +18,20 @@ class ProjectLibraryTableTest : LibraryTableTestCase() { override fun createLibrary(name: String, setup: (LibraryEx.ModifiableModelEx) -> Unit): LibraryEx { return projectModel.addProjectLevelLibrary(name, setup) } + + @Test + fun `do not add library with existing name`() { + val a = createLibrary("a") + val model1 = libraryTable.modifiableModel + val b = createLibrary("b", model1) + val model2 = libraryTable.modifiableModel + createLibrary("b", model2) + runWriteActionAndWait { + model1.commit() + model2.commit() + } + Assertions.assertThat(libraryTable.libraries).containsExactlyInAnyOrder(a, b) + } } class ApplicationLibraryTableTest : LibraryTableTestCase() {