src/debugAdapter: let infer path logic handle non-existing files

When there is no matching candidate inside the target binary,
remoteSourcesAndPackages.remoteSourceFilesNameGrouping won't have
corresponding files and potentialPaths can be undefined.

At least, made me continue debugging the example described in
issue golang/vscode-go#1762. Not sure if that's the end of the bugs
yet.

Updates golang/vscode-go#1762

Change-Id: I2b69beb45397b0ea102a343ad6aad884c304f6b4
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/348972
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: Polina Sokolova <polina@google.com>
diff --git a/src/debugAdapter/goDebug.ts b/src/debugAdapter/goDebug.ts
index f7d3311..9eec60e 100644
--- a/src/debugAdapter/goDebug.ts
+++ b/src/debugAdapter/goDebug.ts
@@ -1038,7 +1038,7 @@
 	 * files with the same base names as filePath.
 	 */
 	protected findPathWithBestMatchingSuffix(filePath: string, potentialPaths: string[]): string | undefined {
-		if (!potentialPaths.length) {
+		if (!potentialPaths?.length) {
 			return;
 		}
 
diff --git a/test/integration/goDebug.test.ts b/test/integration/goDebug.test.ts
index 6c7ffb5..467588a 100644
--- a/test/integration/goDebug.test.ts
+++ b/test/integration/goDebug.test.ts
@@ -82,6 +82,19 @@
 		assert.strictEqual(inferredPath, '/app/hello-world/main.go');
 	});
 
+	test('inferRemotePathFromLocalPath does not crash due to non-existing files', () => {
+		const sourceFileMapping = new Map<string, string[]>();
+		sourceFileMapping.set('main.go', ['/app/hello-world/main.go', '/app/main.go']);
+
+		remoteSourcesAndPackages.remoteSourceFilesNameGrouping = sourceFileMapping;
+
+		// Non-existing file.
+		const inferredPath = goDebugSession['inferRemotePathFromLocalPath'](
+			'C:\\Users\\Documents\\src\\hello-world\\main-copy.go'
+		);
+		assert.strictEqual(inferredPath, undefined);
+	});
+
 	test('inferLocalPathFromRemoteGoPackage works for package in workspaceFolder', () => {
 		const remotePath = '/src/hello-world/morestrings/morestrings.go';
 		const helloPackage: PackageBuildInfo = {