From ba1062620c5e41099792996d3b78095884c382f2 Mon Sep 17 00:00:00 2001 From: Dawid Weiss Date: Sun, 10 Apr 2022 20:35:45 +0200 Subject: [PATCH] LUCENE-10510: Check module access prior to running gjf/spotless tasks (#802) --- gradle/generation/local-settings.gradle | 26 ++++++++++++++++++++++++- gradle/validation/error-prone.gradle | 4 +++- gradle/validation/spotless.gradle | 2 ++ help/localSettings.txt | 9 +++++++-- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/gradle/generation/local-settings.gradle b/gradle/generation/local-settings.gradle index fd906b87f28..9d437ff5d14 100644 --- a/gradle/generation/local-settings.gradle +++ b/gradle/generation/local-settings.gradle @@ -22,6 +22,30 @@ def hasDefaults = rootProject.file("gradle.properties").exists() configure(rootProject) { + // Add a task verifying that gradle process has access to JVM internals. + // this is occasionally needed for certain tasks. + task checkJdkInternalsExportedToGradle() { + doFirst { + def jdkCompilerModule = ModuleLayer.boot().findModule("jdk.compiler").orElseThrow() + def gradleModule = getClass().module + def internalsExported = [ + "com.sun.tools.javac.api", + "com.sun.tools.javac.file", + "com.sun.tools.javac.parser", + "com.sun.tools.javac.tree", + "com.sun.tools.javac.util" + ].stream() + .allMatch(pkg -> jdkCompilerModule.isExported(pkg, gradleModule)) + + if (!internalsExported) { + throw new GradleException( + "Certain gradle tasks and plugins require access to jdk.compiler" + + " internals, your gradle.properties might have just been generated or could be" + + " out of sync (see help/localSettings.txt)") + } + } + } + task localSettings() { doFirst { // If we don't have the defaults yet, create them. @@ -43,7 +67,7 @@ systemProp.file.encoding=UTF-8 # Set up gradle JVM defaults. # The heap seems huge but gradle runs out of memory on lower values (don't know why). # -# We also open up internal compiler modules for spotless/ google jaa format. +# We also open up internal compiler modules for spotless/ google java format. org.gradle.jvmargs=-Xmx3g \\ --add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \\ --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \\ diff --git a/gradle/validation/error-prone.gradle b/gradle/validation/error-prone.gradle index 205f608771c..5ca48a7211c 100644 --- a/gradle/validation/error-prone.gradle +++ b/gradle/validation/error-prone.gradle @@ -22,7 +22,7 @@ if (rootProject.usesAltJvm && rootProject.runtimeJavaVersion > JavaVersion.VERSI } if (!Boolean.parseBoolean(propertyOrDefault("tests.nightly", "false"))) { - skipReason = "skipped on non-nightly runs, pass -Dtests.nightly=true to run" + skipReason = "skipped on non-nightly runs, pass -Ptests.nightly=true to run" } if (skipReason) { @@ -58,6 +58,8 @@ allprojects { prj -> } tasks.withType(JavaCompile) { task -> + task.dependsOn ":checkJdkInternalsExportedToGradle" + options.errorprone.disableWarningsInGeneratedCode = true options.errorprone.errorproneArgs = [ '-Xep:InlineMeSuggester:OFF', // We don't use this annotation diff --git a/gradle/validation/spotless.gradle b/gradle/validation/spotless.gradle index 0ee79cc0b07..372bcf2f9e7 100644 --- a/gradle/validation/spotless.gradle +++ b/gradle/validation/spotless.gradle @@ -103,9 +103,11 @@ configure(project(":lucene").subprojects) { prj -> tasks.matching { task -> task.name == "spotlessApply" }.configureEach { v -> tidy.dependsOn v + v.dependsOn ":checkJdkInternalsExportedToGradle" } tasks.matching { task -> task.name == "spotlessCheck" }.configureEach { v -> check.dependsOn v + v.dependsOn ":checkJdkInternalsExportedToGradle" } } diff --git a/help/localSettings.txt b/help/localSettings.txt index 95968152943..6772c31077b 100644 --- a/help/localSettings.txt +++ b/help/localSettings.txt @@ -4,9 +4,14 @@ Local developer settings The first invocation of any task in Lucene's gradle build will generate and save a project-local 'gradle.properties' file. This file contains the defaults you may (but don't have to) tweak for your particular hardware -(or taste). +(or taste). Note there are certain settings in that file that may +be _required_ at runtime for certain plugins (an example is the spotless/ +google java format plugin, which requires adding custom exports to JVM modules). Gradle +build only generates this file if it's not already present (it never overwrites +the defaults) -- occasionally you may have to manually delete (or move) this +file and regenerate from scratch. -This is an overview of some of these settings. +This is an overview of some settings present in gradle.properties. Parallelism -----------