diff --git a/src/goImplementations.ts b/src/goImplementations.ts index 82f5a16bd..e847700a0 100644 --- a/src/goImplementations.ts +++ b/src/goImplementations.ts @@ -28,13 +28,23 @@ interface GuruImplementsOutput { export class GoImplementationProvider implements vscode.ImplementationProvider { public provideImplementation(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): Thenable { + // To keep `guru implements` fast we want to restrict the scope of the search to current workpsace + // If no workpsace is open, then no-op + let root = vscode.workspace.rootPath; + if (vscode.workspace.getWorkspaceFolder(document.uri)) { + root = vscode.workspace.getWorkspaceFolder(document.uri).uri.fsPath; + } + if (!root) { + vscode.window.showInformationMessage('Cannot find implementations when there is no workspace open.'); + return; + } + return new Promise((resolve, reject) => { if (token.isCancellationRequested) { return resolve(null); } let env = getToolsEnvVars(); - let cwd = vscode.workspace.getWorkspaceFolder(document.uri).uri.fsPath; - let listProcess = cp.execFile(getGoRuntimePath(), ['list', '-e', '-json'], { cwd, env }, (err, stdout, stderr) => { + let listProcess = cp.execFile(getGoRuntimePath(), ['list', '-e', '-json'], { cwd: root, env }, (err, stdout, stderr) => { if (err) { return reject(err); } @@ -47,7 +57,7 @@ export class GoImplementationProvider implements vscode.ImplementationProvider { let buildTags = '"' + vscode.workspace.getConfiguration('go', document.uri)['buildTags'] + '"'; let args = ['-scope', `${scope}/...`, '-json', '-tags', buildTags, 'implements', `${filename}:#${offset.toString()}`]; - let guruProcess = cp.execFile(goGuru, args, {env}, (err, stdout, stderr) => { + let guruProcess = cp.execFile(goGuru, args, { env }, (err, stdout, stderr) => { if (err && (err).code === 'ENOENT') { promptForMissingTool('guru'); return resolve(null); diff --git a/src/goMain.ts b/src/goMain.ts index 587423584..d63a8ffe8 100644 --- a/src/goMain.ts +++ b/src/goMain.ts @@ -127,10 +127,13 @@ export function activate(ctx: vscode.ExtensionContext): void { let gopath = getCurrentGoPath(); let wasInfered = vscode.workspace.getConfiguration('go', vscode.window.activeTextEditor ? vscode.window.activeTextEditor.document.uri : null)['inferGopath']; - let root = vscode.window.activeTextEditor ? (vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri) || vscode.window.activeTextEditor.document).uri.fsPath : vscode.workspace.rootPath; + let root = vscode.workspace.rootPath; + if (vscode.window.activeTextEditor && vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri)) { + root = vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri).uri.fsPath; + } // not only if it was configured, but if it was successful. - if (wasInfered && root.indexOf(gopath) === 0) { + if (wasInfered && root && root.indexOf(gopath) === 0) { const inferredFrom = vscode.window.activeTextEditor ? 'current folder' : 'workspace root'; vscode.window.showInformationMessage(`Current GOPATH is inferred from ${inferredFrom}: ${gopath}`); } else { diff --git a/src/goPath.ts b/src/goPath.ts index 30a0197da..302d579db 100644 --- a/src/goPath.ts +++ b/src/goPath.ts @@ -148,6 +148,10 @@ export function parseEnvFile(path: string): { [key: string]: string } { // Walks up given folder path to return the closest ancestor that has `src` as a child export function getInferredGopath(folderPath: string): string { + if (!folderPath) { + return; + } + let dirs = folderPath.toLowerCase().split(path.sep); // find src directory closest to given folder path diff --git a/src/goSignature.ts b/src/goSignature.ts index 14c5ca942..d20824a2d 100755 --- a/src/goSignature.ts +++ b/src/goSignature.ts @@ -19,7 +19,6 @@ export class GoSignatureHelpProvider implements SignatureHelpProvider { } public provideSignatureHelp(document: TextDocument, position: Position, token: CancellationToken): Promise { - let goConfig = this.goConfig; if (!this.goConfig) { this.goConfig = vscode.workspace.getConfiguration('go', document.uri); } @@ -30,6 +29,7 @@ export class GoSignatureHelpProvider implements SignatureHelpProvider { } let callerPos = this.previousTokenPosition(document, theCall.openParen); // Temporary fix to fall back to godoc if guru is the set docsTool + let goConfig = this.goConfig; if (goConfig['docsTool'] === 'guru') { goConfig = Object.assign({}, goConfig, { 'docsTool': 'godoc' }); } diff --git a/src/goSymbol.ts b/src/goSymbol.ts index c166f8482..83d746388 100644 --- a/src/goSymbol.ts +++ b/src/goSymbol.ts @@ -47,7 +47,14 @@ export class GoWorkspaceSymbolProvider implements vscode.WorkspaceSymbolProvider symbols.push(symbolInfo); }); }; - let root = vscode.window.activeTextEditor ? (vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri) || vscode.window.activeTextEditor.document).uri.fsPath : vscode.workspace.rootPath; + let root = vscode.workspace.rootPath; + if (vscode.window.activeTextEditor && vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri)) { + root = vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri).uri.fsPath; + } + if (!root) { + vscode.window.showInformationMessage('No workspace is open to find symbols.'); + return; + } return getWorkspaceSymbols(root, query).then(results => { let symbols: vscode.SymbolInformation[] = []; diff --git a/src/goTest.ts b/src/goTest.ts index 2803fc02f..16ec2d1cb 100644 --- a/src/goTest.ts +++ b/src/goTest.ts @@ -115,9 +115,17 @@ export function testCurrentPackage(goConfig: vscode.WorkspaceConfiguration, args * @param goConfig Configuration for the Go extension. */ export function testWorkspace(goConfig: vscode.WorkspaceConfiguration, args: any) { + let dir = vscode.workspace.rootPath; + if (vscode.window.activeTextEditor && vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri)) { + dir = vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri).uri.fsPath; + } + if (!dir) { + vscode.window.showInformationMessage('No workspace is open to run tests.'); + return; + } const testConfig = { goConfig: goConfig, - dir: vscode.window.activeTextEditor ? (vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri) || vscode.window.activeTextEditor.document).uri.fsPath : vscode.workspace.rootPath, + dir: dir, flags: getTestFlags(goConfig, args), includeSubDirectories: true }; diff --git a/src/util.ts b/src/util.ts index 5ab70f4a6..18c386bf1 100644 --- a/src/util.ts +++ b/src/util.ts @@ -309,8 +309,8 @@ export function getToolsEnvVars(): any { } export function getCurrentGoPath(workspaceUri?: vscode.Uri): string { - if (!workspaceUri && vscode.window.activeTextEditor) { - workspaceUri = (vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri) || vscode.window.activeTextEditor.document).uri; + if (!workspaceUri && vscode.window.activeTextEditor && vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri)) { + workspaceUri = vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri).uri; } const config = vscode.workspace.getConfiguration('go', workspaceUri); const currentRoot = workspaceUri ? workspaceUri.fsPath : vscode.workspace.rootPath;