From d088aa9e00b6ca326521c18d4c200c4244d9d20d Mon Sep 17 00:00:00 2001 From: "Ilya.Kazakevich" Date: Tue, 24 Feb 2026 00:20:20 +0100 Subject: [PATCH] PY-87889: Fix src duplication. 1. `.iml` file has source `src` 2. so does the new `ModuleEntity` 3. `replaceBySource` expected to merge them, but it duplicates them instead. We now explicilty remove duplicates https://jetbrains.slack.com/archives/C017MGPV4S0/p1771886428685089 (cherry picked from commit e7b26125b3e39d1b603310b9c14899bd28aea2fa) IJ-CR-193095 GitOrigin-RevId: af5940fd61994cb535c25a4c94781a6473a7554a --- .../internal/workspaceBridge/workspaceTools.kt | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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()) {