Skip to content

Commit 55f60ec

Browse files
Add support for Kotlin 1.9 (#996)
*NOTE:* JS support is currently inoperable with 1.9. We are debating continued support. * Add support for Kotlin 1.9 * Disable js example for RC. Co-authored-by: Corbin McNeely-Smith <[email protected]> Co-authored-by: Corbin McNeely-Smith <[email protected]>
1 parent eb251f9 commit 55f60ec

File tree

21 files changed

+386
-177
lines changed

21 files changed

+386
-177
lines changed

.bazelci/presubmit.yml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,16 @@ tasks:
156156
- test
157157
build_targets:
158158
- //...
159-
examples-nodejs:
160-
name: Example - Node
161-
platform: ubuntu1804
162-
working_directory: examples/node
163-
include_json_profile:
164-
- build
165-
- test
166-
build_targets:
167-
- //coroutines-helloworld/...
168-
- //express/...
159+
# examples-nodejs:
160+
# name: Example - Node
161+
# platform: ubuntu1804
162+
# working_directory: examples/node
163+
# include_json_profile:
164+
# - build
165+
# - test
166+
# build_targets:
167+
# - //coroutines-helloworld/...
168+
# - //express/...
169169
example-jetpack-compose:
170170
name: "Example - Jetpack Compose"
171171
platform: ubuntu1804

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ load("@rules_kotlin//kotlin:core.bzl", "define_kt_toolchain")
9797

9898
define_kt_toolchain(
9999
name = "kotlin_toolchain",
100-
api_version = KOTLIN_LANGUAGE_LEVEL, # "1.1", "1.2", "1.3", "1.4", "1.5" "1.6", or "1.7"
100+
api_version = KOTLIN_LANGUAGE_LEVEL, # "1.1", "1.2", "1.3", "1.4", "1.5" "1.6", "1.7", "1.8", or "1.9"
101101
jvm_target = JAVA_LANGUAGE_LEVEL, # "1.6", "1.8", "9", "10", "11", "12", "13", "15", "16", or "17"
102-
language_version = KOTLIN_LANGUAGE_LEVEL, # "1.1", "1.2", "1.3", "1.4", "1.5" "1.6", or "1.7"
102+
language_version = KOTLIN_LANGUAGE_LEVEL, # "1.1", "1.2", "1.3", "1.4", "1.5" "1.6", "1.7", "1.8", or "1.9"
103103
)
104104
```
105105

examples/node/express/BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ kt_js_library(
3030
kt_js_library(
3131
name = "app",
3232
srcs = [":App.kt"],
33+
tags = [
34+
"timings",
35+
"trace",
36+
],
3337
deps = [
3438
":acme-routes",
3539
"//:kotlinx-coroutines-core",

examples/node/express/auth/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ load("@rules_kotlin//kotlin:js.bzl", "kt_js_library")
1616
kt_js_library(
1717
name = "acme-auth",
1818
srcs = ["Auth.kt"],
19+
tags = ["trace"],
1920
visibility = ["//visibility:public"],
2021
)

kotlin/internal/js/impl.bzl

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,6 @@ load(
3434
def kt_js_library_impl(ctx):
3535
toolchain = ctx.toolchains[_TOOLCHAIN_TYPE]
3636

37-
# meta.js is merged in with the js in the builder. It is declared as it's created at the package level and not in
38-
# some anonymous directory.
39-
out_meta = ctx.actions.declare_file(ctx.attr.name + ".meta.js")
40-
41-
# The Kotlin compiler and intellij infrastructure uses jars and bytecode. The out dir contains bytecode generated by
42-
# the kotlin compiler. In addition to the js and js.map file a jar is also produced.
43-
out_dir = ctx.actions.declare_directory(ctx.label.name)
44-
4537
libraries = depset([d[_KtJsInfo].jar for d in ctx.attr.deps])
4638

4739
args = _utils.init_args(
@@ -54,8 +46,8 @@ def kt_js_library_impl(ctx):
5446
"--kotlin_js_passthrough_flags",
5547
[
5648
"-source-map",
57-
"-meta-info",
58-
"-no-stdlib", # TODO remove this once the stdlib is not conveyed to node via the deps attribute.
49+
"-Xir-produce-klib-dir",
50+
"-no-stdlib",
5951
"-module-kind",
6052
ctx.attr.module_kind,
6153
"-target",
@@ -64,7 +56,6 @@ def kt_js_library_impl(ctx):
6456
)
6557

6658
args.add("--output", ctx.outputs.js)
67-
args.add("--kotlin_js_dir", out_dir.path)
6859
args.add("--kotlin_output_js_jar", ctx.outputs.jar)
6960
args.add("--kotlin_output_srcjar", ctx.outputs.srcjar)
7061
args.add("--strict_kotlin_deps", "off")
@@ -83,8 +74,6 @@ def kt_js_library_impl(ctx):
8374
ctx.outputs.js_map,
8475
ctx.outputs.jar,
8576
ctx.outputs.srcjar,
86-
out_meta,
87-
out_dir,
8877
],
8978
executable = toolchain.kotlinbuilder.files_to_run.executable,
9079
execution_requirements = {"supports-workers": "1"},

kotlin/internal/toolchains.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ _kt_toolchain = rule(
134134
"1.6",
135135
"1.7",
136136
"1.8",
137+
"1.9",
137138
],
138139
),
139140
"api_version": attr.string(
@@ -148,6 +149,7 @@ _kt_toolchain = rule(
148149
"1.6",
149150
"1.7",
150151
"1.8",
152+
"1.9",
151153
],
152154
),
153155
"debug": attr.string_list(

src/main/kotlin/io/bazel/kotlin/builder/tasks/js/Kotlin2JsTaskExecutor.kt

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@ import io.bazel.kotlin.builder.toolchain.KotlinToolchain
66
import io.bazel.kotlin.builder.utils.addAll
77
import io.bazel.kotlin.builder.utils.jars.JarCreator
88
import io.bazel.kotlin.builder.utils.jars.SourceJarCreator
9-
import io.bazel.kotlin.builder.utils.resolveTwinVerified
109
import io.bazel.kotlin.model.JsCompilationTask
11-
import java.io.FileOutputStream
1210
import java.nio.file.FileSystem
1311
import java.nio.file.FileSystems
1412
import java.nio.file.Files
1513
import java.nio.file.Path
16-
import java.nio.file.Paths
14+
import java.util.stream.Collectors
1715
import javax.inject.Inject
1816
import javax.inject.Singleton
17+
import kotlin.io.path.absolute
18+
import kotlin.io.path.absolutePathString
19+
import kotlin.io.path.nameWithoutExtension
1920

2021
@Singleton
2122
class Kotlin2JsTaskExecutor @Inject constructor(
@@ -28,64 +29,64 @@ class Kotlin2JsTaskExecutor @Inject constructor(
2829
context: CompilationTaskContext,
2930
task: JsCompilationTask,
3031
) {
31-
task.compile(context)
32-
33-
val jsPath = fileSystem.getPath(task.outputs.js)
34-
val jsMetaFile = jsPath.resolveTwinVerified(".meta.js")
35-
val jsDirectory = Files.createDirectories(
36-
fileSystem.getPath(task.directories.temp)
37-
.resolve(jsPath.toFile().nameWithoutExtension),
38-
)
39-
task.createJar(
40-
jsDirectory,
41-
listOf(jsPath, jsPath.resolveTwinVerified(".js.map"), jsMetaFile),
42-
)
43-
// this mutates the jsPath file , so do it after creating the jar.
44-
appendMetaToPrimary(jsPath, jsMetaFile)
32+
val outputDirectory = task.compile(context)
33+
task.createJar(outputDirectory)
4534
task.createSourceJar()
4635
}
4736

48-
private fun JsCompilationTask.compile(context: CompilationTaskContext) {
49-
val args = mutableListOf<String>().also {
50-
it.addAll(passThroughFlagsList)
51-
it.addAll("-libraries", inputs.librariesList.joinToString(":"))
52-
it.addAll("-output", outputs.js)
53-
it.addAll("-Xuse-deprecated-legacy-compiler")
54-
it.addAll(inputs.kotlinSourcesList)
37+
private fun JsCompilationTask.compile(context: CompilationTaskContext): Path {
38+
val jsOut = fileSystem.getPath(outputs.js)
39+
val outputDirectory = jsOut.parent
40+
val baseName = jsOut.fileName.nameWithoutExtension
41+
val mapOut = outputDirectory.resolve("$baseName.js.map")
42+
val workingDirectory = fileSystem.getPath(directories.temp)
43+
44+
val execRoot = fileSystem.getPath(".").absolute()
45+
46+
val args = mutableListOf<String>().apply {
47+
addAll(passThroughFlagsList)
48+
add("-Xdisable-default-scripting-plugin")
49+
add("-Xir-produce-js")
50+
add("-progressive")
51+
add("-Xoptimize-generated-js=false")
52+
addAll(
53+
"-libraries",
54+
inputs.librariesList.map { execRoot.resolve(it).absolutePathString() }.joinToString(":"),
55+
)
56+
addAll("-ir-output-name", baseName)
57+
addAll("-ir-output-dir", workingDirectory.toString())
58+
addAll("-Xir-module-name=${info.moduleName}")
59+
addAll(inputs.kotlinSourcesList.map { execRoot.resolve(it).absolutePathString() })
5560
}
61+
5662
context.whenTracing { printLines("js compile args", args) }
5763
context.executeCompilerTask(args, invoker::compile)
64+
context.whenTracing {
65+
printLines(
66+
"outputs",
67+
Files.walk(outputDirectory).map { p -> p.toString() }.collect(Collectors.toList()),
68+
)
69+
}
70+
Files.copy(workingDirectory.resolve(jsOut.fileName), jsOut)
71+
Files.copy(workingDirectory.resolve(mapOut.fileName), mapOut)
72+
73+
return workingDirectory
5874
}
5975

6076
private fun JsCompilationTask.createSourceJar() {
6177
try {
62-
SourceJarCreator(Paths.get(outputs.srcjar), false).also { creator ->
63-
creator.addSources(inputs.kotlinSourcesList.map { Paths.get(it) }.stream())
78+
SourceJarCreator(fileSystem.getPath(outputs.srcjar), false).also { creator ->
79+
creator.addSources(inputs.kotlinSourcesList.map { fileSystem.getPath(it) }.stream())
6480
}.execute()
6581
} catch (ex: Throwable) {
6682
throw CompilationException("could not create source jar", ex)
6783
}
6884
}
6985

70-
/**
71-
* Append the meta file to the JS file. This is an accepted pattern, and it allows us to not have to export the
72-
* meta.js file with the js.
73-
*/
74-
private fun appendMetaToPrimary(jsPath: Path, jsMetaFile: Path) {
86+
private fun JsCompilationTask.createJar(jsDirectoryPath: Path) {
7587
try {
76-
FileOutputStream(jsPath.toFile(), true).use { Files.copy(jsMetaFile, it) }
77-
} catch (ex: Throwable) {
78-
throw CompilationException("could not normalize js file", ex)
79-
}
80-
}
81-
82-
private fun JsCompilationTask.createJar(jsDirectoryPath: Path, rootEntries: List<Path>) {
83-
try {
84-
val outputJarPath = Paths.get(outputs.jar)
85-
86-
JarCreator(outputJarPath).also { creator ->
88+
JarCreator(fileSystem.getPath(outputs.jar)).use { creator ->
8789
creator.addDirectory(jsDirectoryPath)
88-
creator.addRootEntries(rootEntries.map { it.toString() })
8990
creator.execute()
9091
}
9192
} catch (ex: Throwable) {

src/main/kotlin/io/bazel/kotlin/plugin/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ kt_bootstrap_library(
3030
generate_jvm_service(
3131
name = "skip-code-gen-services",
3232
services = {
33-
"io.bazel.kotlin.plugin.SkipCodeGen": "org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar",
33+
"io.bazel.kotlin.plugin.SkipCodeGen": "org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar",
3434
},
3535
)
3636

src/main/kotlin/io/bazel/kotlin/plugin/SkipCodeGen.kt

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717
package io.bazel.kotlin.plugin
1818

1919
import com.google.common.base.Preconditions
20-
import com.intellij.mock.MockProject
2120
import com.intellij.openapi.project.Project
2221
import org.jetbrains.kotlin.analyzer.AnalysisResult
23-
import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
22+
import org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar
2423
import org.jetbrains.kotlin.config.CompilerConfiguration
2524
import org.jetbrains.kotlin.container.ComponentProvider
2625
import org.jetbrains.kotlin.context.ProjectContext
@@ -33,20 +32,17 @@ import org.jetbrains.kotlin.resolve.jvm.extensions.AnalysisHandlerExtension
3332
* SkipCodeGen registers an extension to skip code generation. Must be the last compiler plugin.
3433
*/
3534
@OptIn(org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi::class)
36-
class SkipCodeGen : ComponentRegistrar {
35+
class SkipCodeGen : CompilerPluginRegistrar() {
3736

38-
companion object {
39-
val COMPILER_PLUGIN_ID = "io.bazel.kotlin.plugin.SkipCodeGen"
37+
override val supportsK2: Boolean
38+
get() = false
39+
40+
override fun ExtensionStorage.registerExtensions(configuration: CompilerConfiguration) {
41+
AnalysisHandlerExtension.registerExtension(SkipCodeGen)
4042
}
4143

42-
override fun registerProjectComponents(
43-
project: MockProject,
44-
configuration: CompilerConfiguration,
45-
) {
46-
AnalysisHandlerExtension.registerExtension(
47-
project,
48-
SkipCodeGen,
49-
)
44+
companion object {
45+
val COMPILER_PLUGIN_ID = "io.bazel.kotlin.plugin.SkipCodeGen"
5046
}
5147

5248
/**
@@ -71,7 +67,7 @@ class SkipCodeGen : ComponentRegistrar {
7167
module: ModuleDescriptor,
7268
bindingTrace: BindingTrace,
7369
files: Collection<KtFile>,
74-
): AnalysisResult? {
70+
): AnalysisResult {
7571
// Ensure this is the last plugin, as it will short circuit any other plugin analysisCompleted
7672
// calls.
7773
Preconditions.checkState(

src/main/kotlin/io/bazel/kotlin/plugin/jdeps/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ kt_bootstrap_library(
3333
generate_jvm_service(
3434
name = "jdeps-gen-services",
3535
services = {
36-
"io.bazel.kotlin.plugin.jdeps.JdepsGenComponentRegistrar": "org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar",
36+
"io.bazel.kotlin.plugin.jdeps.JdepsGenComponentRegistrar": "org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar",
3737
"io.bazel.kotlin.plugin.jdeps.JdepsGenCommandLineProcessor": "org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor",
3838
},
3939
)

0 commit comments

Comments
 (0)