From 753e22541629f282581f86838c351bf66787f2ae Mon Sep 17 00:00:00 2001 From: Mickael Istria Date: Sat, 14 Oct 2023 17:09:04 +0200 Subject: [PATCH] [vscode-js-debug] Read host name from output The host name to use may depend on network settings (eg IPv4 vs IPv6), so instead of hardcoding it, we read it from the debug adapter output. --- .../debug/node/VSCodeJSDebugDelegate.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/org.eclipse.wildwebdeveloper/src/org/eclipse/wildwebdeveloper/debug/node/VSCodeJSDebugDelegate.java b/org.eclipse.wildwebdeveloper/src/org/eclipse/wildwebdeveloper/debug/node/VSCodeJSDebugDelegate.java index 9d097cbcd3..1bb774e55d 100644 --- a/org.eclipse.wildwebdeveloper/src/org/eclipse/wildwebdeveloper/debug/node/VSCodeJSDebugDelegate.java +++ b/org.eclipse.wildwebdeveloper/src/org/eclipse/wildwebdeveloper/debug/node/VSCodeJSDebugDelegate.java @@ -30,7 +30,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; import java.util.function.Predicate; import java.util.regex.Pattern; @@ -179,14 +179,21 @@ public void launch(ILaunchConfiguration configuration, String mode, ILaunch laun processEnv.put("DA_TEST_DISABLE_TELEMETRY", Boolean.TRUE.toString()); Process vscodeJsDebugExec = DebugPlugin.exec(new String[] { NodeJSManager.getNodeJsLocation().getAbsolutePath(), file.getAbsolutePath(), Integer.toString(port) }, cwdFile, processEnv.entrySet().stream().map(entry -> entry.getKey() + '=' + entry.getValue()).toArray(String[]::new), false); IProcess vscodeJsDebugIProcess = DebugPlugin.newProcess(launch, vscodeJsDebugExec, "debug adapter"); - AtomicBoolean started = new AtomicBoolean(); + AtomicReference host = new AtomicReference<>(); // sometimes ::1, sometimes 127.0.0.1... + String portSuffix = ":" + port; vscodeJsDebugIProcess.getStreamsProxy().getOutputStreamMonitor().addListener((text, mon) -> { if (text.toLowerCase().contains("listening")) { - started.set(true); + for (String word : text.split(" ")) { + word = word.trim(); + if (word.endsWith(portSuffix)) { + host.set(word.substring(0, word.length() - portSuffix.length())); + return; + } + } } }); Instant request = Instant.now(); - while (!started.get() && Duration.between(request, Instant.now()).compareTo(Duration.ofSeconds(3)) < 3) { + while (host.get() == null && Duration.between(request, Instant.now()).compareTo(Duration.ofSeconds(3)) < 3) { try { Thread.sleep(100); } catch (InterruptedException e) { @@ -195,7 +202,7 @@ public void launch(ILaunchConfiguration configuration, String mode, ILaunch laun DSPLaunchDelegateLaunchBuilder builder = new DSPLaunchDelegateLaunchBuilder(configuration, mode, launch, monitor); - builder.setAttachDebugAdapter("::1", port); + builder.setAttachDebugAdapter(host.get(), port); builder.setMonitorDebugAdapter(configuration.getAttribute(DSPPlugin.ATTR_DSP_MONITOR_DEBUG_ADAPTER, false)); builder.setDspParameters(param); super.launch(builder);