[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,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 }