Skip to content

Commit

Permalink
Fix #156 by using a Worker API WorkQueue per task (#157)
Browse files Browse the repository at this point in the history
It looks like work queues aren't safe to share when run in parallel tasks
  • Loading branch information
jeremymailen authored Aug 27, 2020
1 parent c1789b1 commit fd9fc86
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 36 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Available on the Gradle Plugins Portal: https://plugins.gradle.org/plugin/org.jm

```kotlin
plugins {
id("org.jmailen.kotlinter") version "3.0.0"
id("org.jmailen.kotlinter") version "3.0.1"
}
```

Expand All @@ -30,7 +30,7 @@ plugins {

```groovy
plugins {
id "org.jmailen.kotlinter" version "3.0.0"
id "org.jmailen.kotlinter" version "3.0.1"
}
```

Expand All @@ -50,7 +50,7 @@ buildscript {
}
}
dependencies {
classpath("org.jmailen.gradle:kotlinter-gradle:3.0.0")
classpath("org.jmailen.gradle:kotlinter-gradle:3.0.1")
}
}
```
Expand All @@ -75,7 +75,7 @@ buildscript {
}
}
dependencies {
classpath "org.jmailen.gradle:kotlinter-gradle:3.0.0"
classpath "org.jmailen.gradle:kotlinter-gradle:3.0.1"
}
}
```
Expand Down
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
id("com.gradle.plugin-publish") version "0.12.0"
`java-gradle-plugin`
`maven-publish`
// id("org.jmailen.kotlinter") version "2.4.1"
id("org.jmailen.kotlinter") version "3.0.0"
idea
}

Expand All @@ -19,7 +19,7 @@ val githubUrl ="https://github.com/jeremymailen/kotlinter-gradle"
val webUrl = "https://github.com/jeremymailen/kotlinter-gradle"
val projectDescription = "Lint and formatting for Kotlin using ktlint with configuration-free setup on JVM and Android projects"

version = "3.0.0"
version = "3.0.1"
group = "org.jmailen.gradle"
description = projectDescription

Expand Down
12 changes: 1 addition & 11 deletions src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.android.build.gradle.BaseExtension
import com.android.build.gradle.api.AndroidSourceSet
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.file.ConfigurableFileTree
import org.gradle.api.file.FileTree
import org.gradle.api.file.SourceDirectorySet
Expand All @@ -13,22 +12,14 @@ import org.gradle.api.plugins.JavaPluginConvention
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.workers.WorkQueue
import org.gradle.workers.WorkerExecutor
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jmailen.gradle.kotlinter.support.reporterFileExtension
import org.jmailen.gradle.kotlinter.tasks.FormatTask
import org.jmailen.gradle.kotlinter.tasks.InstallPreCommitHookTask
import org.jmailen.gradle.kotlinter.tasks.InstallPrePushHookTask
import org.jmailen.gradle.kotlinter.tasks.LintTask
import javax.inject.Inject

class KotlinterPlugin @Inject constructor(
private val workerExecutor: WorkerExecutor
) : Plugin<Project> {
companion object {
lateinit var workQueue: WorkQueue
}
class KotlinterPlugin : Plugin<Project> {

val extendablePlugins = mapOf(
"org.jetbrains.kotlin.jvm" to KotlinJvmSourceSetResolver,
Expand All @@ -37,7 +28,6 @@ class KotlinterPlugin @Inject constructor(

override fun apply(project: Project) = with(project) {
val kotlinterExtension = extensions.create("kotlinter", KotlinterExtension::class.java)
workQueue = workerExecutor.noIsolation()

if (project.rootProject == project) {
registerPrePushHookTask()
Expand Down
23 changes: 14 additions & 9 deletions src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import org.gradle.api.GradleException
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import org.gradle.workers.WorkerExecutor
import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty
import org.jmailen.gradle.kotlinter.KotlinterPlugin
import org.jmailen.gradle.kotlinter.support.KotlinterError
import org.jmailen.gradle.kotlinter.tasks.format.FormatWorkerAction
import javax.inject.Inject

open class FormatTask : ConfigurableKtLintTask() {
open class FormatTask @Inject constructor(
private val workerExecutor: WorkerExecutor
) : ConfigurableKtLintTask() {

@OutputFile
@Optional
Expand All @@ -21,14 +24,16 @@ open class FormatTask : ConfigurableKtLintTask() {

@TaskAction
fun run() {
KotlinterPlugin.workQueue.submit(FormatWorkerAction::class.java) { p ->
p.name.set(name)
p.files.from(source)
p.projectDirectory.set(project.projectDir)
p.ktLintParams.set(getKtLintParams())
p.output.set(report)
val result = with(workerExecutor.noIsolation()) {
submit(FormatWorkerAction::class.java) { p ->
p.name.set(name)
p.files.from(source)
p.projectDirectory.set(project.projectDir)
p.ktLintParams.set(getKtLintParams())
p.output.set(report)
}
runCatching { await() }
}
val result = KotlinterPlugin.workQueue.runCatching { await() }

result.exceptionOrNull()?.workErrorCauses<KotlinterError>()?.ifNotEmpty {
forEach { logger.error(it.message, it.cause) }
Expand Down
23 changes: 14 additions & 9 deletions src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/LintTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ import org.gradle.api.tasks.OutputFiles
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.TaskAction
import org.gradle.workers.WorkerExecutor
import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty
import org.jmailen.gradle.kotlinter.KotlinterExtension.Companion.DEFAULT_IGNORE_FAILURES
import org.jmailen.gradle.kotlinter.KotlinterPlugin
import org.jmailen.gradle.kotlinter.support.KotlinterError
import org.jmailen.gradle.kotlinter.support.LintFailure
import org.jmailen.gradle.kotlinter.tasks.lint.LintWorkerAction
import java.io.File
import javax.inject.Inject

@CacheableTask
open class LintTask : ConfigurableKtLintTask() {
open class LintTask @Inject constructor(
private val workerExecutor: WorkerExecutor
) : ConfigurableKtLintTask() {

@OutputFiles
val reports = mapProperty(default = emptyMap<String, File>())
Expand All @@ -31,14 +34,16 @@ open class LintTask : ConfigurableKtLintTask() {

@TaskAction
fun run() {
KotlinterPlugin.workQueue.submit(LintWorkerAction::class.java) { p ->
p.name.set(name)
p.files.from(source)
p.projectDirectory.set(project.projectDir)
p.reporters.putAll(reports)
p.ktLintParams.set(getKtLintParams())
val result = with(workerExecutor.noIsolation()) {
submit(LintWorkerAction::class.java) { p ->
p.name.set(name)
p.files.from(source)
p.projectDirectory.set(project.projectDir)
p.reporters.putAll(reports)
p.ktLintParams.set(getKtLintParams())
}
runCatching { await() }
}
val result = KotlinterPlugin.workQueue.runCatching { await() }

result.exceptionOrNull()?.workErrorCauses<KotlinterError>()?.ifNotEmpty {
forEach { logger.error(it.message, it.cause) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class RuleSetsTest {
}
""".trimIndent(),
resolveRuleSets(defaultRuleSetProviders), cb = { _, _ -> }
resolveRuleSets(defaultRuleSetProviders),
cb = { _, _ -> }
)
)
}
Expand Down

0 comments on commit fd9fc86

Please sign in to comment.