[Workspace Model] Fix loading of empty iml file

GitOrigin-RevId: 365f3968f2977e26dc99f50fca53aad55b72f353
This commit is contained in:
Alex Plate
2023-01-23 15:20:56 +02:00
committed by intellij-monorepo-bot
parent c11cd69252
commit d000a0ca2a
4 changed files with 30 additions and 26 deletions

View File

@@ -35,12 +35,12 @@ inline fun <T> JpsFileEntitiesSerializer<*>.runCatchingXmlIssues(body: () -> T):
@Suppress("UnusedReceiverParameter")
@OptIn(ExperimentalContracts::class)
inline fun <T> JpsFileEntitiesSerializer<*>.runCatchingXmlIssues(exceptionsCollector: MutableCollection<Throwable>,body: () -> T) {
inline fun <T> JpsFileEntitiesSerializer<*>.runCatchingXmlIssues(exceptionsCollector: MutableCollection<Throwable>,body: () -> T): T? {
contract {
callsInPlace(body, InvocationKind.EXACTLY_ONCE)
}
try {
body()
return body()
}
catch (e: JDOMException) {
exceptionsCollector.add(e)
@@ -48,4 +48,5 @@ inline fun <T> JpsFileEntitiesSerializer<*>.runCatchingXmlIssues(exceptionsColle
catch (e: IOException) {
exceptionsCollector.add(e)
}
return null
}

View File

@@ -149,19 +149,22 @@ internal open class ModuleImlFileEntitiesSerializer(internal val modulePath: Mod
moduleEntity: ModuleEntity.Builder,
exceptionCollector: MutableList<Throwable>,
): ModuleEntity.Builder {
val source = getOtherEntitiesEntitySource(reader)
if (moduleEntity.isEmpty) {
val newModuleEntity = ModuleEntity(moduleEntity.name, emptyList(), OrphanageWorkerEntitySource) as ModuleEntity.Builder
runCatchingXmlIssues(exceptionCollector) {
loadAdditionalContentRoots(newModuleEntity, reader, virtualFileManager, source)
}
return newModuleEntity
val source = runCatchingXmlIssues(exceptionCollector) {
getOtherEntitiesEntitySource(reader)
} ?: return moduleEntity
val roots = runCatchingXmlIssues(exceptionCollector) {
loadAdditionalContentRoots(moduleEntity, reader, virtualFileManager, source)
}
else {
runCatchingXmlIssues(exceptionCollector) {
loadAdditionalContentRoots(moduleEntity, reader, virtualFileManager, source)
}
return moduleEntity
if (roots == null) return moduleEntity
return if (moduleEntity.isEmpty) {
ModuleEntity(moduleEntity.name, emptyList(), OrphanageWorkerEntitySource) {
this.contentRoots = roots
} as ModuleEntity.Builder
} else {
moduleEntity.contentRoots += roots
moduleEntity
}
}
@@ -302,13 +305,9 @@ internal open class ModuleImlFileEntitiesSerializer(internal val modulePath: Mod
reader: JpsFileContentReader,
virtualFileManager: VirtualFileUrlManager,
contentRootEntitySource: EntitySource,
) {
val additionalElements = reader.loadComponent(fileUrl.url, ADDITIONAL_MODULE_ELEMENTS_COMPONENT_NAME, getBaseDirPath())?.clone()
if (additionalElements != null) {
val additionalContentRoots = loadContentRootEntities(moduleEntity, additionalElements, virtualFileManager, contentRootEntitySource)
moduleEntity.contentRoots += additionalContentRoots
}
): List<ContentRootEntity>? {
val additionalElements = reader.loadComponent(fileUrl.url, ADDITIONAL_MODULE_ELEMENTS_COMPONENT_NAME, getBaseDirPath())?.clone() ?: return null
return loadContentRootEntities(moduleEntity, additionalElements, virtualFileManager, contentRootEntitySource)
}
private fun getOtherEntitiesEntitySource(reader: JpsFileContentReader): EntitySource {

View File

@@ -543,8 +543,8 @@ class JpsSplitModuleAndContentRootTest {
@TestFor(classes = [JavaModuleSettingsEntity::class, ModuleImlFileEntitiesSerializer::class, JavaSettingsSerializer::class])
@Test
fun `load module without java custom settings`() {
checkSaveProjectAfterChange("after/imlWithoutJavaSettings", "after/imlWithoutJavaSettings") { _, orphanage, _ ->
val javaSettings = orphanage.entities(ModuleEntity::class.java).single().javaSettings
checkSaveProjectAfterChange("after/imlWithoutJavaSettings", "after/imlWithoutJavaSettings") { builder, orphanage, _ ->
val javaSettings = builder.entities(ModuleEntity::class.java).single().javaSettings
assertNull(javaSettings)
}
}

View File

@@ -33,7 +33,7 @@ import org.junit.jupiter.api.io.TempDir
import java.nio.file.Path
import java.util.*
import kotlin.test.assertContains
import kotlin.test.assertFalse
import kotlin.test.fail
// TODO: Cover case in com.intellij.workspaceModel.ide.impl.jps.serialization.JpsSplitModuleAndContentRoot.load module without java custom settings
@@ -93,11 +93,15 @@ class ImlCreationPropertyTest {
if (moduleEntity.isEmpty) {
if (moduleEntity.isExternal) {
val file = prj.cache.modules.resolve("${moduleEntity.name}.xml").toFile()
assertFalse(file.exists(), "File should not exist ${file}. Content: ${file.readText()}")
if (file.exists()) {
fail("File should not exist ${file}. Content: ${file.readText()}")
}
}
else {
val file = prj.resolve("${moduleEntity.name}.iml").toFile()
assertFalse(file.exists(), "File should not exist ${file}. Content: ${file.readText()}")
if (file.exists()) {
fail("File should not exist ${file}. Content: ${file.readText()}")
}
}
}
else {