[Analysis API] use createSearchScopeByLibraryRoots for test libraries

As it is more similar to production. Also, fixed scope search for klib.
Now we register main binary modules before to avoid duplication with
libraries from createLibrariesByCompilerConfigurators as now we can
have a declared library and regular sources

^KT-62888
^KT-62651
This commit is contained in:
Dmitrii Gridin
2024-01-02 18:58:12 +01:00
committed by Space Team
parent 02d99769ff
commit 8de9d42b0d
6 changed files with 45 additions and 14 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -19,6 +19,7 @@ import org.jetbrains.kotlin.analysis.decompiler.konan.KlibMetaFileType
import org.jetbrains.kotlin.analysis.decompiler.psi.BuiltInDefinitionFile
import org.jetbrains.kotlin.analysis.decompiler.psi.KotlinBuiltInFileType
import org.jetbrains.kotlin.analysis.decompiler.psi.file.KtClsFile
import org.jetbrains.kotlin.analysis.project.structure.KtBinaryModule
import org.jetbrains.kotlin.analysis.providers.*
import org.jetbrains.kotlin.analysis.providers.impl.*
import org.jetbrains.kotlin.analysis.test.framework.project.structure.ktModuleProvider
@@ -66,10 +67,18 @@ object AnalysisApiBaseTestServiceRegistrar : AnalysisApiTestServiceRegistrar() {
override fun registerProjectModelServices(project: MockProject, testServices: TestServices) {
val moduleStructure = testServices.ktModuleProvider.getModuleStructure()
val allSourceKtFiles = moduleStructure.mainModules.flatMap { it.files.filterIsInstance<KtFile>() }
val binaryDependencies = moduleStructure.binaryModules.toMutableSet()
for (mainModule in moduleStructure.mainModules) {
val ktModule = mainModule.ktModule
if (ktModule !is KtBinaryModule) continue
binaryDependencies -= ktModule
}
val roots = StandaloneProjectFactory.getVirtualFilesForLibraryRoots(
moduleStructure.binaryModules.flatMap { binary -> binary.getBinaryRoots() },
binaryDependencies.flatMap { binary -> binary.getBinaryRoots() },
testServices.environmentManager.getProjectEnvironment()
).distinct()
project.apply {
registerService(KotlinAnnotationsResolverFactory::class.java, KotlinStaticAnnotationsResolverFactory(project, allSourceKtFiles))
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -53,6 +53,7 @@ import org.jetbrains.kotlin.cli.jvm.modules.CliJavaModuleResolver
import org.jetbrains.kotlin.cli.jvm.modules.CoreJrtFileSystem
import org.jetbrains.kotlin.cli.jvm.modules.JavaModuleGraph
import org.jetbrains.kotlin.config.*
import org.jetbrains.kotlin.library.KLIB_FILE_EXTENSION
import org.jetbrains.kotlin.load.java.structure.impl.source.JavaElementSourceFactory
import org.jetbrains.kotlin.load.kotlin.MetadataFinderFactory
import org.jetbrains.kotlin.load.kotlin.VirtualFileFinderFactory
@@ -374,7 +375,7 @@ object StandaloneProjectFactory {
return roots.mapNotNull { path ->
val pathString = FileUtil.toSystemIndependentName(path.toAbsolutePath().toString())
when {
pathString.endsWith(JAR_PROTOCOL) -> {
pathString.endsWith(JAR_PROTOCOL) || pathString.endsWith(KLIB_FILE_EXTENSION) -> {
environment.environment.jarFileSystem.findFileByPath(pathString + JAR_SEPARATOR)
}
@@ -1,13 +1,14 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.analysis.test.framework.project.structure
import com.intellij.openapi.project.Project
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.kotlin.analysis.api.standalone.base.project.structure.KtModuleWithFiles
import org.jetbrains.kotlin.analysis.api.standalone.base.project.structure.StandaloneProjectFactory
import org.jetbrains.kotlin.analysis.test.framework.services.environmentManager
import org.jetbrains.kotlin.analysis.test.framework.services.libraries.compiledLibraryProvider
import org.jetbrains.kotlin.analysis.test.framework.services.libraries.testModuleDecompiler
import org.jetbrains.kotlin.test.model.TestModule
@@ -25,12 +26,15 @@ object KtLibraryBinaryModuleFactory : KtModuleFactory {
KtLibraryModuleImpl(
testModule.name,
testModule.targetPlatform,
GlobalSearchScope.filesScope(project, decompiledFiles.mapTo(mutableSetOf()) { it.virtualFile }),
StandaloneProjectFactory.createSearchScopeByLibraryRoots(
listOf(library),
testServices.environmentManager.getProjectEnvironment(),
),
project,
binaryRoots = listOf(library),
librarySources = null,
),
decompiledFiles
decompiledFiles,
)
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -9,9 +9,11 @@ import com.intellij.openapi.project.Project
import com.intellij.psi.search.GlobalSearchScope
import org.jetbrains.kotlin.analysis.api.impl.base.util.LibraryUtils
import org.jetbrains.kotlin.analysis.api.standalone.base.project.structure.KtModuleWithFiles
import org.jetbrains.kotlin.analysis.api.standalone.base.project.structure.StandaloneProjectFactory
import org.jetbrains.kotlin.analysis.test.framework.services.environmentManager
import org.jetbrains.kotlin.analysis.test.framework.services.libraries.compiledLibraryProvider
import org.jetbrains.kotlin.test.model.TestModule
import org.jetbrains.kotlin.test.services.TestServices
import org.jetbrains.kotlin.analysis.test.framework.services.libraries.compiledLibraryProvider
import java.nio.file.Path
/**
@@ -28,15 +30,25 @@ object KtLibrarySourceModuleFactory : KtModuleFactory {
librarySourcesJar = librarySourcesJar,
testModule = testModule,
project = project,
testServices = testServices,
)
}
}
fun createKtLibrarySourceModule(libraryJar: Path, librarySourcesJar: Path, testModule: TestModule, project: Project): KtModuleWithFiles {
fun createKtLibrarySourceModule(
libraryJar: Path,
librarySourcesJar: Path,
testModule: TestModule,
project: Project,
testServices: TestServices,
): KtModuleWithFiles {
val libraryKtModule = KtLibraryModuleImpl(
testModule.name,
testModule.targetPlatform,
GlobalSearchScope.filesScope(project, LibraryUtils.getAllVirtualFilesFromJar(libraryJar)),
StandaloneProjectFactory.createSearchScopeByLibraryRoots(
listOf(libraryJar),
testServices.environmentManager.getProjectEnvironment(),
),
project,
binaryRoots = listOf(libraryJar),
librarySources = null,
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -58,6 +58,10 @@ object TestModuleStructureFactory {
// Not-under-content-root modules have no external dependencies on purpose
}
is KtModuleWithModifiableDependencies -> {
if (ktModule is KtBinaryModule) {
binaryModulesBySourceRoots.getOrPut(ktModule.getBinaryRoots().toSet()) { ktModule }
}
addModuleDependencies(testModule, moduleEntriesByName, ktModule)
stdlibAndSdkDependencies(ktModule, testModule, project, testServices).forEach { library ->
val cachedLibrary = binaryModulesBySourceRoots.getOrPut(library.getBinaryRoots().toSet()) { library }
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -33,6 +33,7 @@ private object KtStdlibSourceModuleFactory : KtModuleFactory {
librarySourcesJar = librarySourcesJar,
testModule = testModule,
project = project,
testServices = testServices,
)
}
}