Skip to content

Commit

Permalink
src/goDebugConfiguration: take program verbatim with external adapter
Browse files Browse the repository at this point in the history
As a fix for #1677, the extension massages launch
configurations to start dlv from the package directory and locate
'program' to be relative from the package directory.

This heuristic does not work well if dlv dap adapter is launched
externally so dlv runs from other directory. Until `delveCWD` or
a similar mechanism that allows to control where delve runs the
build, we take the launch configuration verbatim and avoid any
mutation.

Relative paths are resolved as described in the descriptions in
package.json. This changes only the internal mechanics.

Fixes #1793

Change-Id: Ic651be25a692dbb23a51707d5ebc274f22a3c532
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/351272
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>
  • Loading branch information
hyangah committed Sep 22, 2021
1 parent 022e403 commit cfee3e1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/goDebugConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ export class GoDebugConfigurationProvider implements vscode.DebugConfigurationPr
(attr) => debugConfiguration[attr] && !path.isAbsolute(debugConfiguration[attr])
);
if (debugAdapter === 'dlv-dap') {
// relative paths -> absolute paths
// 1. Relative paths -> absolute paths
if (entriesWithRelativePaths.length > 0) {
const workspaceRoot = folder?.uri.fsPath;
if (workspaceRoot) {
Expand All @@ -408,8 +408,18 @@ export class GoDebugConfigurationProvider implements vscode.DebugConfigurationPr
);
}
}
// compute build dir, and translate the dirname in program to a path relative to buildDir.
if (debugConfiguration.request === 'launch') {
// 2. For launch debug/test modes that builds the debug target,
// delve needs to be launched from the right directory (inside the main module of the target).
// Compute the launch dir heuristically, and translate the dirname in program to a path relative to buildDir.
// We skip this step when working with externally launched debug adapter
// because we do not control the adapter's launch process.
if (
debugConfiguration.request === 'launch' &&
// Presence of the following attributes indicates externally launched debug adapter.
!debugConfiguration.port &&
!debugConfiguration.host &&
!debugConfiguration.debugServer
) {
const mode = debugConfiguration['mode'] || 'debug';
if (['debug', 'test', 'auto'].includes(mode)) {
// Massage config to build the target from the package directory
Expand Down
46 changes: 46 additions & 0 deletions test/integration/goDebugConfiguration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,52 @@ suite('Debug Configuration Converts Relative Paths', () => {
);
});

test('program and __buildDir are not updated when working with externally launched adapters', () => {
const config: vscode.DebugConfiguration = debugConfig('dlv-dap');
config.program = path.join('foo', 'bar', 'pkg');
config.port = 12345;
const workspaceFolder = {
uri: vscode.Uri.file(os.tmpdir()),
name: 'test',
index: 0
};
const { program, cwd, __buildDir } = debugConfigProvider.resolveDebugConfigurationWithSubstitutedVariables(
workspaceFolder,
config
);
assert.deepStrictEqual(
{ program, cwd, __buildDir },
{
program: path.join(os.tmpdir(), 'foo', 'bar', 'pkg'),
cwd: os.tmpdir(),
__buildDir: undefined
}
);
});

test('program and __buildDir are not updated when working with externally launched adapters (debugServer)', () => {
const config: vscode.DebugConfiguration = debugConfig('dlv-dap');
config.program = path.join('foo', 'bar', 'pkg');
config.debugServer = 4777;
const workspaceFolder = {
uri: vscode.Uri.file(os.tmpdir()),
name: 'test',
index: 0
};
const { program, cwd, __buildDir } = debugConfigProvider.resolveDebugConfigurationWithSubstitutedVariables(
workspaceFolder,
config
);
assert.deepStrictEqual(
{ program, cwd, __buildDir },
{
program: path.join(os.tmpdir(), 'foo', 'bar', 'pkg'),
cwd: os.tmpdir(),
__buildDir: undefined
}
);
});

test('empty, undefined paths are not affected', () => {
const config = debugConfig('dlv-dap');
config.program = undefined;
Expand Down

0 comments on commit cfee3e1

Please sign in to comment.