Skip to content
This repository was archived by the owner on Aug 5, 2024. It is now read-only.

Commit e6ad2a0

Browse files
abrams27Space Team
authored andcommitted
[fix] kotlin stdlibs now are obtained from toolchain instead of classpath
now its project level lib review fix [fix] kotlin stdlibs now are obtained from toolchain instead of classpath Merge-request: BAZEL-MR-667 Merged-by: Marcin Abramowicz <[email protected]>
1 parent a10ecb0 commit e6ad2a0

File tree

5 files changed

+33
-33
lines changed

5 files changed

+33
-33
lines changed

aspects/rules/jvm/jvm_info.bzl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,6 @@ def extract_jvm_info(target, ctx, output_groups, **kwargs):
136136
resolve_files += compile_jars
137137
resolve_files += source_jars
138138

139-
compile_classpath = map(file_location, compile_jars)
140-
141139
javac_opts = getattr(ctx.rule.attr, "javacopts", [])
142140
jvm_flags = getattr(ctx.rule.attr, "jvm_flags", [])
143141
args = getattr(ctx.rule.attr, "args", [])
@@ -149,7 +147,6 @@ def extract_jvm_info(target, ctx, output_groups, **kwargs):
149147
info = create_struct(
150148
jars = jars,
151149
generated_jars = generated_jars,
152-
compile_classpath = compile_classpath,
153150
javac_opts = javac_opts,
154151
jvm_flags = jvm_flags,
155152
main_class = main_class,

aspects/rules/kt/kt_info.bzl.template

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
load("@${ruleName}//kotlin/internal:defs.bzl", "KtJvmInfo")
22
load("@${ruleName}//kotlin/internal:opts.bzl", "KotlincOptions")
3-
load("//aspects:utils/utils.bzl", "convert_struct_to_dict", "create_proto", "create_struct")
3+
load("//aspects:utils/utils.bzl", "convert_struct_to_dict", "create_proto", "create_struct", "map", "file_location")
44

55
KOTLIN_TOOLCHAIN_TYPE = "@${ruleName}//kotlin/internal:kt_toolchain_type"
66

@@ -21,10 +21,14 @@ def extract_kotlin_info(target, ctx, **kwargs):
2121
kotlinc_opts_target = getattr(ctx.rule.attr, "kotlinc_opts", None)
2222
kotlinc_opts = kotlinc_opts_target[KotlincOptions] if kotlinc_opts_target and KotlincOptions in kotlinc_opts_target else toolchain_kotlinc_opts
2323

24+
stdlibs_files = kotlin_toolchain.jvm_stdlibs.compile_jars.to_list()
25+
stdlibs = map(file_location, stdlibs_files)
26+
2427
kotlin_info = dict(
2528
language_version = language_version,
2629
api_version = api_version,
2730
associates = associates_labels,
31+
stdlibs = stdlibs,
2832
)
2933

3034
kotlinc_opts_dict = convert_struct_to_dict(kotlinc_opts)

server/src/main/kotlin/org/jetbrains/bsp/bazel/server/sync/BazelProjectMapper.kt

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ import java.nio.file.Paths
3030
import kotlin.io.path.exists
3131
import kotlin.io.path.notExists
3232

33-
val KOTLIN_STDLIB_ROOT_EXECUTION_REGEX =
34-
"""external/(rules_kotlin~.*~rules_kotlin_extensions~|)com_github_jetbrains_kotlin""".toRegex()
35-
const val KOTLIN_STDLIB_RELATIVE_PATH_PREFIX = "lib/"
36-
3733
class BazelProjectMapper(
3834
private val languagePluginsService: LanguagePluginsService,
3935
private val bazelPathsResolver: BazelPathsResolver,
@@ -125,14 +121,34 @@ class BazelProjectMapper(
125121
}
126122

127123
private fun calculateKotlinStdlibsMapper(targetsToImport: Sequence<TargetInfo>): Map<String, List<Library>> {
128-
val projectLevelKotlinStdlibs = calculateProjectLevelKotlinStdlibs(targetsToImport)
129-
val rulesKotlinTargets = targetsToImport
130-
.filter { targetInfo -> targetInfo.jvmTargetInfo.compileClasspathList.any { it.isKotlinStdlibPath() } }
131-
.map { it.id }
132-
.toSet()
133-
return rulesKotlinTargets.associateWith { listOf(projectLevelKotlinStdlibs) }
124+
val projectLevelKotlinStdlibsLibrary = calculateProjectLevelKotlinStdlibsLibrary(targetsToImport)
125+
val kotlinTargetsIds = targetsToImport.filter { it.hasKotlinTargetInfo() }.map { it.id }
126+
127+
return projectLevelKotlinStdlibsLibrary
128+
?.let { stdlibsLibrary -> kotlinTargetsIds.associateWith { listOf(stdlibsLibrary) } }
129+
.orEmpty()
130+
}
131+
132+
private fun calculateProjectLevelKotlinStdlibsLibrary(targetsToImport: Sequence<TargetInfo>): Library? {
133+
val kotlinStdlibsJars = calculateProjectLevelKotlinStdlibsJars(targetsToImport)
134+
135+
return if (kotlinStdlibsJars.isNotEmpty()) {
136+
Library(
137+
label = "rules_kotlin_kotlin-stdlibs",
138+
outputs = kotlinStdlibsJars,
139+
sources = emptySet(),
140+
dependencies = emptyList(),
141+
)
142+
} else null
134143
}
135144

145+
private fun calculateProjectLevelKotlinStdlibsJars(targetsToImport: Sequence<TargetInfo>): Set<URI> =
146+
targetsToImport
147+
.filter { it.hasKotlinTargetInfo() }
148+
.map { it.kotlinTargetInfo.stdlibsList }
149+
.flatMap { it.resolveUris() }
150+
.toSet()
151+
136152
/**
137153
* In some cases, the jar dependencies of a target might be injected by bazel or rules and not are not
138154
* available via `deps` field of a target. For this reason, we read JavaOutputInfo's jdeps file and
@@ -226,23 +242,6 @@ class BazelProjectMapper(
226242
return Paths.get(lib).fileName.toString().replace("[^0-9a-zA-Z]".toRegex(), "-") + "-" + shaOfPath
227243
}
228244

229-
private fun calculateProjectLevelKotlinStdlibs(targets: Sequence<TargetInfo>) =
230-
Library(
231-
label = "rules_kotlin_kotlin-stdlibs",
232-
outputs = targets
233-
.flatMap { it.jvmTargetInfo.compileClasspathList }
234-
.filter { it.isKotlinStdlibPath() }
235-
.map { bazelPathsResolver.resolveUri(it) }
236-
.toSet(),
237-
sources = emptySet(),
238-
dependencies = emptyList(),
239-
)
240-
241-
private fun FileLocation.isKotlinStdlibPath() =
242-
KOTLIN_STDLIB_ROOT_EXECUTION_REGEX.matches(this.rootExecutionPathFragment) &&
243-
this.relativePath.startsWith(KOTLIN_STDLIB_RELATIVE_PATH_PREFIX)
244-
245-
246245
private fun createLibraries(targets: Map<String, TargetInfo>): Map<String, Library> {
247246
return targets.mapValues { entry ->
248247
val targetId = entry.key

server/src/main/kotlin/org/jetbrains/bsp/bazel/server/sync/languages/java/JavaLanguagePlugin.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class JavaLanguagePlugin(
3939
it.interfaceJarsList + it.binaryJarsList
4040
}.map(bazelPathsResolver::resolveUri)
4141
val mainClass = getMainClass(this)
42-
val compileClasspath = bazelPathsResolver.resolveUris(compileClasspathList + generatedJarsList.flatMap { it.binaryJarsList }, true)
42+
val compileClasspath = bazelPathsResolver.resolveUris(generatedJarsList.flatMap { it.binaryJarsList }, true)
4343
val runtimeJdk = jdkResolver.resolveJdk(targetInfo)
4444

4545
JavaModule(

server/src/main/kotlin/org/jetbrains/bsp/bazel/server/sync/proto/bsp_target_info.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ message JvmOutputs {
3030
message JvmTargetInfo {
3131
repeated JvmOutputs jars = 1;
3232
repeated JvmOutputs generated_jars = 2;
33-
repeated FileLocation compile_classpath = 4;
3433
repeated string javac_opts = 6;
3534
repeated string jvm_flags = 7;
3635
string main_class = 8;
@@ -68,6 +67,7 @@ message KotlinTargetInfo {
6867
string api_version = 2;
6968
repeated string associates = 3;
7069
repeated string kotlinc_opts = 4;
70+
repeated FileLocation stdlibs = 5;
7171
}
7272

7373
message PythonTargetInfo {

0 commit comments

Comments
 (0)