Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

regression: gradle --version fails in bash, runs well in cmd, on an ENV variable issue for JAVA_HOME, with #477

Open
doctorpangloss opened this issue Nov 15, 2024 · 8 comments

Comments

@doctorpangloss
Copy link

doctorpangloss commented Nov 15, 2024

This issue is observed in busybox-w64u-PRE-5531-g0e958a72e.exe

This issue does not reproduce in

BusyBox v1.37.0-PRE-5288-ge960b0d69 (2024-01-31 08:44:10 GMT)

which lacks the fixes needed for chocolatey related to environment variables.

executing this script with C:/busybox64.exe bash -X ./check_install.sh

#!/busybox64.exe bash
set -eux

# test versions
java --version
echo $JAVA_HOME
echo $PATH
which java
java -XshowSettings:properties 2>&1 | grep 'java.home'
cmd.exe /c "gradle --version"
gradle --version

gradle --version fails where cmd.exe /c "gradle --version" succeeds

+ java --version
openjdk 21.0.2 2024-01-16
OpenJDK Runtime Environment GraalVM CE 21.0.2+13.1 (build 21.0.2+13-jvmci-23.1-b30)
OpenJDK 64-Bit Server VM GraalVM CE 21.0.2+13.1 (build 21.0.2+13-jvmci-23.1-b30, mixed mode, sharing)
C:\Java\graalvm-community-openjdk-21.0.2
C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Users\ContainerAdministrator\AppData\Local\Microsoft\WindowsApps;C:\Program Files\dotnet;C:\Users\ContainerAdministrator\.dotnet\tools;C:\Program Files\powershell;C:\ProgramData\chocolatey\bin;C:\Program Files\Git\bin;C:\Program Files\PowerShell\7\;C:\Docker\docker;C:\Java\graalvm-community-openjdk-21.0.2\bin;C:\Java\graalvm-community-openjdk-21.0.2\lib\svm\bin;C:\Program Files\nodejs;C:\Gradle\gradle-8.10.1\bin;C:\Program Files\Go\bin;C:\Program Files\Amazon\AWSCLIV2;C:\Program Files\dotnet;C:\Users\ContainerAdministrator\AppData\Roaming\npm;C:\Users\ContainerAdministrator\go\bin
+ echo 'C:\Java\graalvm-community-openjdk-21.0.2'
+ echo 'C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Users\ContainerAdministrator\AppData\Local\Microsoft\WindowsApps;C:\Program' 'Files\dotnet;C:\Users\ContainerAdministrator\.dotnet\tools;C:\Program' 'Files\powershell;C:\ProgramData\chocolatey\bin;C:\Program' 'Files\Git\bin;C:\Program' 'Files\PowerShell\7\;C:\Docker\docker;C:\Java\graalvm-community-openjdk-21.0.2\bin;C:\Java\graalvm-community-openjdk-21.0.2\lib\svm\bin;C:\Program' 'Files\nodejs;C:\Gradle\gradle-8.10.1\bin;C:\Program' 'Files\Go\bin;C:\Program' 'Files\Amazon\AWSCLIV2;C:\Program' 'Files\dotnet;C:\Users\ContainerAdministrator\AppData\Roaming\npm;C:\Users\ContainerAdministrator\go\bin'
+ which java
C:/Java/graalvm-community-openjdk-21.0.2/bin/java.exe
+ java -XshowSettings:properties
+ grep java.home
    java.home = C:\Java\graalvm-community-openjdk-21.0.2
+ cmd.exe /c 'gradle --version'

Welcome to Gradle 8.10.1!

Here are the highlights of this release:
 - Support for Java 23
 - Faster configuration cache
 - Better configuration cache reports

For more details see https://docs.gradle.org/8.10.1/release-notes.html


------------------------------------------------------------
Gradle 8.10.1
------------------------------------------------------------

Build time:    2024-09-09 07:42:56 UTC
Revision:      8716158d3ec8c59e38f87a67f1f311f297b79576

Kotlin:        1.9.24
Groovy:        3.0.22
Ant:           Apache Ant(TM) version 1.10.14 compiled on August 16 2023
Launcher JVM:  21.0.2 (GraalVM Community 21.0.2+13-jvmci-23.1-b30)
Daemon JVM:    C:\Java\graalvm-community-openjdk-21.0.2 (no JDK specified, using current Java home)
OS:            Windows Server 2022 10.0 amd64
+ gradle --version

ERROR: JAVA_HOME is set to an invalid directory: C:\Java\graalvm-community-openjdk-21.0.2

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.

You can install the prereqs by installing Chocolately and then using:

#!/busybox64.exe bash
set -eux
cd C:/install


