Skip to content

Commit 99742c9

Browse files
authored
SONARIAC-2775 Fix Docker build on Java 21 (#111)
In Java 21 there is a change in JDK around starting new processes, the PATH environment variable is not taken into account. This PR fix it.
1 parent 26aaaad commit 99742c9

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

gradle-modules/src/main/kotlin/org.sonarsource.cloud-native.go-binary-builder.gradle.kts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import org.sonarsource.cloudnative.gradle.GoBuild
2121
import org.sonarsource.cloudnative.gradle.allGoSourcesAndMakeScripts
2222
import org.sonarsource.cloudnative.gradle.callMake
2323
import org.sonarsource.cloudnative.gradle.crossCompileEnv
24+
import org.sonarsource.cloudnative.gradle.findExecutable
2425
import org.sonarsource.cloudnative.gradle.getArchitecture
2526
import org.sonarsource.cloudnative.gradle.getPlatform
2627
import org.sonarsource.cloudnative.gradle.goLangCiLintVersion
@@ -50,6 +51,8 @@ goBuildExtension.dockerCommands.convention(
5051
)
5152

5253
if (isCi()) {
54+
val goLangCiLintExecutable = findExecutable("golangci-lint")
55+
5356
val cleanGoCode by tasks.registering(Exec::class) {
5457
description = "Clean all compiled version of the go code."
5558
group = "build"
@@ -92,7 +95,7 @@ if (isCi()) {
9295
outputs.cacheIf { true }
9396

9497
commandLine(
95-
"golangci-lint",
98+
goLangCiLintExecutable,
9699
"run",
97100
// Don't limit the number of issues in the report
98101
"--max-issues-per-linter=0",

gradle-modules/src/main/kotlin/org.sonarsource.cloud-native.go-docker-environment.gradle.kts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ import org.sonarsource.cloudnative.gradle.GO_LICENSES_OUTPUT_DIR
2020
import org.sonarsource.cloudnative.gradle.GoBuild
2121
import org.sonarsource.cloudnative.gradle.allGoSourcesAndMakeScripts
2222
import org.sonarsource.cloudnative.gradle.crossCompileEnv
23+
import org.sonarsource.cloudnative.gradle.findExecutable
2324
import org.sonarsource.cloudnative.gradle.goLangCiLintVersion
2425
import org.sonarsource.cloudnative.gradle.goVersion
2526
import org.sonarsource.cloudnative.gradle.isCi
2627

28+
val dockerExecutable = findExecutable("docker")
29+
2730
val goBuildExtension = extensions.findByType<GoBuild>() ?: extensions.create<GoBuild>("goBuild")
2831

2932
val buildDockerImage by tasks.registering(Exec::class) {
@@ -49,7 +52,7 @@ val buildDockerImage by tasks.registering(Exec::class) {
4952
val noTrafficInspection = "false" == System.getProperty("trafficInspection")
5053

5154
val arguments = buildList {
52-
add("docker")
55+
add(dockerExecutable)
5356
add("buildx")
5457
add("build")
5558
add("--file")
@@ -102,7 +105,7 @@ val dockerTasks = goBuildExtension.dockerCommands.map { tasksToCommands ->
102105

103106
val workDir = goBuildExtension.dockerWorkDir.get()
104107
commandLine(
105-
"docker",
108+
dockerExecutable,
106109
"run",
107110
"--rm",
108111
"--network=host",

gradle-modules/src/main/kotlin/org/sonarsource/cloudnative/gradle/BuildUtils.kt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,29 @@ import org.gradle.internal.os.OperatingSystem
3131

3232
fun isCi() = System.getenv("CI")?.equals("true") == true
3333

34+
/**
35+
* Resolves the absolute path of an executable by searching the PATH environment variable.
36+
* This works around a JDK 21 issue where process creation may not properly resolve executables from PATH.
37+
* Falls back to the bare executable name if not found (letting the OS handle resolution).
38+
*/
39+
fun findExecutable(name: String): String {
40+
val pathSeparator = File.pathSeparator
41+
val executableExtensions = if (OperatingSystem.current().isWindows) listOf(".exe", ".cmd", ".bat", "") else listOf("")
42+
val pathDirs = System.getenv("PATH")
43+
?.split(pathSeparator)
44+
?.filter { it.isNotBlank() }
45+
?: return name
46+
for (dir in pathDirs) {
47+
for (ext in executableExtensions) {
48+
val candidate = File(dir, "$name$ext")
49+
if (candidate.isFile && candidate.canExecute()) {
50+
return candidate.absolutePath
51+
}
52+
}
53+
}
54+
return name
55+
}
56+
3457
fun Project.signingCondition(): Boolean {
3558
val branch = System.getenv("GITHUB_REF_NAME") ?: System.getenv("CIRRUS_BRANCH") ?: ""
3659
return (branch == "master" || branch.matches("branch-.+".toRegex())) &&
@@ -100,7 +123,7 @@ fun checkJarEntriesPathUniqueness(file: File) {
100123

101124
fun Project.commitHashProvider(ref: String = "HEAD") =
102125
providers.exec {
103-
commandLine("git", "rev-parse", ref)
126+
commandLine(findExecutable("git"), "rev-parse", ref)
104127
}.standardOutput.asText
105128

106129
fun getPlatform(): String {

0 commit comments

Comments
 (0)