src/goDebugFactory: don't modify DebugConfiguration.port

This is a change to avoid modifying vscode.DebugConfiguration's
port attribute inside startDapServer function.
VSCode invokes DebugAdapterDescriptorFactory's
createDebugAdapterDescriptor with the same DebugSession
& DebugConfiguration objects when the session restart is
requested. If we mess with the port attribute, the next call
will be handled as if it was requested to connect to an external
DAP server. So, let's stop modifying it.

Updates golang/vscode-go#1347

Change-Id: I82d7b7f48924fd36e7a881be39fedf2e440e4809
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/305552
Trust: Hyang-Ah Hana Kim <hyangah@gmail.com>
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Suzy Mueller <suzmue@golang.org>
diff --git a/src/goDebugFactory.ts b/src/goDebugFactory.ts
index 3163d3c..9ad0e25 100644
--- a/src/goDebugFactory.ts
+++ b/src/goDebugFactory.ts
@@ -244,29 +244,28 @@
 	log?: (msg: string) => void,
 	logErr?: (msg: string) => void
 ): Promise<{ port: number; host: string; dlvDapServer?: ChildProcessWithoutNullStreams }> {
-	if (!configuration.host) {
-		configuration.host = '127.0.0.1';
-	}
+	const host = configuration.host || '127.0.0.1';
 
 	if (configuration.port) {
 		// If a port has been specified, assume there is an already
 		// running dap server to connect to.
-		return { port: configuration.port, host: configuration.host };
-	} else {
-		configuration.port = await getPort();
+		return { port: configuration.port, host };
 	}
+	const port = await getPort();
 	if (!log) {
 		log = appendToDebugConsole;
 	}
 	if (!logErr) {
 		logErr = appendToDebugConsole;
 	}
-	const dlvDapServer = await spawnDlvDapServerProcess(configuration, log, logErr);
-	return { dlvDapServer, port: configuration.port, host: configuration.host };
+	const dlvDapServer = await spawnDlvDapServerProcess(configuration, host, port, log, logErr);
+	return { dlvDapServer, port, host };
 }
 
 async function spawnDlvDapServerProcess(
 	launchArgs: vscode.DebugConfiguration,
+	host: string,
+	port: number,
 	log: (msg: string) => void,
 	logErr: (msg: string) => void
 ): Promise<ChildProcess> {
@@ -293,7 +292,7 @@
 	if (launchArgs.dlvFlags && launchArgs.dlvFlags.length > 0) {
 		dlvArgs.push(...launchArgs.dlvFlags);
 	}
-	dlvArgs.push(`--listen=${launchArgs.host}:${launchArgs.port}`);
+	dlvArgs.push(`--listen=${host}:${port}`);
 	if (launchArgs.showLog) {
 		dlvArgs.push('--log=' + launchArgs.showLog.toString());
 	}
diff --git a/test/integration/goDebug.test.ts b/test/integration/goDebug.test.ts
index 81665b0..473fa11 100644
--- a/test/integration/goDebug.test.ts
+++ b/test/integration/goDebug.test.ts
@@ -1807,6 +1807,7 @@
 		if (isDlvDap) {
 			const { port, dlvDapServer } = await proxy.startDapServer(debugConfig);
 			dlvDapProcess = dlvDapServer;
+			debugConfig.port = port; // let the debug test client connect to our dap server.
 			await dc.start(port);
 		}
 		return debugConfig;