From e74d15a6058f9fde8258f60a521c4789a105c25e Mon Sep 17 00:00:00 2001 From: Robert Stupp Date: Tue, 11 Apr 2023 10:56:48 +0200 Subject: [PATCH] Gradle: ability to disable worker process isolation, pass all envs+sys-props * Gradle workers for Quarkus use _process_ isaolation by default. This can make debugging "tricky". This change changes this to _class loader_ isolation, if the `org.gradle.debug` or the `quarkus.gradle-worker.no-process` property is set to `true`. * Also ensure that all environment variables and system properties are passed to the worker processes. See also #32465 --- .../gradle/tasks/QuarkusBuildTask.java | 4 +-- .../gradle/tasks/QuarkusGenerateCode.java | 4 +-- .../io/quarkus/gradle/tasks/QuarkusTask.java | 26 ++++++++++++++++++- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusBuildTask.java b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusBuildTask.java index 44751d285074d..137fe96d0e738 100644 --- a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusBuildTask.java +++ b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusBuildTask.java @@ -163,9 +163,7 @@ void generateBuild() { .collect(Collectors.joining("\n ", "\n ", ""))); } - WorkQueue workQueue = getWorkerExecutor() - .processIsolation(processWorkerSpec -> configureProcessWorkerSpec(processWorkerSpec, effectiveConfig, - extension().buildForkOptions)); + WorkQueue workQueue = workQueue(effectiveConfig, () -> extension().buildForkOptions); workQueue.submit(BuildWorker.class, params -> { params.getBuildSystemProperties().putAll(effectiveConfig.configMap()); diff --git a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusGenerateCode.java b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusGenerateCode.java index 0c8dd49a28754..5770278d3068f 100644 --- a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusGenerateCode.java +++ b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusGenerateCode.java @@ -104,9 +104,7 @@ public void generateCode() { getLogger().debug("Will trigger preparing sources for source directory: {} buildDir: {}", sourcesDirectories, getProject().getBuildDir().getAbsolutePath()); - WorkQueue workQueue = getWorkerExecutor() - .processIsolation(processWorkerSpec -> configureProcessWorkerSpec(processWorkerSpec, effectiveConfig, - extension().codeGenForkOptions)); + WorkQueue workQueue = workQueue(effectiveConfig, () -> extension().codeGenForkOptions); workQueue.submit(CodeGenWorker.class, params -> { params.getBuildSystemProperties().putAll(effectiveConfig.configMap()); diff --git a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusTask.java b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusTask.java index c255133f03c4b..7296c33730523 100644 --- a/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusTask.java +++ b/devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/QuarkusTask.java @@ -4,6 +4,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; +import java.util.function.Supplier; import javax.inject.Inject; @@ -11,6 +12,7 @@ import org.gradle.api.DefaultTask; import org.gradle.process.JavaForkOptions; import org.gradle.workers.ProcessWorkerSpec; +import org.gradle.workers.WorkQueue; import org.gradle.workers.WorkerExecutor; import io.quarkus.gradle.extension.QuarkusPluginExtension; @@ -35,12 +37,34 @@ QuarkusPluginExtension extension() { return extension; } - void configureProcessWorkerSpec(ProcessWorkerSpec processWorkerSpec, EffectiveConfig effectiveConfig, + WorkQueue workQueue(EffectiveConfig effectiveConfig, Supplier>> forkOptionsActions) { + WorkerExecutor workerExecutor = getWorkerExecutor(); + + // Use process isolation by default, unless Gradle's started with its debugging system property or the + // system property `quarkus.gradle-worker.no-process is set to `true`. + if (Boolean.getBoolean("org.gradle.debug") || Boolean.getBoolean("quarkus.gradle-worker.no-process")) { + return workerExecutor.classLoaderIsolation(); + } + + return workerExecutor.processIsolation(processWorkerSpec -> configureProcessWorkerSpec(processWorkerSpec, + effectiveConfig, forkOptionsActions.get())); + } + + private void configureProcessWorkerSpec(ProcessWorkerSpec processWorkerSpec, EffectiveConfig effectiveConfig, List> customizations) { JavaForkOptions forkOptions = processWorkerSpec.getForkOptions(); customizations.forEach(a -> a.execute(forkOptions)); + // Pass all system properties + System.getProperties().forEach((k, v) -> { + String key = k.toString(); + forkOptions.systemProperty(key, v); + }); + + // Pass all environment variables + forkOptions.environment(System.getenv()); + if (OS.determineOS() == OS.WINDOWS) { // On Windows, gRPC code generation is sometimes(?) unable to find "java.exe". Feels (not proven) that // the grpc code generation tool looks up "java.exe" instead of consulting the 'JAVA_HOME' environment.