[JPS] Fix KotlinJvmModuleBuildTarget.findJavaSourceRoots to only return Java source roots

This change is needed because otherwise, source files that are not Java will be added as `JavaSourceRoot`, this is incorrect and causes assertion error in `com.intellij.core.JavaCoreProjectEnvironment.addSourcesToClasspath`. Refer to KT-65325 for more context.


Merge-request: KT-MR-14088
Merged-by: Xuan Son Trinh <xuanson.trinh@jetbrains.com>
This commit is contained in:
Xuan-Son Trinh
2024-01-31 16:52:42 +00:00
committed by Space Team
parent d69deb97fe
commit f4f009cb9a
5 changed files with 39 additions and 3 deletions
@@ -202,7 +202,7 @@ class KotlinJvmModuleBuildTarget(kotlinContext: KotlinCompileContext, jpsModuleB
kotlinModuleId.name,
outputDir.absolutePath,
preprocessSources(allFiles),
target.findSourceRoots(dirtyFilesHolder.context),
target.findJavaSourceRoots(dirtyFilesHolder.context),
target.findClassPathRoots(),
preprocessSources(commonSourceFiles),
target.findModularJdkRoot(),
@@ -327,13 +327,14 @@ class KotlinJvmModuleBuildTarget(kotlinContext: KotlinCompileContext, jpsModuleB
return File(url.substringAfter(URLUtil.JRT_PROTOCOL + URLUtil.SCHEME_SEPARATOR).substringBeforeLast(URLUtil.JAR_SEPARATOR))
}
private fun findSourceRoots(context: CompileContext): List<JvmSourceRoot> {
private fun findJavaSourceRoots(context: CompileContext): List<JvmSourceRoot> {
val roots = context.projectDescriptor.buildRootIndex.getTargetRoots(jpsModuleBuildTarget, context)
val result = mutableListOf<JvmSourceRoot>()
for (root in roots) {
val file = root.rootFile
val filePath = file.toPath()
val prefix = root.packagePrefix
if (Files.exists(file.toPath())) {
if (Files.exists(filePath) && (Files.isDirectory(filePath) || file.extension == "java")) {
result.add(JvmSourceRoot(file, prefix.ifEmpty { null }))
}
}