Skip to content

Commit

Permalink
[vscode-js-debug] Read host name from output
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mickaelistria committed Oct 14, 2023
1 parent 17be0cb commit 753e225
Showing 1 changed file with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String> 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) {
Expand All @@ -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);
Expand Down

0 comments on commit 753e225

Please sign in to comment.