Skip to content

Commit 4dd9296

Browse files
authored
Rewrite how compiler args are passed (#3115)
The cause is that tasks.withType(...) affects primarily platform compilations and may not affect model imported in the IDE, leading to inconsistencies in the IDE analysis of non-platform source sets.
1 parent 8926b06 commit 4dd9296

File tree

8 files changed

+80
-65
lines changed

8 files changed

+80
-65
lines changed

benchmark/build.gradle.kts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import org.gradle.kotlin.dsl.support.*
2-
import org.jetbrains.kotlin.gradle.dsl.*
3-
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
4-
51
/*
62
* Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
73
*/
@@ -47,11 +43,9 @@ tasks.assemble {
4743

4844
kotlin {
4945
compilerOptions {
50-
jvmTarget = JvmTarget.JVM_1_8
51-
if (overriddenLanguageVersion != null) {
52-
languageVersion = KotlinVersion.fromVersion(overriddenLanguageVersion!!)
53-
freeCompilerArgs.add("-Xsuppress-version-warnings")
54-
}
46+
defaultOptions()
47+
setJava8Compatible()
48+
languageVersion(overriddenLanguageVersion)
5549
}
5650
}
5751

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ tasks.named("knitPrepare") {
8383
// == compiler flags setup ==
8484

8585
subprojects {
86-
apply(plugin = "global-compiler-options")
86+
apply(plugin = "cli-compiler-options")
8787
}
8888

8989
// == TeamCity setup ==
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import org.gradle.kotlin.dsl.assign
2+
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
3+
import org.jetbrains.kotlin.gradle.dsl.KotlinCommonCompilerOptions
4+
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompilerOptions
5+
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
6+
7+
/*
8+
* Copyright 2017-2025 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
9+
*/
10+
11+
val defaultCompilerArgs
12+
get() = listOf(
13+
"-P", "plugin:org.jetbrains.kotlinx.serialization:disableIntrinsic=false",
14+
"-Xreport-all-warnings",
15+
"-Xrender-internal-diagnostic-names",
16+
"-Xreturn-value-checker=full",
17+
)
18+
19+
fun KotlinCommonCompilerOptions.defaultOptions() {
20+
freeCompilerArgs.addAll(defaultCompilerArgs)
21+
}
22+
23+
fun KotlinJvmCompilerOptions.setJava8Compatible() {
24+
jvmTarget = JvmTarget.JVM_1_8
25+
freeCompilerArgs.addAll("-Xjdk-release=1.8")
26+
}
27+
28+
fun KotlinCommonCompilerOptions.languageVersion(overriddenLanguageVersion: String?) {
29+
if (overriddenLanguageVersion != null) {
30+
languageVersion = KotlinVersion.fromVersion(overriddenLanguageVersion)
31+
freeCompilerArgs.add("-Xsuppress-version-warnings")
32+
}
33+
}

buildSrc/src/main/kotlin/global-compiler-options.gradle.kts renamed to buildSrc/src/main/kotlin/cli-compiler-options.gradle.kts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,28 @@
55
import org.jetbrains.kotlin.gradle.tasks.*
66
import kotlin.collections.joinToString
77

8+
/**
9+
* This file is intended for compiler options that affect CLI compilation only.
10+
* The reason is that tasks.withType(...) affects only platform compilations,
11+
* so you may get incorrect analysis results in IDE on 'intermediate' source sets.
12+
*
13+
* If compiler option is likely to affect IDE (e.g., new diagnostic or language feature),
14+
* add it to CompilerOptions.kt instead.
15+
*/
16+
17+
// Used only for User Projects TeamCity configurations, no IDE there
818
val kotlinAdditionalCliOptions = providers.gradleProperty("kotlin_additional_cli_options")
919
.orNull?.let { options ->
1020
options.removeSurrounding("\"").split(" ").filter { it.isNotBlank() }
1121
}
1222

13-
val globalCompilerArgs
14-
get() = listOf(
15-
"-P", "plugin:org.jetbrains.kotlinx.serialization:disableIntrinsic=false",
16-
"-Xreport-all-warnings",
17-
"-Xrender-internal-diagnostic-names",
18-
"-Xreturn-value-checker=full",
19-
)
20-
2123
val kotlin_Werror_override: String? by project
2224

25+
// -Werror option only for test source sets
26+
// Cannot migrate to general compilerOptions {} because we need compilation task name
27+
// to know whether it is main or test SS.
2328
tasks.withType(KotlinCompilationTask::class).configureEach {
2429
compilerOptions {
25-
// Unconditional compiler options
26-
freeCompilerArgs.addAll(globalCompilerArgs)
2730
kotlinAdditionalCliOptions?.forEach { option -> freeCompilerArgs.add(option) }
2831

2932
val isMainTaskName = name.startsWith("compileKotlin")

buildSrc/src/main/kotlin/source-sets-conventions.gradle.kts

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import org.gradle.kotlin.dsl.*
66
import org.jetbrains.kotlin.gradle.*
77
import org.jetbrains.kotlin.gradle.dsl.*
8-
import org.jetbrains.kotlin.gradle.tasks.*
98

109
plugins {
1110
kotlin("multiplatform")
@@ -20,11 +19,24 @@ internal fun Project.versionCatalog(): VersionCatalog = versionCatalogs.named("l
2019
kotlin {
2120
explicitApi()
2221

22+
compilerOptions {
23+
progressiveMode = true
24+
optIn.addAll(
25+
listOf(
26+
"kotlin.ExperimentalMultiplatform",
27+
"kotlin.ExperimentalSubclassOptIn",
28+
"kotlinx.serialization.InternalSerializationApi",
29+
"kotlinx.serialization.SealedSerializationApi",
30+
)
31+
)
32+
defaultOptions()
33+
languageVersion(overriddenLanguageVersion)
34+
freeCompilerArgs.add("-Xexpect-actual-classes")
35+
}
36+
2337
jvm {
24-
@OptIn(ExperimentalKotlinGradlePluginApi::class)
2538
compilerOptions {
26-
jvmTarget = JvmTarget.JVM_1_8
27-
freeCompilerArgs.addAll("-Xjdk-release=1.8")
39+
setJava8Compatible()
2840
}
2941
}
3042
jvmToolchain(jdkToolchainVersion)
@@ -38,7 +50,6 @@ kotlin {
3850
}
3951
}
4052

41-
@OptIn(ExperimentalKotlinGradlePluginApi::class)
4253
compilerOptions {
4354
sourceMap = true
4455
moduleKind = JsModuleKind.MODULE_UMD
@@ -60,26 +71,6 @@ kotlin {
6071
resources.srcDirs("$name/resources")
6172
}
6273

63-
compilerOptions {
64-
// These configuration replaces 'languageSettings' config on line 67
65-
progressiveMode.set(true)
66-
optIn.addAll(
67-
listOf(
68-
"kotlin.ExperimentalMultiplatform",
69-
"kotlin.ExperimentalSubclassOptIn",
70-
"kotlinx.serialization.InternalSerializationApi",
71-
"kotlinx.serialization.SealedSerializationApi",
72-
)
73-
)
74-
if (overriddenLanguageVersion != null) {
75-
languageVersion = KotlinVersion.fromVersion(overriddenLanguageVersion!!)
76-
freeCompilerArgs.add("-Xsuppress-version-warnings")
77-
}
78-
freeCompilerArgs.add("-Xexpect-actual-classes")
79-
// for some reason, IDE does not enable feature in test source sets without this line:
80-
freeCompilerArgs.add("-Xreturn-value-checker=full")
81-
}
82-
8374
sourceSets {
8475
commonMain {
8576
dependencies {

formats/hocon/build.gradle.kts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
21
import Java9Modularity.configureJava9ModuleInfo
3-
import org.jetbrains.kotlin.gradle.dsl.*
42

53

64
/*
@@ -17,20 +15,12 @@ kotlin {
1715
jvmToolchain(jdkToolchainVersion)
1816

1917
compilerOptions {
20-
jvmTarget = JvmTarget.JVM_1_8
21-
if (overriddenLanguageVersion != null) {
22-
languageVersion = KotlinVersion.fromVersion(overriddenLanguageVersion!!)
23-
freeCompilerArgs.add("-Xsuppress-version-warnings")
24-
}
25-
freeCompilerArgs.addAll("-Xjdk-release=1.8")
26-
}
27-
28-
sourceSets.all {
29-
languageSettings {
30-
progressiveMode = true
18+
defaultOptions()
19+
setJava8Compatible()
20+
languageVersion(overriddenLanguageVersion)
3121

32-
optIn("kotlinx.serialization.InternalSerializationApi")
33-
}
22+
progressiveMode = true
23+
optIn.add("kotlinx.serialization.InternalSerializationApi")
3424
}
3525
}
3626

guide/build.gradle.kts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ plugins {
1111
}
1212

1313
kotlin {
14-
jvmToolchain(8)
14+
jvmToolchain(jdkToolchainVersion)
1515

1616
compilerOptions {
17-
if (overriddenLanguageVersion != null) {
18-
languageVersion = KotlinVersion.fromVersion(overriddenLanguageVersion!!)
19-
freeCompilerArgs.add("-Xsuppress-version-warnings")
20-
}
17+
defaultOptions()
18+
// Do not set jvmTarget=1.8 here, as it conflicts with jvmToolchain
19+
languageVersion(overriddenLanguageVersion)
2120
}
2221
}
2322

rules/rules-integration-tests/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ plugins {
2626
kotlin {
2727
// use toolchain from settings
2828
jvmToolchain(jdkToolchainVersion)
29+
compilerOptions {
30+
defaultOptions()
31+
// Do not set jvmTarget=1.8 here, as it conflicts with jvmToolchain
32+
languageVersion(overriddenLanguageVersion)
33+
}
2934
}
3035

3136
val sharedSourceSet = sourceSets.create("shared") {

0 commit comments

Comments
 (0)