From 9a5d04aa897ccbf029c72d4f173bc9a9a0854ead Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Fri, 15 Mar 2024 10:09:02 +0100 Subject: [PATCH 1/3] fix(gradle): Only register a `ProgressListener` in debug log mode Not always registering a `ProgressListener` that, depening on the log level, eventually does not nothing is good practice anyway, and it helps to circumvent a bug with Gradle 8.5 [1] when analyzing such projects. [1]: https://github.com/gradle/gradle/issues/28464 Signed-off-by: Sebastian Schuberth --- .../gradle-inspector/src/main/kotlin/GradleInspector.kt | 6 +++++- .../package-managers/gradle/src/main/kotlin/Gradle.kt | 9 ++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/plugins/package-managers/gradle-inspector/src/main/kotlin/GradleInspector.kt b/plugins/package-managers/gradle-inspector/src/main/kotlin/GradleInspector.kt index 8a1b9a8e37b34..d796df482b1ad 100644 --- a/plugins/package-managers/gradle-inspector/src/main/kotlin/GradleInspector.kt +++ b/plugins/package-managers/gradle-inspector/src/main/kotlin/GradleInspector.kt @@ -160,7 +160,11 @@ class GradleInspector( // then block execution of the plugin until a remote debug session is attached to port 5005 (by default), // also see https://docs.gradle.org/current/userguide/troubleshooting.html#sec:troubleshooting_build_logic. val model = connection.model(OrtDependencyTreeModel::class.java) - .addProgressListener(ProgressListener { logger.debug { it.displayName } }) + .apply { + if (logger.delegate.isDebugEnabled) { + addProgressListener(ProgressListener { logger.debug(it.displayName) }) + } + } .setJvmArguments(jvmArgs) .setStandardOutput(stdout) .setStandardError(stderr) diff --git a/plugins/package-managers/gradle/src/main/kotlin/Gradle.kt b/plugins/package-managers/gradle/src/main/kotlin/Gradle.kt index 12db8fefe9dea..f57e3acb4234f 100644 --- a/plugins/package-managers/gradle/src/main/kotlin/Gradle.kt +++ b/plugins/package-managers/gradle/src/main/kotlin/Gradle.kt @@ -212,10 +212,13 @@ class Gradle( val stdout = ByteArrayOutputStream() val stderr = ByteArrayOutputStream() - val dependencyTreeModel = connection - .model(OrtDependencyTreeModel::class.java) + val dependencyTreeModel = connection.model(OrtDependencyTreeModel::class.java) + .apply { + if (logger.delegate.isDebugEnabled) { + addProgressListener(ProgressListener { logger.debug(it.displayName) }) + } + } .addJvmArguments(jvmArgs) - .addProgressListener(ProgressListener { logger.debug { it.displayName } }) .setStandardOutput(stdout) .setStandardError(stderr) .withArguments("-Duser.home=${Os.userHomeDirectory}", "--init-script", initScriptFile.path) From af19c695e28121cf96edd5186cc884731bac50c4 Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Fri, 15 Mar 2024 10:50:01 +0100 Subject: [PATCH 2/3] fix(gradle): Add a dedicated work-around for a Gradle 8.5 bug Signed-off-by: Sebastian Schuberth --- .../src/main/kotlin/GradleInspector.kt | 11 ++++++++++- .../package-managers/gradle/src/main/kotlin/Gradle.kt | 11 ++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/plugins/package-managers/gradle-inspector/src/main/kotlin/GradleInspector.kt b/plugins/package-managers/gradle-inspector/src/main/kotlin/GradleInspector.kt index d796df482b1ad..a34d36b8e1065 100644 --- a/plugins/package-managers/gradle-inspector/src/main/kotlin/GradleInspector.kt +++ b/plugins/package-managers/gradle-inspector/src/main/kotlin/GradleInspector.kt @@ -32,6 +32,7 @@ import org.apache.logging.log4j.kotlin.logger import org.gradle.tooling.GradleConnector import org.gradle.tooling.events.ProgressListener import org.gradle.tooling.internal.consumer.DefaultGradleConnector +import org.gradle.tooling.model.build.BuildEnvironment import org.ossreviewtoolkit.analyzer.AbstractPackageManagerFactory import org.ossreviewtoolkit.analyzer.PackageManager @@ -68,6 +69,8 @@ import org.ossreviewtoolkit.utils.ort.okHttpClient import org.ossreviewtoolkit.utils.ort.ortToolsDirectory import org.ossreviewtoolkit.utils.spdx.SpdxOperator +import org.semver4j.Semver + /** * The names of Gradle (Groovy, Kotlin script) build files for a Gradle project. */ @@ -156,12 +159,18 @@ class GradleInspector( .splitOnWhitespace() .map { it.unquote() } + val environment = connection.model(BuildEnvironment::class.java).get() + val buildGradleVersion = Semver.coerce(environment.gradle.gradleVersion) + + logger.info { "The project at '$projectDir' uses Gradle version $buildGradleVersion." } + // In order to debug the plugin, pass the "-Dorg.gradle.debug=true" option to the JVM running ORT. This will // then block execution of the plugin until a remote debug session is attached to port 5005 (by default), // also see https://docs.gradle.org/current/userguide/troubleshooting.html#sec:troubleshooting_build_logic. val model = connection.model(OrtDependencyTreeModel::class.java) .apply { - if (logger.delegate.isDebugEnabled) { + // Work around https://github.com/gradle/gradle/issues/28464. + if (logger.delegate.isDebugEnabled && buildGradleVersion?.isEqualTo("8.5.0") != true) { addProgressListener(ProgressListener { logger.debug(it.displayName) }) } } diff --git a/plugins/package-managers/gradle/src/main/kotlin/Gradle.kt b/plugins/package-managers/gradle/src/main/kotlin/Gradle.kt index f57e3acb4234f..26b3e378f4b81 100644 --- a/plugins/package-managers/gradle/src/main/kotlin/Gradle.kt +++ b/plugins/package-managers/gradle/src/main/kotlin/Gradle.kt @@ -36,6 +36,7 @@ import org.eclipse.aether.repository.WorkspaceRepository import org.gradle.tooling.GradleConnector import org.gradle.tooling.events.ProgressListener import org.gradle.tooling.internal.consumer.DefaultGradleConnector +import org.gradle.tooling.model.build.BuildEnvironment import org.ossreviewtoolkit.analyzer.AbstractPackageManagerFactory import org.ossreviewtoolkit.analyzer.PackageManager @@ -62,6 +63,8 @@ import org.ossreviewtoolkit.utils.common.temporaryProperties import org.ossreviewtoolkit.utils.common.unquote import org.ossreviewtoolkit.utils.ort.createOrtTempFile +import org.semver4j.Semver + private val GRADLE_USER_HOME = Os.env["GRADLE_USER_HOME"]?.let { File(it) } ?: Os.userHomeDirectory.resolve(".gradle") private val GRADLE_BUILD_FILES = listOf("build.gradle", "build.gradle.kts") @@ -212,9 +215,15 @@ class Gradle( val stdout = ByteArrayOutputStream() val stderr = ByteArrayOutputStream() + val environment = connection.model(BuildEnvironment::class.java).get() + val buildGradleVersion = Semver.coerce(environment.gradle.gradleVersion) + + logger.info { "The project at '$projectDir' uses Gradle version $buildGradleVersion." } + val dependencyTreeModel = connection.model(OrtDependencyTreeModel::class.java) .apply { - if (logger.delegate.isDebugEnabled) { + // Work around https://github.com/gradle/gradle/issues/28464. + if (logger.delegate.isDebugEnabled && buildGradleVersion?.isEqualTo("8.5.0") != true) { addProgressListener(ProgressListener { logger.debug(it.displayName) }) } } From 832261582fe1d866c2e22039d16864f7bf1dc47c Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Fri, 15 Mar 2024 10:14:53 +0100 Subject: [PATCH 3/3] fix(gradle-inspector): Use ORT's fixed-up user home directory Align with the code from the "legacy" Gradle package manager implementation. Signed-off-by: Sebastian Schuberth --- .../gradle-inspector/src/main/kotlin/GradleInspector.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/package-managers/gradle-inspector/src/main/kotlin/GradleInspector.kt b/plugins/package-managers/gradle-inspector/src/main/kotlin/GradleInspector.kt index a34d36b8e1065..33d80ca9fe382 100644 --- a/plugins/package-managers/gradle-inspector/src/main/kotlin/GradleInspector.kt +++ b/plugins/package-managers/gradle-inspector/src/main/kotlin/GradleInspector.kt @@ -177,7 +177,7 @@ class GradleInspector( .setJvmArguments(jvmArgs) .setStandardOutput(stdout) .setStandardError(stderr) - .withArguments("--init-script", initScriptFile.path) + .withArguments("-Duser.home=${Os.userHomeDirectory}", "--init-script", initScriptFile.path) .get() if (stdout.size() > 0) {