[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
@@ -946,6 +946,12 @@ open class KotlinJpsBuildTest : KotlinJpsBuildTestBase() {
checkWhen(emptyArray(), null, packageClasses("kotlinProject", "src/test1.kt", "Test1Kt"))
}
@WorkingDir("KotlinProjectWithSingleKotlinFileAsSourceRoot")
fun testBuildProjectWithSingleKotlinFileAsSource() {
initProject(JVM_MOCK_RUNTIME)
buildAllModules().assertSuccessful()
}
fun testBuildAfterGdwBuild() {
initProject(JVM_FULL_RUNTIME)
findModule("module2").let {
@@ -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 }))
}
}
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$/src/test1.kt">
<sourceFolder url="file://$MODULE_DIR$/src/test1.kt" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="IDEA_JDK" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="kotlinProject" />
</component>
</module>
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<option name="DEFAULT_COMPILER" value="Javac" />
</component>
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/kotlinProject.iml" filepath="$PROJECT_DIR$/kotlinProject.iml" />
</modules>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" assert-keyword="true" jdk-15="true" project-jdk-name="IDEA_JDK" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
@@ -0,0 +1,3 @@
fun foo() {
}