add_path() {
  to_add=$1
  updated_path="${PATH};${to_add}"
  updated_path=$(echo "${updated_path}" | sed 's/\//\\/g')
  setx /M PATH "${updated_path}"
  export PATH="${updated_path}"
}

set_var() {
  var_to_add=$1
  value_to_add=$(echo "$2" | sed 's/\//\\/g')
  setx /M $var_to_add "${value_to_add}"
  export $var_to_add="${value_to_add}"
}


# graalvm
curl -sLO https://github.com/graalvm/graalvm-ce-builds/releases/download/jdk-21.0.2/graalvm-community-jdk-21.0.2_windows-x64_bin.zip
unzip -q graalvm-community-jdk-21.0.2_windows-x64_bin.zip -d C:/Java
mv C:/Java/graalvm-community-openjdk-21.0.2+13.1/ C:/Java/graalvm-community-openjdk-21.0.2/
add_path "C:/Java/graalvm-community-openjdk-21.0.2/bin"
add_path "C:/Java/graalvm-community-openjdk-21.0.2/lib/svm/bin"
set_var JAVA_HOME 'C:\Java\graalvm-community-openjdk-21.0.2'
rm graalvm-community-jdk-21.0.2_windows-x64_bin.zip


# gradle
mkdir 'C:\Gradle'
curl -sLO https://services.gradle.org/distributions/gradle-8.10.1-all.zip
unzip -q gradle-8.10.1-all.zip -d 'C:\Gradle'
rm gradle-8.10.1-all.zip
add_path "C:/Gradle/gradle-8.10.1/bin"
@ale5000-git
Copy link

@doctorpangloss
From cmd.exe executing gradle would execute gradle.bat, instead from inside busybox it prefer the gradle file without extension.

@doctorpangloss
Copy link
Author

Hmm well it works correctly in a build from January so I think it has something to do with environment variables that I do not understand.

@doctorpangloss doctorpangloss changed the title gradle --version fails in bash, runs well in cmd, on an ENV variable issue for JAVA_HOME, with regression: gradle --version fails in bash, runs well in cmd, on an ENV variable issue for JAVA_HOME, with Nov 15, 2024
@ale5000-git
Copy link

ale5000-git commented Nov 16, 2024

The preference of execution was changed in busybox but I don't remember the date.
You can still force it to use the bat by specify directly gradle.bat

@ale5000-git
Copy link

ale5000-git commented Nov 16, 2024

@doctorpangloss
gradle without extension isn't meant for Windows, but if you want to make it working just run this before executing:
unset JAVA_HOME

PS: java must be found in a folder inside PATH.

@rmyorston
Copy link
Owner

There's nothing wrong with the environment variable. The issue results from a change in the way executables are found. From PRE-5330 on 22nd April 2024 shell scripts take precedence over batch files.

The gradle shell script checks the validity of JAVA_HOME by testing for an executable in the expected directory. Because the script is for Unix systems it expects the name of the binary to be java, but on Windows the binary is called java.exe so the test fails.

There are various workarounds:

  • Explicitly execute the batch file gradle.bat;
  • Edit the gradle script to add the .exe suffix to the name of the binary;
  • Make a copy of or a link to java.exe named java.

A previous issue on this subject (#405) has additional information.

MSYS2 works around the problem by telling falsehoods about the result of the test. I'm reluctant to go down that route. While it may solve this particular issue it seems possible that it could break other perfectly valid tests.

@ale5000-git
Copy link

ale5000-git commented Nov 16, 2024

You can also create a profile file with this:

gradle() { gradle.bat "${@}"; }
gradlew() { gradlew.bat "${@}"; }

Or as I said before just unset JAVA_HOME, if JAVA_HOME is not set the script will search java in PATH and it won't have any issue with the extension.

@doctorpangloss
Copy link
Author

$ which gradle
C:/ProgramData/chocolatey/bin/gradle.exe

As an update: gradle is neither a .bat nor a .sh file here. I installed it via chocolatey.

I am also seeing that gradle.exe doesn't close its processes correctly while it is running, with error sh: C:\ProgramData\chocolatey\bin/gradle.exe: Error 0xffffffff, in busybox

@rmyorston
Copy link
Owner

@doctorpangloss I'm confused. In your initial post on this issue you provided a script to install gradle by unzipping it and adding C:/Gradle/gradle-8.10.1/bin to PATH. Now you say you installed it using Chocolatey.

How exactly did you install gradle?

When I install gradle using Chocolatey from PowerShell as Administrator it says:

Added C:\ProgramData\chocolatey\bin\gradle.exe shim pointed to '..\lib\gradle\tools\gradle-8.12\bin\gradle.bat'.

The bin directory containing the gradle and gradle.bat files isn't on PATH, instead the gradle.exe shim from Chocolatey's bin directory is invoked to run gradle.bat. And it works nicely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants