is there a way to pass an option to ksp extension ...
# ksp
v
is there a way to pass an option to ksp extension based on the results of a gradle task? 🤔
e
if your other task exposes a property it should be easy enough to wire up
Copy code
val otherTask = ...

tasks.withType<com.google.devtools.ksp.gradle.KspTask>().all {
    apOptions.put("myKey", otherTask.output)
}
v
By doing so,
environment.options
returns an empty map, so
SymbolProcessor
does not get this option :S
e
is your output of type
Property
? If not you'll need to explicitly add the task dependency too
v
I added the following snippet and options are still empty
Copy code
tasks.withType<com.google.devtools.ksp.gradle.KspTask>().all {
    apOptions.put("myKey", "someDummyValue")
}
e
Oh interesting, looks like the task configuration overrides it with the extension value and prevents further changes
Copy code
fun configureAsKspTask(kspTask: KspTask, isIncremental: Boolean) {
            // depends on the processor; if the processor changes, it needs to be reprocessed.
            val processorClasspath = project.configurations.maybeCreate("${kspTaskName}ProcessorClasspath")
                .extendsFrom(*nonEmptyKspConfigurations.toTypedArray())
            kspTask.processorClasspath.from(processorClasspath)
            kspTask.dependsOn(processorClasspath.buildDependencies)

            kspTask.options.addAll(
                kspTask.project.provider {
                    getSubpluginOptions(
                        project,
                        kspExtension,
                        processorClasspath,
                        sourceSetName,
                        isIncremental,
                        kspExtension.allWarningsAsErrors
                    )
                }
            )
            kspTask.destination = kspOutputDir
            kspTask.blockOtherCompilerPlugins = kspExtension.blockOtherCompilerPlugins
            kspTask.apOptions.value(kspExtension.arguments).disallowChanges()
            kspTask.kspCacheDir.fileValue(getKspCachesDir(project, sourceSetName)).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.isKspIncremental = isIncremental
        }
may be worth opening an issue about this
In theory KspExtension could be updated to use a
MapProperty
and then you could configure it like
Copy code
ksp {
  arg("myKey", myTask.output)
}
v
I found a way to pass value from a task with the help of
project.extra
I do not know if it is the best/recommended way though...