Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
Ensure local debugee gets removed
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlad Barosan committed Mar 7, 2019
1 parent 6222ea4 commit b2fceb4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
8 changes: 2 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"extensionDependencies": [],
"dependencies": {
"diff": "^3.5.0",
"fs-extra": "^7.0.1",
"json-rpc2": "^1.0.2",
"vscode-debugadapter": "^1.32.1",
"vscode-debugprotocol": "^1.32.0",
Expand All @@ -47,7 +48,6 @@
"@types/fs-extra": "^5.0.4",
"@types/mocha": "^5.2.5",
"@types/node": "^6.14.0",
"fs-extra": "^7.0.0",
"tslint": "^5.11.0",
"typescript": "^3.1.3",
"vscode": "^1.1.26"
Expand Down
27 changes: 25 additions & 2 deletions src/debugAdapter/goDebug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

import * as path from 'path';
import * as os from 'os';
import * as fs from 'fs-extra';
import { DebugProtocol } from 'vscode-debugprotocol';
import { DebugSession, InitializedEvent, TerminatedEvent, ThreadEvent, StoppedEvent, OutputEvent, Thread, StackFrame, Scope, Source, Handles, LoggingDebugSession, Logger, logger } from 'vscode-debugadapter';
import { existsSync, lstatSync } from 'fs';
import { basename, dirname, extname } from 'path';
import { spawn, ChildProcess, execSync, spawnSync, execFile } from 'child_process';
import { Client, RPCConnection } from 'json-rpc2';
import { parseEnvFile, getBinPathWithPreferredGopath, resolveHomeDir, getInferredGopath, getCurrentGoWorkspaceFromGOPATH, envPath, fixDriveCasingInWindows } from '../goPath';
import { parseEnvFile, getBinPathWithPreferredGopath, getInferredGopath, getCurrentGoWorkspaceFromGOPATH, envPath, fixDriveCasingInWindows } from '../goPath';

// This enum should stay in sync with https://golang.org/pkg/reflect/#Kind

Expand Down Expand Up @@ -260,6 +261,7 @@ function normalizePath(filePath: string) {
class Delve {
program: string;
remotePath: string;
localDebugeePath: string | undefined;
debugProcess: ChildProcess;
loadConfig: LoadConfig;
connection: Promise<RPCConnection>;
Expand Down Expand Up @@ -391,7 +393,7 @@ class Delve {
return reject(`Cannot find Delve debugger. Install from https://github.com/derekparker/delve & ensure it is in your Go tools path, "GOPATH/bin" or "PATH".`);
}

let currentGOWorkspace = getCurrentGoWorkspaceFromGOPATH(env['GOPATH'], dirname);
const currentGOWorkspace = getCurrentGoWorkspaceFromGOPATH(env['GOPATH'], dirname);
let dlvArgs = [mode || 'debug'];
if (mode === 'exec') {
dlvArgs = dlvArgs.concat([program]);
Expand Down Expand Up @@ -436,6 +438,7 @@ class Delve {
env,
});

this.localDebugeePath = this.privateGetLocalDebugeePath(launchArgs.output);
function connectClient(port: number, host: string) {
// Add a slight delay to avoid issues on Linux with
// Delve failing calls made shortly after connection.
Expand Down Expand Up @@ -529,10 +532,30 @@ class Delve {
logError(`Failed to detach - ${(err.toString() || '')}`);
}
}
if (!this.isRemoteDebugging()) {
await this.ensureDebugeeExecutableIsRemoved();
}
return resolve();
});
}

private privateGetLocalDebugeePath(output: string | undefined): string {
const configOutput = output || "debug"
return path.isAbsolute(configOutput)
? configOutput
: path.resolve(this.program, configOutput)
}

private async ensureDebugeeExecutableIsRemoved(): Promise<void> {
try {
if (this.localDebugeePath && await fs.pathExists(this.localDebugeePath)) {
await fs.remove(this.localDebugeePath);
}
} catch (e) {
logError(`Failed to potentially remove leftover debug file ${this.localDebugeePath} - ${e.toString() || ""}`)
}
}

private isRemoteDebugging() {
return !this.debugProcess;
}
Expand Down

0 comments on commit b2fceb4

Please sign in to comment.