Skip to content

Commit 20e9536

Browse files
aarzillih9jiang
authored andcommitted
extension: use Delve's guess-substitute-path-helper when available
When the remote mode is specified try to call dlv guess-substitute-path-helper and if it is available use its ouptut and switch to dlv-dap mode unless otherwise specified. The legacy adapter has been unmaintained for years and has known bugs (for example, breakpoints can not be set while the program is running) but couldn't be deleted because it implements automatic mappings of source files. Updates #3096, #3193 Change-Id: Ie2f731b149e0ba4d5031bb57d61e7b407c3a40f8 Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/643280 Reviewed-by: Hongxiang Jiang <[email protected]> kokoro-CI: kokoro <[email protected]> Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent 1b300c4 commit 20e9536

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

extension/src/goDebugConfiguration.ts

+51-4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { parseEnvFiles } from './utils/envUtils';
3333
import { resolveHomeDir } from './utils/pathUtils';
3434
import { createRegisterCommand } from './commands';
3535
import { GoExtensionContext } from './context';
36+
import { spawn } from 'child_process';
3637

3738
let dlvDAPVersionChecked = false;
3839

@@ -182,12 +183,23 @@ export class GoDebugConfigurationProvider implements vscode.DebugConfigurationPr
182183
debugConfiguration['debugAdapter'] = dlvConfig['debugAdapter'];
183184
}
184185
}
185-
// If neither launch.json nor settings.json gave us the debugAdapter value, we go with the default
186-
// from package.json (dlv-dap) unless this is remote attach with a stable release.
186+
187+
// If neither launch.json nor settings.json gave us the debugAdapter, we go with the default from pacakge.json (dlv-dap) for all modes except 'remote'.
188+
// For remote we will use 'dlv-dap' if we can call 'dlv substitute-path-guess-helper' or 'legacy' otherwise.
187189
if (!debugConfiguration['debugAdapter']) {
190+
// set dlv-dap by default
188191
debugConfiguration['debugAdapter'] = defaultConfig.debugAdapter.default;
189-
if (debugConfiguration['mode'] === 'remote' && !extensionInfo.isPreview) {
190-
debugConfiguration['debugAdapter'] = 'legacy';
192+
if (debugConfiguration['mode'] === 'remote') {
193+
const substitutePathGuess = await this.guessSubstitutePath();
194+
if (substitutePathGuess === null) {
195+
if (!extensionInfo.isPreview) {
196+
// can't guess substitute path and isPreview isn't set, fall back to legacy.
197+
debugConfiguration['debugAdapter'] = 'legacy';
198+
}
199+
} else {
200+
debugConfiguration['debugAdapter'] = defaultConfig.debugAdapter.default;
201+
debugConfiguration['guessSubstitutePath'] = substitutePathGuess;
202+
}
191203
}
192204
}
193205
if (debugConfiguration['debugAdapter'] === 'dlv-dap') {
@@ -372,6 +384,41 @@ export class GoDebugConfigurationProvider implements vscode.DebugConfigurationPr
372384
return debugConfiguration;
373385
}
374386

387+
/**
388+
* Calls `dlv substitute-path-guess-helper` to get a set of parameters used by Delve to guess the substitutePath configuration after also examining the executable.
389+
*
390+
* See https://github.com/go-delve/delve/blob/d5fb3bee427202f0d4b1683bf743bfd2adb41757/service/debugger/debugger.go#L2466
391+
*/
392+
private async guessSubstitutePath(): Promise<object | null> {
393+
return new Promise((resolve) => {
394+
const child = spawn(getBinPath('dlv'), ['substitute-path-guess-helper']);
395+
let stdoutData = '';
396+
let stderrData = '';
397+
child.stdout.on('data', (data) => {
398+
stdoutData += data;
399+
});
400+
child.stderr.on('data', (data) => {
401+
stderrData += data;
402+
});
403+
404+
child.on('close', (code) => {
405+
if (code !== 0) {
406+
resolve(null);
407+
} else {
408+
try {
409+
resolve(JSON.parse(stdoutData));
410+
} catch (error) {
411+
resolve(null);
412+
}
413+
}
414+
});
415+
416+
child.on('error', (error) => {
417+
resolve(null);
418+
});
419+
});
420+
}
421+
375422
public removeGcflags(args: string): { args: string; removed: boolean } {
376423
// From `go help build`
377424
// ...

0 commit comments

Comments
 (0)