Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Microsoft/vscode-go into ma-go-bu…
Browse files Browse the repository at this point in the history
…ild-i

* 'master' of github.com:Microsoft/vscode-go:
  Moved debugging doc to a separate wiki
  Make a copy of process.env when setting VSCODE_GOTOOLS
  Debugger cannot read settings
  Improve error handling of test coverage (microsoft#721)
  Use proper plurality in output (microsoft#720)
  Fixes microsoft#303 Dont run test if current file is  not test file
  Added a note on how to debug single tests
  Allow Go tools to be installed in a separate GOPATH (microsoft#351)
  Fix typo in README.md (microsoft#715)
  Honor the 'cwd' launch configuration argument (microsoft#714)
  • Loading branch information
mattetti committed Jan 16, 2017
2 parents daa4e4b + ddb434c commit dc9549f
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 64 deletions.
63 changes: 6 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ This extension adds rich language support for the Go language to VS Code, includ
- Build-on-save (using `go build` and `go test`)
- Lint-on-save (using `golint` or `gometalinter`)
- Format (using `goreturns` or `goimports` or `gofmt`)
- Generate unit tests squeleton (using `gotests`)
- Generate unit tests skeleton (using `gotests`)
- Add Imports (using `gopkgs`)
- [_partially implemented_] Debugging (using `delve`)

Expand Down Expand Up @@ -109,64 +109,11 @@ In addition to integrated editing features, the extension also provides several

To use the debugger, you must currently manually install `delve`. See the [Installation Instructions](https://github.com/derekparker/delve/tree/master/Documentation/installation) for full details. On OS X it requires creating a self-signed cert to sign the `dlv` binary.

Once this is installed, go to the Code debug viewlet and select the configuration gear, placing the following in your launch.json:

```json
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceRoot}",
"env": {},
"args": []
}
]
}
```

The `program` option can refer to a package folder to debug, or a file within that folder.

The `mode` parameter can be set to:

* `debug` to compile the contents of the program folder and launch under the debugger. [default]
* `test` to debug tests in the program folder.
* `exec` to run a pre-built binary specified in program, for example `"program":"${workspaceRoot}/mybin"`.
* `remote` to attach to a remote headless Delve server. You must manually run Delve on the remote machine, and provide the additional `remotePath`, `host` and `port` debug configuration options pointing at the remote machine.
For more read [Debugging Go Code Using VS Code](https://github.com/Microsoft/vscode-go/wiki/Debugging-Go-code-using-VS-Code)

#### Remote Debugging

To remote debug using VS Code, you must first run a headless Delve server on the target machine. For example:

```bash
$ dlv debug --headless --listen=:2345 --log
```

Then, create a remote debug configuration in VS Code `launch.json`.

```json
{
"name": "Remote",
"type": "go",
"request": "launch",
"mode": "remote",
"remotePath": "${workspaceRoot}",
"port": 2345,
"host": "127.0.0.1",
"program": "${workspaceRoot}",
"env": {},
"args": []
}
```

When you launch the debugger with this new `Remote` target selected, VS Code will send debugging
commands to the `dlv` server you started previously instead of launching it's own `dlv` instance against your app.

The above example runs both the headless `dlv` server and the VS Code debugger locally on the same machine. For an
example of running these on different hosts, see the example of debugging a process running in a docker host at https://github.com/lukehoban/webapp-go/tree/debugging.
To remote debug using VS Code, read [Remote Debugging](https://github.com/Microsoft/vscode-go/wiki/Debugging-Go-code-using-VS-Code#remote-debugging)

## Building and Debugging the Extension

Expand Down Expand Up @@ -209,7 +156,9 @@ The extension uses the following tools, installed in the current GOPATH. If any
- guru: `go get -u -v golang.org/x/tools/cmd/guru`
- gotests: `go get -u -v github.com/cweill/gotests/...`

To install them just paste and run:
If you wish to have the extension use a separate GOPATH for its tools, set the VSCODE_GOTOOLS environment variable to the desired path.

To install the tools manually in the current GOPATH, just paste and run:
```bash
go get -u -v github.com/nsf/gocode
go get -u -v github.com/rogpeppe/godef
Expand Down
3 changes: 3 additions & 0 deletions src/debugAdapter/goDebug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ class Delve {
if (showLog) {
dlvArgs = dlvArgs.concat(['--log=' + showLog.toString()]);
}
if (cwd) {
dlvArgs = dlvArgs.concat(['--wd=' + cwd]);
}
if (buildFlags) {
dlvArgs = dlvArgs.concat(['--build-flags=' + buildFlags]);
}
Expand Down
7 changes: 5 additions & 2 deletions src/goCover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import os = require('os');
import fs = require('fs');
import { getBinPath, getGoRuntimePath } from './goPath';
import rl = require('readline');
import { outputChannel } from './goStatus';

let coveredHighLight = vscode.window.createTextEditorDecorationType({
// Green
Expand Down Expand Up @@ -107,8 +108,10 @@ export function getCoverage(filename: string): Promise<any[]> {
try {
// Clear existing coverage files
clearCoverage();
if (err && (<any>err).code === 'ENOENT') {
vscode.window.showInformationMessage('Could not generate coverage report. Install Go from http://golang.org/dl/.');
if (err && (<any>err).code !== 0) {
outputChannel.clear();
outputChannel.append(((<any>err).message));
outputChannel.show(true);
return resolve([]);
}

Expand Down
16 changes: 14 additions & 2 deletions src/goInstallTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,16 @@ export function promptForUpdatingTool(tool: string) {
function installTools(goVersion: SemVersion, missing?: string[]) {
let tools = getTools(goVersion);
let goRuntimePath = getGoRuntimePath();
if (!goRuntimePath) {
vscode.window.showInformationMessage('Cannot find "go" binary. Update PATH or GOROOT appropriately');
return;
}
if (!missing) {
missing = Object.keys(tools);
}
outputChannel.show();
outputChannel.clear();
outputChannel.appendLine('Installing ' + missing.length + ' tools');
outputChannel.appendLine(`Installing ${missing.length} ${missing.length > 1 ? 'tools' : 'tool'}`);
missing.forEach((missingTool, index, missing) => {
outputChannel.appendLine(' ' + missingTool);
});
Expand All @@ -117,7 +121,7 @@ function installTools(goVersion: SemVersion, missing?: string[]) {

// http.proxy setting takes precedence over environment variables
let httpProxy = vscode.workspace.getConfiguration('http').get('proxy');
let env = process.env;
let env = Object.assign({}, process.env);
if (httpProxy) {
env = Object.assign({}, process.env, {
http_proxy: httpProxy,
Expand All @@ -126,6 +130,14 @@ function installTools(goVersion: SemVersion, missing?: string[]) {
HTTPS_PROXY: httpProxy,
});
}

// If the VSCODE_GOTOOLS environment variable is set, use
// its value as the GOPATH for the "go get" child precess.
let toolsGoPath = process.env['VSCODE_GOTOOLS'];
if (toolsGoPath) {
env['GOPATH'] = toolsGoPath;
}

missing.reduce((res: Promise<string[]>, tool: string) => {
return res.then(sofar => new Promise<string[]>((resolve, reject) => {
cp.execFile(goRuntimePath, ['get', '-u', '-v', tools[tool]], { env }, (err, stdout, stderr) => {
Expand Down
12 changes: 9 additions & 3 deletions src/goPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import path = require('path');
import os = require('os');

let binPathCache: { [bin: string]: string; } = {};
let runtimePathCache: string = 'go';
let runtimePathCache: string = '';

export function getBinPathFromEnvVar(toolName: string, envVar: string, appendBinToPath: boolean): string {
toolName = correctBinname(toolName);
Expand All @@ -33,7 +33,13 @@ export function getBinPathFromEnvVar(toolName: string, envVar: string, appendBin
export function getBinPath(binname: string) {
if (binPathCache[correctBinname(binname)]) return binPathCache[correctBinname(binname)];

// First search each GOPATH workspace's bin folder
// First search VSCODE_GOTOOLS' bin folder
let pathFromToolsGoPath = getBinPathFromEnvVar(binname, 'VSCODE_GOTOOLS', true);
if (pathFromToolsGoPath) {
return pathFromToolsGoPath;
}

// Then search each GOPATH workspace's bin folder
let pathFromGoPath = getBinPathFromEnvVar(binname, 'GOPATH', true);
if (pathFromGoPath) {
return pathFromGoPath;
Expand Down Expand Up @@ -68,7 +74,7 @@ function correctBinname(binname: string) {
* @return the path to the Go binary.
*/
export function getGoRuntimePath(): string {
if (runtimePathCache !== 'go') return runtimePathCache;
if (runtimePathCache) return runtimePathCache;
let correctBinNameGo = correctBinname('go');
if (process.env['GOROOT']) {
runtimePathCache = path.join(process.env['GOROOT'], 'bin', correctBinNameGo);
Expand Down
8 changes: 8 additions & 0 deletions src/goTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export function testAtCursor(goConfig: vscode.WorkspaceConfiguration) {
vscode.window.showInformationMessage('No editor is active.');
return;
}
if (!editor.document.fileName.endsWith('_test.go')) {
vscode.window.showInformationMessage('No tests found. Current file is not a test file.');
return;
}
getTestFunctions(editor.document).then(testFunctions => {
let testFunction: vscode.SymbolInformation;
// Find any test function containing the cursor.
Expand Down Expand Up @@ -105,6 +109,10 @@ export function testCurrentFile(goConfig: vscode.WorkspaceConfiguration): Thenab
vscode.window.showInformationMessage('No editor is active.');
return;
}
if (!editor.document.fileName.endsWith('_test.go')) {
vscode.window.showInformationMessage('No tests found. Current file is not a test file.');
return;
}
return getTestFunctions(editor.document).then(testFunctions => {
return goTest({
goConfig: goConfig,
Expand Down

0 comments on commit dc9549f

Please sign in to comment.