Skip to content

Commit 03a8315

Browse files
committed
src/goDebugFactory.ts: fix problem with attach requests
The current launch logic for delve dap assumes that the program attribute is set. This is not true for attach requests. Use the current directory to launch delve dap for attach requests. Change-Id: I9acdc81c3227b5134c4858400e2df95abbc68b81 Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/322030 Trust: Suzy Mueller <[email protected]> Run-TryBot: Suzy Mueller <[email protected]> TryBot-Result: kokoro <[email protected]> Reviewed-by: Hyang-Ah Hana Kim <[email protected]>
1 parent 82d2b5d commit 03a8315

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

src/debugAdapter/goDebug.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ function logError(...args: any[]) {
357357
}
358358

359359
export function findPathSeparator(filePath: string) {
360-
return filePath.includes('\\') ? '\\' : '/';
360+
return filePath && filePath.includes('\\') ? '\\' : '/';
361361
}
362362

363363
// Comparing two different file paths while ignoring any different path separators.

src/goDebugConfiguration.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,12 @@ export class GoDebugConfigurationProvider implements vscode.DebugConfigurationPr
186186
if (!debugConfiguration.hasOwnProperty('substitutePath') && dlvConfig.hasOwnProperty('substitutePath')) {
187187
debugConfiguration['substitutePath'] = dlvConfig['substitutePath'];
188188
}
189-
if (debugConfiguration.request === 'attach' && !debugConfiguration['cwd']) {
189+
if (
190+
debugAdapter !== 'dlv-dap' &&
191+
debugConfiguration.request === 'attach' &&
192+
debugConfiguration.mode === 'remote' &&
193+
!debugConfiguration['cwd']
194+
) {
190195
debugConfiguration['cwd'] = '${workspaceFolder}';
191196
if (vscode.workspace.workspaceFolders?.length > 1) {
192197
debugConfiguration['cwd'] = '${fileWorkspaceFolder}';

src/goDebugFactory.ts

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import * as net from 'net';
1616
import { getTool } from './goTools';
1717
import { Logger, TimestampedLogger } from './goLogging';
1818
import { DebugProtocol } from 'vscode-debugprotocol';
19+
import { getWorkspaceFolderPath } from './util';
1920

2021
export class GoDebugAdapterDescriptorFactory implements vscode.DebugAdapterDescriptorFactory {
2122
constructor(private outputChannel?: vscode.OutputChannel) {}
@@ -194,7 +195,7 @@ export class ProxyDebugAdapter implements vscode.DebugAdapter {
194195
// VSCode and a dlv dap process spawned and managed by this adapter.
195196
// It turns the process's stdout/stderrr into OutputEvent.
196197
export class DelveDAPOutputAdapter extends ProxyDebugAdapter {
197-
constructor(private config: vscode.DebugConfiguration, logger?: Logger) {
198+
constructor(private configuration: vscode.DebugConfiguration, logger?: Logger) {
198199
super(logger);
199200
this.connected = this.startAndConnectToServer();
200201
}
@@ -276,7 +277,7 @@ export class DelveDAPOutputAdapter extends ProxyDebugAdapter {
276277
private async startAndConnectToServer() {
277278
try {
278279
const { port, host, dlvDapServer } = await startDapServer(
279-
this.config,
280+
this.configuration,
280281
(msg) => this.outputEvent('stdout', msg),
281282
(msg) => this.outputEvent('stderr', msg),
282283
(msg) => {
@@ -336,17 +337,17 @@ async function startDapServer(
336337
}
337338

338339
function spawnDlvDapServerProcess(
339-
launchArgs: vscode.DebugConfiguration,
340+
launchAttachArgs: vscode.DebugConfiguration,
340341
host: string,
341342
port: number,
342343
log: (msg: string) => void,
343344
logErr: (msg: string) => void,
344345
logConsole: (msg: string) => void
345346
): Promise<ChildProcess> {
346-
const launchArgsEnv = launchArgs.env || {};
347+
const launchArgsEnv = launchAttachArgs.env || {};
347348
const env = Object.assign({}, process.env, launchArgsEnv);
348349

349-
const dlvPath = launchArgs.dlvToolPath ?? getTool('dlv-dap');
350+
const dlvPath = launchAttachArgs.dlvToolPath ?? getTool('dlv-dap');
350351

351352
if (!fs.existsSync(dlvPath)) {
352353
const envPath = process.env['PATH'] || (process.platform === 'win32' ? process.env['Path'] : null);
@@ -358,27 +359,29 @@ function spawnDlvDapServerProcess(
358359
);
359360
throw new Error('Cannot find Delve debugger (dlv dap)');
360361
}
361-
let dir = '';
362-
try {
363-
dir = parseProgramArgSync(launchArgs).dirname;
364-
} catch (err) {
365-
logErr(`Program arg: ${launchArgs.program}\n${err}\n`);
366-
throw err; // rethrow so the caller knows it failed.
362+
let dir = getWorkspaceFolderPath();
363+
if (launchAttachArgs.request === 'launch') {
364+
try {
365+
dir = parseProgramArgSync(launchAttachArgs).dirname;
366+
} catch (err) {
367+
logErr(`Program arg: ${launchAttachArgs.program}\n${err}\n`);
368+
throw err; // rethrow so the caller knows it failed.
369+
}
367370
}
368371

369372
const dlvArgs = new Array<string>();
370373
dlvArgs.push('dap');
371374
// add user-specified dlv flags first. When duplicate flags are specified,
372375
// dlv doesn't mind but accepts the last flag value.
373-
if (launchArgs.dlvFlags && launchArgs.dlvFlags.length > 0) {
374-
dlvArgs.push(...launchArgs.dlvFlags);
376+
if (launchAttachArgs.dlvFlags && launchAttachArgs.dlvFlags.length > 0) {
377+
dlvArgs.push(...launchAttachArgs.dlvFlags);
375378
}
376379
dlvArgs.push(`--listen=${host}:${port}`);
377-
if (launchArgs.showLog) {
378-
dlvArgs.push('--log=' + launchArgs.showLog.toString());
380+
if (launchAttachArgs.showLog) {
381+
dlvArgs.push('--log=' + launchAttachArgs.showLog.toString());
379382
}
380-
if (launchArgs.logOutput) {
381-
dlvArgs.push('--log-output=' + launchArgs.logOutput);
383+
if (launchAttachArgs.logOutput) {
384+
dlvArgs.push('--log-output=' + launchAttachArgs.logOutput);
382385
}
383386

384387
const onWindows = process.platform === 'win32';
@@ -387,7 +390,7 @@ function spawnDlvDapServerProcess(
387390
dlvArgs.push('--log-dest=3');
388391
}
389392

390-
const logDest = launchArgs.logDest;
393+
const logDest = launchAttachArgs.logDest;
391394
if (typeof logDest === 'number') {
392395
logErr(`Using a file descriptor for 'logDest' (${logDest}) is not allowed.\n`);
393396
throw new Error('Using a file descriptor for `logDest` is not allowed.');
@@ -511,9 +514,9 @@ function spawnDlvDapServerProcess(
511514
}
512515

513516
export function parseProgramArgSync(
514-
launchArgs: vscode.DebugConfiguration
517+
launchAttachArgs: vscode.DebugConfiguration
515518
): { program: string; dirname: string; programIsDirectory: boolean } {
516-
const program = launchArgs.program;
519+
const program = launchAttachArgs.program;
517520
if (!program) {
518521
throw new Error('The program attribute is missing in the debug configuration in launch.json');
519522
}
@@ -524,7 +527,7 @@ export function parseProgramArgSync(
524527
// TODO(hyangah): why can't the program be a package name?
525528
throw new Error('The program attribute must point to valid directory, .go file or executable.');
526529
}
527-
if (!programIsDirectory && launchArgs.mode !== 'exec' && path.extname(program) !== '.go') {
530+
if (!programIsDirectory && launchAttachArgs.mode !== 'exec' && path.extname(program) !== '.go') {
528531
throw new Error('The program attribute must be a directory or .go file in debug and test mode');
529532
}
530533
const dirname = programIsDirectory ? program : path.dirname(program);

0 commit comments

Comments
 (0)