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

Add go.toolCommands which specifies alternate command locations #1297

Merged
merged 10 commits into from
Jun 11, 2018
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,12 @@
"default": [],
"description": "Folder names (not paths) to ignore while using Go to Symbol in Workspace feature",
"scope": "resource"
},
"go.toolCommands": {
"type": "object",
"default": {},
"description": "Alternate paths for tools. Use when you want to use wrapper script for commands. They can be file names in GOROOT/bin or absolute paths.",
"scope": "resource"
}
}
},
Expand Down
6 changes: 4 additions & 2 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,8 @@ function sendTelemetryEventForConfig(goConfig: vscode.WorkspaceConfiguration) {
"removeTags": { "classification": "CustomerContent", "purpose": "FeatureInsight" },
"editorContextMenuCommands": { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
"liveErrors": { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
"codeLens": { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
"codeLens": { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
"toolCommands": { "classification": "CustomerContent", "purpose": "FeatureInsight" }
}
*/
sendTelemetryEvent('goConfig', {
Expand Down Expand Up @@ -458,7 +459,8 @@ function sendTelemetryEventForConfig(goConfig: vscode.WorkspaceConfiguration) {
removeTags: JSON.stringify(goConfig['removeTags']),
editorContextMenuCommands: JSON.stringify(goConfig['editorContextMenuCommands']),
liveErrors: JSON.stringify(goConfig['liveErrors']),
codeLens: JSON.stringify(goConfig['enableCodeLens'])
codeLens: JSON.stringify(goConfig['enableCodeLens']),
toolCommands: JSON.stringify(goConfig['toolCommands'])
});
}

Expand Down
53 changes: 46 additions & 7 deletions src/goPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,21 @@
import fs = require('fs');
import path = require('path');
import os = require('os');
import vscode = require('vscode');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is used by the debug adapter via the goDebug.ts file. The debug adapter runs in a different process than the extension host and so will not have access to vscode


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

export function getBinPathFromEnvVar(toolName: string, envVarValue: string, appendBinToPath: boolean): string {
toolName = correctBinname(toolName);
let binname = correctBinname(toolName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correctBinName gets run twice resulting in strings like golint.exe.exe in Windows

if (path.isAbsolute(binname)) {
binPathCache[toolName] = binname;
return binname;
}
if (envVarValue) {
let paths = envVarValue.split(path.delimiter);
for (let i = 0; i < paths.length; i++) {
let binpath = path.join(paths[i], appendBinToPath ? 'bin' : '', toolName);
let binpath = path.join(paths[i], appendBinToPath ? 'bin' : '', binname);
if (fileExists(binpath)) {
binPathCache[toolName] = binpath;
return binpath;
Expand All @@ -30,8 +35,14 @@ export function getBinPathFromEnvVar(toolName: string, envVarValue: string, appe
return null;
}

export function getBinPathWithPreferredGopath(binname: string, ...preferredGopaths) {
if (binPathCache[correctBinname(binname)]) return binPathCache[correctBinname(binname)];
export function getBinPathWithPreferredGopath(toolName: string, ...preferredGopaths) {
if (binPathCache[toolName]) return binPathCache[toolName];

let binname = correctBinname(toolName);
if (path.isAbsolute(binname)) {
// Configured by the user
return binname;
}

for (let i = 0; i < preferredGopaths.length; i++) {
if (typeof preferredGopaths[i] === 'string') {
Expand Down Expand Up @@ -59,11 +70,36 @@ export function getBinPathWithPreferredGopath(binname: string, ...preferredGopat
return binname;
}

function correctBinname(binname: string) {
/**
* Returns the actual name of the tool.
*
* This can be overridden with `go.toolCommands` configuration.
*
* @param toolName the name of the tool such as "go", "godoc" and so on.
*
* @return the actual name of the tool, such as "go", "go.exe", "/path/to/go".
* May be an absolute path or only a file name (relative to GOROOT/bin).
* Returns an absolute path only when it exists.
*/
function correctBinname(toolName: string) {
let goConfig = vscode.workspace.getConfiguration('go');
let toolCommands = goConfig['toolCommands'];
if (toolCommands[toolName]) {
let binname = toolCommands[toolName];
if (!path.isAbsolute(binname)) {
// Relative name like 'go.sh', 'go.bat'
return binname;
}
// Absolute path.
// Valid only when it exists.
if (fileExists(binname)) {
return binname;
}
}
if (process.platform === 'win32')
return binname + '.exe';
return toolName + '.exe';
else
return binname;
return toolName;
}

/**
Expand All @@ -74,6 +110,9 @@ function correctBinname(binname: string) {
export function getGoRuntimePath(): string {
if (runtimePathCache) return runtimePathCache;
let correctBinNameGo = correctBinname('go');
if (path.isAbsolute(correctBinNameGo)) {
return correctBinNameGo;
}
if (process.env['GOROOT']) {
let runtimePathFromGoRoot = path.join(process.env['GOROOT'], 'bin', correctBinNameGo);
if (fileExists(runtimePathFromGoRoot)) {
Expand Down