Skip to content

Commit

Permalink
[fix] fix pure kotlin projects for bazel 8 | #BAZEL-1562
Browse files Browse the repository at this point in the history
  • Loading branch information
abrams27 authored and Space Team committed Jan 23, 2025
1 parent c91a8fa commit cd202b8
Show file tree
Hide file tree
Showing 35 changed files with 415 additions and 175 deletions.
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ server/e2e/test-projects/bzlmod/allow-manual-targets-sync-project/bazel-allow-ma
server/e2e/test-projects/bzlmod/bazel-build-and-sync-test
server/e2e/test-projects/bzlmod/first-phase-sync-project/bazel-first-phase-sync-project
server/e2e/test-projects/bzlmod/java-diagnostics-project/bazel-java-diagnostics-project
server/e2e/test-projects/bzlmod/kotlin-project/bazel-kotlin-project
server/e2e/test-projects/bzlmod/local-jdk-project/bazel-local-jdk-project
server/e2e/test-projects/bzlmod/nested-modules/bazel-nested-modules
server/e2e/test-projects/bzlmod/nested-modules/inner/bazel-inner
Expand Down
4 changes: 2 additions & 2 deletions .bazelrc

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ plugin-bazel/src/test/testData/**/*.bzl rules-lint-ignored
.teamcity/** rules-lint-ignored
plugin-bazel/src/main/kotlin/org/jetbrains/bazel/languages/bazelrc/lexer/_BazelrcLexer.java rules-lint-ignored
plugin-bazel/src/main/kotlin/org/jetbrains/bazel/languages/starlark/lexer/_StarlarkLexer.java rules-lint-ignored
# exclude test projects for e2e tests
server/e2e/test-projects/** rules-lint-ignored
3 changes: 0 additions & 3 deletions ktlint-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
<file name="sdkcompat/v251/org/jetbrains/bsp/sdkcompat/Dummy.kt">
<error line="1" column="1" source="standard:no-empty-file" />
</file>
<file name="server/e2e/test-projects/workspace/kotlin-project/plugin_allopen_test/User.kt">
<error line="3" column="1" source="standard:no-wildcard-imports" />
</file>
<file name="server/example/kotlincplugin/User.kt">
<error line="3" column="1" source="standard:no-wildcard-imports" />
</file>
Expand Down
2 changes: 1 addition & 1 deletion server/aspects/rules/go/go_info.bzl.template
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@${rulesetName}//go:def.bzl", "GoArchive", "GoSource", "go_context")
load("//aspects:utils/utils.bzl", "create_struct", "file_location")
load("@${ruleName}//go:def.bzl", "go_context", "GoArchive", "GoSource")

def extract_go_info(target, ctx, **kwargs):
if GoArchive not in target:
Expand Down
44 changes: 24 additions & 20 deletions server/aspects/rules/java/java_info.bzl.template
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
load("//aspects:utils/utils.bzl", "create_struct", "to_file_location")
#if( $javaEnabled == "true" && $bazel8OrAbove == "true" )
load("@${ruleName}//java/common:java_info.bzl", "JavaInfo")
load("@${rulesetName}//java/common:java_info.bzl", "JavaInfo")
#end

load("//aspects:utils/utils.bzl", "create_struct", "to_file_location")

def java_info_in_target(target):
#if( $javaEnabled == "true" )
return JavaInfo in target
#else
return False
#end
#if( $javaEnabled == "true" )
return JavaInfo in target

#else
return False
#end

def get_java_info(target):
#if( $javaEnabled == "true" )
if JavaInfo in target:
return target[JavaInfo]
else:
return None
#else
return None
#end
#if( $javaEnabled == "true" )
if JavaInfo in target:
return target[JavaInfo]
else:
return None

#else
return None
#end

def java_info_reference():
#if( $javaEnabled == "true" )
return [JavaInfo]
#else
return []
#end
#if( $javaEnabled == "true" )
return [JavaInfo]

#else
return []
#end

def extract_java_toolchain(target, ctx, dep_targets, **kwargs):
toolchain = None
Expand Down
4 changes: 3 additions & 1 deletion server/aspects/rules/jvm/jvm_info.bzl.template
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ def extract_runtime_jars(target, provider):
if compilation_info:
return compilation_info.runtime_classpath

return getattr(provider, "transitive_runtime_jars", get_java_info(target).transitive_runtime_jars)
java_info = get_java_info(target)

return getattr(provider, "transitive_runtime_jars", java_info.transitive_runtime_jars if java_info else depset())

def extract_compile_jars(provider):
compilation_info = getattr(provider, "compilation_info", None)
Expand Down
9 changes: 5 additions & 4 deletions server/aspects/rules/kt/kt_info.bzl.template
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
load("@${ruleName}//kotlin/internal:defs.bzl", "KtJvmInfo", "KtCompilerPluginInfo")
load("@${ruleName}//kotlin/internal:opts.bzl", "KotlincOptions", "kotlinc_options_to_flags")
load("//aspects:utils/utils.bzl", "convert_struct_to_dict", "create_struct", "map", "file_location", "filter_not_none", "log_warn", "collect_targets_from_attrs", "COMPILE_DEPS")
load("@${rulesetName}//kotlin/internal:defs.bzl", "KtCompilerPluginInfo", "KtJvmInfo")
load("@${rulesetName}//kotlin/internal:opts.bzl", "KotlincOptions", "kotlinc_options_to_flags")
load("//aspects:utils/utils.bzl", "COMPILE_DEPS", "collect_targets_from_attrs", "convert_struct_to_dict", "create_struct", "file_location", "filter_not_none", "log_warn", "map")

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

def get_kt_jvm_provider(target):
if KtJvmInfo in target:
Expand Down Expand Up @@ -50,6 +50,7 @@ def extract_kotlin_info(target, ctx, dep_targets, **kwargs):
# accumulate Kotlin compiler plugin info
direct_plugins = getattr(rule_attrs, "plugins", [])
dep_plugins = []

# exported_compiler_plugins is not transitive, so we only iterate over direct dependencies.
# See https://github.com/bazelbuild/rules_kotlin/blob/f14d01ef5af3ad7ff0660ff671ca9b20c8a020d2/kotlin/internal/jvm/jvm.bzl#L268
for dep in dep_targets:
Expand Down
18 changes: 14 additions & 4 deletions server/e2e/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,20 @@ bazel_integration_test_all_versions(
test_runner = "//server/e2e/src/main/kotlin/org/jetbrains/bsp/bazel:BazelBspPythonProjectTest",
)

bazel_integration_test(
bazel_integration_test_all_versions(
name = "kotlin_project_test",
timeout = "eternal",
bazel_version = "6.4.0",
bzlmod_project_path = "test-projects/bzlmod/kotlin-project",
exclude_bazel_5 = True, # bazel 5 will be deprecated soon, project fails to build with bazel 5 so let's just skip it
project_path = "test-projects/workspace/kotlin-project",
test_runner = "//server/e2e/src/main/kotlin/org/jetbrains/bsp/bazel:BazelBspKotlinProjectTest",
workspace_path = "test-projects/workspace/kotlin-project",
)

bazel_integration_test_all_versions(
name = "kotlin_project_with_enabled_rules_test",
bzlmod_project_path = "test-projects/bzlmod/kotlin-project",
exclude_bazel_5 = True, # bazel 5 will be deprecated soon, project fails to build with bazel 5 so let's just skip it
project_path = "test-projects/workspace/kotlin-project",
test_runner = "//server/e2e/src/main/kotlin/org/jetbrains/bsp/bazel:BazelBspKotlinProjectWithEnabledRulesTest",
)

bazel_integration_test_all_versions(
Expand Down Expand Up @@ -147,6 +155,8 @@ test_suite(
":first_phase_sync_test",
":go_project_test",
":java_diagnostics_test",
":kotlin_project_test",
":kotlin_project_with_enabled_rules_test",
":local_jdk_test",
":nested_modules_test",
":partial_sync_test",
Expand Down
16 changes: 16 additions & 0 deletions server/e2e/src/main/kotlin/org/jetbrains/bsp/bazel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ kt_jvm_binary(
],
)

kt_jvm_binary(
name = "BazelBspKotlinProjectWithEnabledRulesTest",
srcs = ["BazelBspKotlinProjectWithEnabledRulesTest.kt"],
main_class = "org.jetbrains.bsp.bazel.BazelBspKotlinProjectWithEnabledRulesTest",
resources = ["//server/e2e/src/main/resources:bsp-e2e-resources"],
visibility = ["//server/e2e:__subpackages__"],
deps = [
":BazelBspKotlinProjectTest",
"//protocol",
"//server/commons",
"//server/e2e/src/main/kotlin/org/jetbrains/bsp/bazel/base",
"//server/server/src/main/kotlin/org/jetbrains/bsp/bazel/server/model",
"@maven//:ch_epfl_scala_bsp4j",
],
)

kt_jvm_binary(
name = "ServerDownloadsBazeliskTest",
srcs = ["ServerDownloadsBazeliskTest.kt"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ import org.jetbrains.bsp.bazel.install.Install
import org.jetbrains.bsp.protocol.KotlinBuildTarget
import kotlin.time.Duration.Companion.seconds

object BazelBspKotlinProjectTest : BazelBspTestBaseScenario() {
open class BazelBspKotlinProjectTest : BazelBspTestBaseScenario() {
private val testClient = createTestkitClient()

@JvmStatic
fun main(args: Array<String>) = executeScenario()

override fun installServer() {
Install.main(
arrayOf(
Expand All @@ -27,13 +24,10 @@ object BazelBspKotlinProjectTest : BazelBspTestBaseScenario() {
bazelBinary,
"-t",
"//...",
"--enabled-rules",
"rules_kotlin",
"--shard-sync",
"true",
"--target-shard-size",
"1",
"--shard-approach",
"EXPAND_AND_SHARD",
),
)
}
Expand All @@ -44,9 +38,14 @@ object BazelBspKotlinProjectTest : BazelBspTestBaseScenario() {
)

override fun expectedWorkspaceBuildTargetsResult(): WorkspaceBuildTargetsResult {
val workspaceJavaHome = "file://\$BAZEL_OUTPUT_BASE_PATH/external/remotejdk11_$javaHomeArchitecture/"
val bzlmodJavaHome =
"file://\$BAZEL_OUTPUT_BASE_PATH/external/rules_java$bzlmodRepoNameSeparator" +
"${bzlmodRepoNameSeparator}toolchains${bzlmodRepoNameSeparator}remotejdk11_$javaHomeArchitecture/"
val javaHome = if (isBzlmod) bzlmodJavaHome else workspaceJavaHome
val jvmBuildTargetData =
JvmBuildTarget().also {
it.javaHome = "file://\$BAZEL_OUTPUT_BASE_PATH/external/remotejdk11_$javaHomeArchitecture/"
it.javaHome = javaHome
it.javaVersion = "11"
}

Expand All @@ -55,31 +54,31 @@ object BazelBspKotlinProjectTest : BazelBspTestBaseScenario() {
languageVersion = "1.9",
apiVersion = "1.9",
kotlincOptions =
listOf(
listOfNotNull(
"-Xsam-conversions=class",
"-Xlambdas=class",
"-Xno-source-debug-extension",
if (isBzlmod) null else "-Xno-source-debug-extension",
"-jvm-target=1.8",
),
associates = listOf(),
jvmBuildTarget = jvmBuildTargetData,
jvmBuildTarget = if (isBazel8OrHigher) null else jvmBuildTargetData,
)

val kotlincTestBuildTargetData =
KotlinBuildTarget(
languageVersion = "1.9",
apiVersion = "1.9",
kotlincOptions =
listOf(
listOfNotNull(
"-Xno-call-assertions",
"-Xno-param-assertions",
"-Xsam-conversions=class",
"-Xlambdas=class",
"-Xno-source-debug-extension",
if (isBzlmod) null else "-Xno-source-debug-extension",
"-jvm-target=1.8",
),
associates = listOf(),
jvmBuildTarget = jvmBuildTargetData,
jvmBuildTarget = if (isBazel8OrHigher) null else jvmBuildTargetData,
)

val kotlincTestBuildTarget =
Expand Down Expand Up @@ -118,40 +117,46 @@ object BazelBspKotlinProjectTest : BazelBspTestBaseScenario() {
openForTestingBuildTarget.data = kotlinBuildTargetData
openForTestingBuildTarget.dataKind = "kotlin"

val bzlmodPluginRepo =
"rules_kotlin${bzlmodRepoNameSeparator}$bzlmodRepoNameSeparator" +
"rules_kotlin_extensions${bzlmodRepoNameSeparator}com_github_jetbrains_kotlin_git"
val workspacePluginRepo = "com_github_jetbrains_kotlin"
val pluginRepo = if (isBzlmod) bzlmodPluginRepo else workspacePluginRepo

val userBuildTargetData =
KotlinBuildTarget(
languageVersion = "1.9",
apiVersion = "1.9",
kotlincOptions =
listOf(
listOfNotNull(
"-P",
"-Xlambdas=class",
"-Xno-source-debug-extension",
"-Xplugin=\$BAZEL_OUTPUT_BASE_PATH/external/com_github_jetbrains_kotlin/lib/allopen-compiler-plugin.jar",
if (isBzlmod) null else "-Xno-source-debug-extension",
"-Xplugin=\$BAZEL_OUTPUT_BASE_PATH/external/$pluginRepo/lib/allopen-compiler-plugin.jar",
"-Xsam-conversions=class",
"-jvm-target=1.8",
"plugin:org.jetbrains.kotlin.allopen:annotation=plugin.allopen.OpenForTesting",
),
associates = listOf(),
jvmBuildTarget = jvmBuildTargetData,
jvmBuildTarget = if (isBazel8OrHigher) null else jvmBuildTargetData,
)

val userOfExportBuildTargetData =
KotlinBuildTarget(
languageVersion = "1.9",
apiVersion = "1.9",
kotlincOptions =
listOf(
listOfNotNull(
"-P",
"-Xlambdas=class",
"-Xno-source-debug-extension",
"-Xplugin=\$BAZEL_OUTPUT_BASE_PATH/external/com_github_jetbrains_kotlin/lib/allopen-compiler-plugin.jar",
if (isBzlmod) null else "-Xno-source-debug-extension",
"-Xplugin=\$BAZEL_OUTPUT_BASE_PATH/external/$pluginRepo/lib/allopen-compiler-plugin.jar",
"-Xsam-conversions=class",
"-jvm-target=1.8",
"plugin:org.jetbrains.kotlin.allopen:annotation=plugin.allopen.OpenForTesting",
),
associates = listOf(),
jvmBuildTarget = jvmBuildTargetData,
jvmBuildTarget = if (isBazel8OrHigher) null else jvmBuildTargetData,
)

val userBuildTarget =
Expand Down Expand Up @@ -233,5 +238,10 @@ object BazelBspKotlinProjectTest : BazelBspTestBaseScenario() {
private fun compareWorkspaceTargetsResults(): BazelBspTestScenarioStep =
BazelBspTestScenarioStep(
"compare workspace targets results",
) { testClient.testWorkspaceTargets(60.seconds, expectedWorkspaceBuildTargetsResult()) }
) { testClient.testWorkspaceTargets(140.seconds, expectedWorkspaceBuildTargetsResult()) }

companion object {
@JvmStatic
fun main(args: Array<String>) = BazelBspKotlinProjectTest().executeScenario()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.jetbrains.bsp.bazel

import org.jetbrains.bsp.bazel.install.Install

object BazelBspKotlinProjectWithEnabledRulesTest : BazelBspKotlinProjectTest() {
@JvmStatic
fun main(args: Array<String>): Unit = executeScenario()

override fun installServer() {
Install.main(
arrayOf(
"-d",
workspaceDir,
"-b",
bazelBinary,
"-t",
"//...",
"--enabled-rules",
"rules_kotlin",
"--shard-sync",
"true",
"--target-shard-size",
"1",
),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,20 @@ abstract class BazelBspTestBaseScenario {
val targetPrefix = "@"

val isBzlmod = majorBazelVersion >= 7
val isBazel8OrHigher = majorBazelVersion >= 8
val bzlmodRepoNameSeparator = if (majorBazelVersion == 7) "~" else "+"

private val architecturePart
get() = if (System.getProperty("os.arch") == "aarch64") "_aarch64" else ""
val javaHomeArchitecture get() = "\$OS$architecturePart"

private val bazelArch get() =
if (OsFamily.inferFromSystem() == OsFamily.MACOS) {
"darwin_arm64"
} else {
"k8"
}
private val bazelArch
get() =
if (OsFamily.inferFromSystem() == OsFamily.MACOS) {
"darwin_arm64"
} else {
"k8"
}
private val mainBinName = if (majorBazelVersion >= 7) "_main" else "__main__"
val bazelBinDirectory get() = "file://\$BAZEL_OUTPUT_BASE_PATH/execroot/$mainBinName/bazel-out/$bazelArch-fastbuild/bin"

Expand Down
1 change: 1 addition & 0 deletions server/e2e/test-projects/bzlmod/kotlin-project/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build --java_runtime_version=remotejdk_11
5 changes: 5 additions & 0 deletions server/e2e/test-projects/bzlmod/kotlin-project/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.bazelbsp/
.bsp/
.idea/
.ijwb/
bazel-*
Loading

0 comments on commit cd202b8

Please sign in to comment.