diff --git a/python/python-pyproject/src/com/intellij/python/pyproject/model/internal/workspaceBridge/workspaceTools.kt b/python/python-pyproject/src/com/intellij/python/pyproject/model/internal/workspaceBridge/workspaceTools.kt index d119b901db02..bf54bd3e6245 100644 --- a/python/python-pyproject/src/com/intellij/python/pyproject/model/internal/workspaceBridge/workspaceTools.kt +++ b/python/python-pyproject/src/com/intellij/python/pyproject/model/internal/workspaceBridge/workspaceTools.kt @@ -7,6 +7,7 @@ import com.intellij.openapi.util.NlsSafe import com.intellij.platform.backend.workspace.workspaceModel import com.intellij.platform.workspace.jps.JpsImportedEntitySource import com.intellij.platform.workspace.jps.entities.ContentRootEntity +import com.intellij.platform.workspace.jps.entities.ContentRootEntityBuilder import com.intellij.platform.workspace.jps.entities.DependencyScope import com.intellij.platform.workspace.jps.entities.ExcludeUrlEntity import com.intellij.platform.workspace.jps.entities.ExternalSystemModuleOptionsEntity @@ -70,10 +71,27 @@ internal suspend fun rebuildProjectModel(project: Project, files: FSWalkInfoWith renameSameModule(syncStorage, projectStorage) relocateFacetAndSdk(syncStorage, projectStorage) projectStorage.replaceBySource({ it.isPythonEntity }, syncStorage) + + // For some reason, WSM duplicates sources instead of merging them, so we remove duplicates + for (moduleEntity in projectStorage.entities(ModuleEntity::class.java).filter { it.entitySource.isPythonEntity }) { + for (contentRoot in moduleEntity.contentRoots) { + if (contentRoot.sourceRoots.size > 1) { + projectStorage.modifyContentRootEntity(contentRoot) { + removeSrcDuplicates() + } + } + } + } + } } } +private fun ContentRootEntityBuilder.removeSrcDuplicates() { + val newSourceRoots = sourceRoots.distinctBy { it.url } + sourceRoots = newSourceRoots +} + private fun renameSameModule(syncStorage: EntityStorage, projectStorage: MutableEntityStorage) { // TODO: fix O(N^2) for (syncModuleEntity in syncStorage.entities()) {