Skip to content

Commit debd206

Browse files
committed
Avoid reporting the most common Java 8 attach-unavailable error
1 parent d535e63 commit debd206

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/interceptors/jvm.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { commandExists, canAccess } from '../util/fs';
1212

1313
type JvmTarget = { pid: string, name: string, interceptedByProxy: number | undefined };
1414

15+
const OLD_JAVA_MISSING_ATTACH_CLASS = 'com/sun/tools/attach/AgentLoadException';
16+
1517
// Check that Java is present, and that it's compatible with agent attachment:
1618
const javaBinPromise: Promise<string | false> = (async () => {
1719
// Check what Java binaries might exist:
@@ -45,15 +47,29 @@ const javaBinPromise: Promise<string | false> = (async () => {
4547
)[0];
4648

4749
if (javaTestResults.length && !bestJava) {
48-
// If some Java is present, but none are working, we report the failures. Hoping that this will hunt
49-
// down some specific incompatibilities that we can better work around/detect.
50+
// Some Java binaries are present, but none are usable. Log the error output for debugging:
5051
javaTestResults.forEach((testResult) => {
5152
console.log(`Running ${testResult.javaBin}:`);
5253
console.log(testResult.output.stdout);
5354
console.log(testResult.output.stderr);
5455
});
5556

56-
throw new Error(`JVM attach not available, exited with ${javaTestResults[0].output.exitCode}`);
57+
// The most common reason for this is that outdated Java versions (most notably Java 8) don't include
58+
// the necessary APIs to attach to remote JVMs. That's inconvenient, but unavoidable & not unusual.
59+
// Fortunately, I think most active Java developers do have a recent version of Java installed.
60+
const nonOutdatedJavaErrors = javaTestResults.filter(({ output }) =>
61+
!output.stderr.includes(OLD_JAVA_MISSING_ATTACH_CLASS) &&
62+
!output.stdout.includes(OLD_JAVA_MISSING_ATTACH_CLASS)
63+
);
64+
65+
if (nonOutdatedJavaErrors.length === 0) {
66+
console.warn('Only older Java versions were detected - Java attach APIs are not available');
67+
return false;
68+
} else {
69+
// If we find any other unexpected Java errors, we report them, to aid with debugging and
70+
// detecting issues with unusual JVMs.
71+
throw new Error(`JVM attach test failed unusually - exited with ${nonOutdatedJavaErrors[0].output.exitCode}`);
72+
}
5773
} else if (bestJava) {
5874
return bestJava.javaBin;
5975
} else {

0 commit comments

Comments
 (0)