Skip to content

Commit 0e6e1b9

Browse files
fix: javac detection does not work as required by Gradle
The current `javac` detection tries to get the `javac` path from JAVA_HOME and to verify it is correct. In case it fails somewhere, it fallbacks to check the `javac` from PATH. So, from users' perspective it seems javac is setup correctly. However, Gradle works in slightly different manner - it checks if you have JAVA_HOME and if so - verifies `java` from it. In case there's no `java` in the `$JAVA_HOME/bin/java`, an error is thrown. Make our check for `javac` in the same way - in case you have set JAVA_HOME, we'll verify `javac` from there. This is exactly the same as the one we had prior 1.9.0 version, so this is just a regression fix. Add unit tests to cover this scenario.
1 parent 1d934b8 commit 0e6e1b9

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lib/sys-info.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class SysInfo implements NativeScriptDoctor.ISysInfo {
6161

6262
public getJavaCompilerVersion(): Promise<string> {
6363
return this.getValueForProperty(() => this.javaCompilerVerCache, async (): Promise<string> => {
64-
const javacVersion = (await this.getVersionOfJavaExecutableFromJavaHome(Constants.JAVAC_EXECUTABLE_NAME, SysInfo.JAVA_COMPILER_VERSION_REGEXP)) ||
64+
const javacVersion = process.env["JAVA_HOME"] ? (await this.getVersionOfJavaExecutableFromJavaHome(Constants.JAVAC_EXECUTABLE_NAME, SysInfo.JAVA_COMPILER_VERSION_REGEXP)) :
6565
(await this.getVersionOfJavaExecutableFromPath(Constants.JAVAC_EXECUTABLE_NAME, SysInfo.JAVA_COMPILER_VERSION_REGEXP));
6666

6767
return javacVersion;

test/sys-info.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,31 @@ describe("SysInfo unit tests", () => {
241241
assert.deepEqual(execCommands, ['which javac', '"javac" -version']);
242242
});
243243

244+
it("null when there is incorrect JAVA_HOME on non-Windows OS", async () => {
245+
const originalJavaHome = process.env[JavaHomeName];
246+
process.env[JavaHomeName] = "/some/invalid/dir/name/where/java/does/not/exist";
247+
248+
const result = await sysInfo.getJavaCompilerVersion();
249+
250+
process.env[JavaHomeName] = originalJavaHome;
251+
252+
assert.deepEqual(result, null);
253+
assert.deepEqual(execCommands, []);
254+
});
255+
256+
it("null when there is incorrect JAVA_HOME on Window OS", async () => {
257+
const originalJavaHome = process.env[JavaHomeName];
258+
hostInfo.isWindows = true;
259+
process.env[JavaHomeName] = "C:\\Program Files\\Not existing dir";
260+
261+
const result = await sysInfo.getJavaCompilerVersion();
262+
263+
process.env[JavaHomeName] = originalJavaHome;
264+
265+
assert.deepEqual(result, null);
266+
assert.deepEqual(execCommands, []);
267+
});
268+
244269
it("java compiler version when there is no JAVA_HOME on Window OS", async () => {
245270
const originalJavaHome = process.env[JavaHomeName];
246271
hostInfo.isWindows = true;

0 commit comments

Comments
 (0)