Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block other compiler plugins by default #1126

Merged
merged 3 commits into from Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -37,7 +37,8 @@ open class KspExtension {
commandLineArgumentProviders.add(arg)
}

open var blockOtherCompilerPlugins: Boolean = false
@Deprecated("KSP will stop supporting other compiler plugins in KSP's Gradle tasks after 1.0.8.")
open var blockOtherCompilerPlugins: Boolean = true

// Instruct KSP to pickup sources from compile tasks, instead of source sets.
// Note that it depends on behaviors of other Gradle plugins, that may bring surprises and can be hard to debug.
Expand Down
Expand Up @@ -105,6 +105,10 @@ class KspGradleSubplugin @Inject internal constructor(private val registry: Tool
const val KSP_ARTIFACT_NAME = "symbol-processing"
const val KSP_ARTIFACT_NAME_NATIVE = "symbol-processing-cmdline"
const val KSP_PLUGIN_ID = "com.google.devtools.ksp.symbol-processing"
const val KSP_API_ID = "symbol-processing-api"
const val KSP_COMPILER_PLUGIN_ID = "symbol-processing"
const val KSP_GROUP_ID = "com.google.devtools.ksp"
const val KSP_PLUGIN_CLASSPATH_CONFIGURATION_NAME = "kspPluginClasspath"

@JvmStatic
fun getKspOutputDir(project: Project, sourceSetName: String, target: String) =
Expand Down Expand Up @@ -234,6 +238,17 @@ class KspGradleSubplugin @Inject internal constructor(private val registry: Tool
val resourceOutputDir = getKspResourceOutputDir(project, sourceSetName, target)
val kspOutputDir = getKspOutputDir(project, sourceSetName, target)

val kspClasspathCfg = project.configurations.maybeCreate(KSP_PLUGIN_CLASSPATH_CONFIGURATION_NAME)
project.dependencies.add(
KSP_PLUGIN_CLASSPATH_CONFIGURATION_NAME,
"$KSP_GROUP_ID:$KSP_API_ID:$KSP_VERSION"
)
project.dependencies.add(
KSP_PLUGIN_CLASSPATH_CONFIGURATION_NAME,

"$KSP_GROUP_ID:$KSP_COMPILER_PLUGIN_ID:$KSP_VERSION"
)

if (javaCompile != null) {
val generatedJavaSources = javaCompile.project.fileTree(javaOutputDir)
generatedJavaSources.include("**/*.java")
Expand Down Expand Up @@ -273,14 +288,7 @@ class KspGradleSubplugin @Inject internal constructor(private val registry: Tool
kspTask.kspCacheDir.fileValue(getKspCachesDir(project, sourceSetName, target)).disallowChanges()

if (kspExtension.blockOtherCompilerPlugins) {
// FIXME: ask upstream to provide an API to make this not implementation-dependent.
val cfg = project.configurations.getByName(kotlinCompilation.pluginConfigurationName)
kspTask.overridePluginClasspath.value(
kspTask.project.provider {
val dep = cfg.dependencies.single { it.name == KSP_ARTIFACT_NAME }
cfg.fileCollection(dep)
}
)
kspTask.overridePluginClasspath.from(kspClasspathCfg)
}
kspTask.isKspIncremental = isIncremental
}
Expand Down Expand Up @@ -358,6 +366,7 @@ class KspGradleSubplugin @Inject internal constructor(private val registry: Tool
// KotlinNativeCompile computes -Xplugin=... from compilerPluginClasspath.
kspTask.compilerPluginClasspath = project.configurations.getByName(pluginConfigurationName)
kspTask.commonSources.from(kotlinCompileTask.commonSources)
kspTask.compilerPluginOptions.addPluginArgument(kotlinCompileTask.compilerPluginOptions)
}
}
}
Expand Down Expand Up @@ -449,7 +458,7 @@ interface KspTask : Task {

@get:Optional
@get:Classpath
val overridePluginClasspath: Property<FileCollection>
val overridePluginClasspath: ConfigurableFileCollection

@get:Input
var blockOtherCompilerPlugins: Boolean
Expand Down Expand Up @@ -623,7 +632,7 @@ abstract class KspTaskJvm @Inject constructor(
)
)
if (blockOtherCompilerPlugins) {
args.blockOtherPlugins(overridePluginClasspath.get())
args.blockOtherPlugins(overridePluginClasspath)
}
args.addPluginOptions(options.get())
args.destinationAsFile = destination
Expand Down Expand Up @@ -742,7 +751,7 @@ abstract class KspTaskJS @Inject constructor(
)
)
if (blockOtherCompilerPlugins) {
args.blockOtherPlugins(overridePluginClasspath.get())
args.blockOtherPlugins(overridePluginClasspath)
}
args.addPluginOptions(options.get())
args.outputFile = File(destination, "dummyOutput.js").canonicalPath
Expand Down Expand Up @@ -826,7 +835,7 @@ abstract class KspTaskMetadata @Inject constructor(
)
)
if (blockOtherCompilerPlugins) {
args.blockOtherPlugins(overridePluginClasspath.get())
args.blockOtherPlugins(overridePluginClasspath)
}
args.addPluginOptions(options.get())
args.destination = destination.canonicalPath
Expand Down Expand Up @@ -883,7 +892,7 @@ abstract class KspTaskNative @Inject constructor(
override var compilerPluginClasspath: FileCollection? = null
get() {
if (blockOtherCompilerPlugins) {
field = overridePluginClasspath.get()
field = overridePluginClasspath
}
return field
}
Expand Down
Expand Up @@ -59,7 +59,7 @@ class PlaygroundIT {
val gradleRunner = GradleRunner.create().withProjectDir(project.root)

File(project.root, "workload/build.gradle.kts")
.appendText("\nksp {\n blockOtherCompilerPlugins = true\n}\n")
.appendText("\nksp {\n blockOtherCompilerPlugins = false\n}\n")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this for?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to deprecate the flag gradually. The default becomes true effectively. Let's not break blockOtherCompilerPlugins = false too soon.

gradleRunner.buildAndCheck("clean", "build")
gradleRunner.buildAndCheck("clean", "build")
project.restore("workload/build.gradle.kts")
Expand Down