Skip to content

Commit c337f32

Browse files
http-client-java, validate java runtime (#10521)
Validate JDK is not enough. We got case that seems javac is good, but java is on 8. Better to verify both.
1 parent adc2a2a commit c337f32

4 files changed

Lines changed: 66 additions & 1 deletion

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
changeKind: feature
3+
packages:
4+
- "@typespec/http-client-java"
5+
---
6+
7+
Validate Java runtime version in dependency check

packages/http-client-java/emitter/src/lib.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export const $lib = createTypeSpecLibrary({
2323
default:
2424
"Java Development Kit (JDK) is not found in PATH. Please install JDK 17 or above. Microsoft Build of OpenJDK can be downloaded from https://learn.microsoft.com/java/openjdk/download",
2525
jdkVersion: paramMessage`Java Development Kit (JDK) in PATH is version '${"javaVersion"}'. Please install JDK 17 or above. Microsoft Build of OpenJDK can be downloaded from https://learn.microsoft.com/java/openjdk/download`,
26+
java: "Java Runtime is not found in PATH. Please install JDK 17 or above. Microsoft Build of OpenJDK can be downloaded from https://learn.microsoft.com/java/openjdk/download",
27+
javaVersion: paramMessage`Java Runtime in PATH is version '${"javaVersion"}'. Please install JDK 17 or above. Microsoft Build of OpenJDK can be downloaded from https://learn.microsoft.com/java/openjdk/download`,
2628
maven:
2729
"Apache Maven is not found in PATH. Apache Maven can be downloaded from https://maven.apache.org/download.cgi",
2830
},

packages/http-client-java/emitter/src/validate.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,47 @@ export async function validateDependencies(
5151
}
5252
}
5353

54+
// Check Java Runtime and version
55+
try {
56+
const result = await spawnAsync("java", ["-version"], { stdio: "pipe" });
57+
const javaRuntimeVersion =
58+
findJavaRuntimeVersion(result.stdout) ?? findJavaRuntimeVersion(result.stderr);
59+
if (javaRuntimeVersion) {
60+
if (program && logDiagnostic) {
61+
trace(program, `Java Runtime in PATH is version ${javaRuntimeVersion}.`);
62+
}
63+
const javaMajorVersion = getJavaMajorVersion(javaRuntimeVersion);
64+
if (javaMajorVersion < 11) {
65+
if (program && logDiagnostic) {
66+
reportDiagnostic(program, {
67+
code: "invalid-java-sdk-dependency",
68+
messageId: "javaVersion",
69+
format: { javaVersion: javaRuntimeVersion },
70+
target: NoTarget,
71+
});
72+
}
73+
}
74+
}
75+
} catch (error: any) {
76+
if (error && "code" in error && error["code"] === "ENOENT") {
77+
if (program && logDiagnostic) {
78+
reportDiagnostic(program, {
79+
code: "invalid-java-sdk-dependency",
80+
messageId: "java",
81+
target: NoTarget,
82+
});
83+
}
84+
} else {
85+
if (program && logDiagnostic) {
86+
reportDiagnostic(program, {
87+
code: "unknown-error",
88+
format: { errorMessage: error.message },
89+
target: NoTarget,
90+
});
91+
}
92+
}
93+
}
94+
5495
// Check Maven
5596
// nodejs does not allow spawn of .cmd on win32
5697
const shell = process.platform === "win32";
@@ -113,6 +154,15 @@ export function getJavaMajorVersion(version: string): number {
113154
return 0;
114155
}
115156

157+
export function findJavaRuntimeVersion(output: string): string | undefined {
158+
// "java version "21.0.3"" or "openjdk version "17.0.11""
159+
const matches = output.match(/version "?([\d.]+)"?.*/);
160+
if (matches && matches.length > 1) {
161+
return matches[1];
162+
}
163+
return undefined;
164+
}
165+
116166
function findMavenVersion(output: string): string | undefined {
117167
// there is control characters in the output
118168
const matches = output.match(/.*Apache Maven ([\d.]+).*/);

packages/http-client-java/emitter/test/validate.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from "vitest";
2-
import { findJavaVersion, getJavaMajorVersion } from "../src/validate.js";
2+
import { findJavaRuntimeVersion, findJavaVersion, getJavaMajorVersion } from "../src/validate.js";
33

44
describe("validate", () => {
55
it("findJavaVersion", () => {
@@ -13,4 +13,10 @@ describe("validate", () => {
1313
expect(getJavaMajorVersion("21.0.3")).toBe(21);
1414
expect(getJavaMajorVersion("24")).toBe(24);
1515
});
16+
17+
it("findJavaRuntimeVersion", () => {
18+
expect(findJavaRuntimeVersion('java version "1.8.0_422"')).toBe("1.8.0");
19+
expect(findJavaRuntimeVersion('openjdk version "21.0.3" 2024-04-16')).toBe("21.0.3");
20+
expect(findJavaRuntimeVersion('openjdk version "17.0.11" 2024-04-16')).toBe("17.0.11");
21+
});
1622
});

0 commit comments

Comments
 (0)