This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 645
/
goInstallTools.ts
440 lines (401 loc) · 15.4 KB
/
goInstallTools.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------*/
'use strict';
import vscode = require('vscode');
import fs = require('fs');
import path = require('path');
import cp = require('child_process');
import { getLanguageServerToolPath } from './goLanguageServer';
import { envPath, getToolFromToolPath } from './goPath';
import { hideGoStatus, outputChannel, showGoStatus } from './goStatus';
import { containsString, containsTool, getConfiguredTools, getImportPath, getTool, hasModSuffix, isGocode, isWildcard, Tool } from './goTools';
import { getBinPath, getCurrentGoPath, getGoConfig, getGoVersion, getTempFilePath, getToolsGopath, GoVersion, resolvePath } from './util';
// declinedUpdates tracks the tools that the user has declined to update.
const declinedUpdates: Tool[] = [];
// declinedUpdates tracks the tools that the user has declined to install.
const declinedInstalls: Tool[] = [];
export async function installAllTools(updateExistingToolsOnly: boolean = false) {
const goVersion = await getGoVersion();
const allTools = getConfiguredTools(goVersion);
// Update existing tools by finding all tools the user has already installed.
if (updateExistingToolsOnly) {
installTools(allTools.filter(tool => {
const toolPath = getBinPath(tool.name);
return toolPath && path.isAbsolute(toolPath);
}), goVersion);
return;
}
// Otherwise, allow the user to select which tools to install or update.
vscode.window.showQuickPick(allTools.map(x => {
const item: vscode.QuickPickItem = {
label: x.name,
description: x.description
};
return item;
}), {
canPickMany: true,
placeHolder: 'Select the tools to install/update.'
}).then(selectedTools => {
if (!selectedTools) {
return;
}
installTools(selectedTools.map(x => getTool(x.label)), goVersion);
});
}
/**
* Installs given array of missing tools. If no input is given, the all tools are installed
*
* @param string[] array of tool names to be installed
*/
export function installTools(missing: Tool[], goVersion: GoVersion): Promise<void> {
const goRuntimePath = getBinPath('go');
if (!goRuntimePath) {
vscode.window.showErrorMessage(`Failed to run "go get" to install the packages as the "go" binary cannot be found in either GOROOT(${process.env['GOROOT']}) or PATH(${envPath})`);
return;
}
if (!missing) {
return;
}
// http.proxy setting takes precedence over environment variables
const httpProxy = vscode.workspace.getConfiguration('http', null).get('proxy');
let envForTools = Object.assign({}, process.env);
if (httpProxy) {
envForTools = Object.assign({}, process.env, {
http_proxy: httpProxy,
HTTP_PROXY: httpProxy,
https_proxy: httpProxy,
HTTPS_PROXY: httpProxy,
});
}
outputChannel.show();
outputChannel.clear();
// If the go.toolsGopath is set, use its value as the GOPATH for the "go get" child process.
// Else use the Current Gopath
let toolsGopath = getToolsGopath();
if (toolsGopath) {
// User has explicitly chosen to use toolsGopath, so ignore GOBIN
envForTools['GOBIN'] = '';
outputChannel.appendLine(`Using the value ${toolsGopath} from the go.toolsGopath setting.`);
} else {
toolsGopath = getCurrentGoPath();
outputChannel.appendLine(`go.toolsGopath setting is not set. Using GOPATH ${toolsGopath}`);
}
if (toolsGopath) {
const paths = toolsGopath.split(path.delimiter);
toolsGopath = paths[0];
envForTools['GOPATH'] = toolsGopath;
} else {
const msg = 'Cannot install Go tools. Set either go.gopath or go.toolsGopath in settings.';
vscode.window.showInformationMessage(msg, 'Open User Settings', 'Open Workspace Settings').then(selected => {
switch (selected) {
case 'Open User Settings':
vscode.commands.executeCommand('workbench.action.openGlobalSettings');
break;
case 'Open Workspace Settings':
vscode.commands.executeCommand('workbench.action.openWorkspaceSettings');
break;
}
});
return;
}
let installingMsg = `Installing ${missing.length} ${missing.length > 1 ? 'tools' : 'tool'} at ${toolsGopath}${path.sep}bin`;
// If the user is on Go >= 1.11, tools should be installed with modules enabled.
// This ensures that users get the latest tagged version, rather than master,
// which may be unstable.
let modulesOff = false;
if (goVersion.lt('1.11')) {
modulesOff = true;
} else {
installingMsg += ' in module mode.';
}
outputChannel.appendLine(installingMsg);
missing.forEach((missingTool, index, missing) => {
outputChannel.appendLine(' ' + missingTool.name);
});
outputChannel.appendLine(''); // Blank line for spacing.
// Install tools in a temporary directory, to avoid altering go.mod files.
const toolsTmpDir = fs.mkdtempSync(getTempFilePath('go-tools-'));
// Write a temporary go.mod file.
const tmpGoModFile = path.join(toolsTmpDir, 'go.mod');
fs.writeFileSync(tmpGoModFile, 'module tools');
return missing.reduce((res: Promise<string[]>, tool: Tool) => {
// Disable modules for tools which are installed with the "..." wildcard.
// TODO: ... will be supported in Go 1.13, so enable these tools to use modules then.
if (modulesOff || isWildcard(tool, goVersion)) {
envForTools['GO111MODULE'] = 'off';
} else {
envForTools['GO111MODULE'] = 'on';
}
return res.then(sofar => new Promise<string[]>((resolve, reject) => {
const opts = {
env: envForTools,
cwd: toolsTmpDir,
};
const callback = (err: Error, stdout: string, stderr: string) => {
// Make sure to run `go mod tidy` between tool installations.
// This avoids us having to create a fresh go.mod file for each tool.
if (!modulesOff) {
cp.execFileSync(goRuntimePath, ['mod', 'tidy'], opts);
}
if (err) {
outputChannel.appendLine('Installing ' + getImportPath(tool, goVersion) + ' FAILED');
const failureReason = tool.name + ';;' + err + stdout.toString() + stderr.toString();
resolve([...sofar, failureReason]);
} else {
outputChannel.appendLine('Installing ' + getImportPath(tool, goVersion) + ' SUCCEEDED');
resolve([...sofar, null]);
}
};
let closeToolPromise = Promise.resolve(true);
const toolBinPath = getBinPath(tool.name);
if (path.isAbsolute(toolBinPath) && isGocode(tool)) {
closeToolPromise = new Promise<boolean>((innerResolve) => {
cp.execFile(toolBinPath, ['close'], {}, (err, stdout, stderr) => {
if (stderr && stderr.indexOf('rpc: can\'t find service Server.') > -1) {
outputChannel.appendLine('Installing gocode aborted as existing process cannot be closed. Please kill the running process for gocode and try again.');
return innerResolve(false);
}
innerResolve(true);
});
});
}
closeToolPromise.then((success) => {
if (!success) {
resolve([...sofar, null]);
return;
}
const args = ['get', '-v'];
// Only get tools at master if we are not using modules.
if (modulesOff) {
args.push('-u');
}
// Tools with a "mod" suffix should not be installed,
// instead we run "go build -o" to rename them.
if (hasModSuffix(tool)) {
args.push('-d');
}
args.push(getImportPath(tool, goVersion));
cp.execFile(goRuntimePath, args, opts, (err, stdout, stderr) => {
if (stderr.indexOf('unexpected directory layout:') > -1) {
outputChannel.appendLine(`Installing ${tool.name} failed with error "unexpected directory layout". Retrying...`);
cp.execFile(goRuntimePath, args, opts, callback);
} else if (!err && hasModSuffix(tool)) {
const outputFile = path.join(toolsGopath, 'bin', process.platform === 'win32' ? `${tool.name}.exe` : tool.name);
cp.execFile(goRuntimePath, ['build', '-o', outputFile, getImportPath(tool, goVersion)], opts, callback);
} else {
callback(err, stdout, stderr);
}
});
});
}));
}, Promise.resolve([])).then(res => {
outputChannel.appendLine(''); // Blank line for spacing
const failures = res.filter(x => x != null);
if (failures.length === 0) {
if (containsString(missing, 'go-langserver') || containsString(missing, 'gopls')) {
outputChannel.appendLine('Reload VS Code window to use the Go language server');
}
outputChannel.appendLine('All tools successfully installed. You\'re ready to Go :).');
return;
}
outputChannel.appendLine(failures.length + ' tools failed to install.\n');
failures.forEach((failure, index, failures) => {
const reason = failure.split(';;');
outputChannel.appendLine(reason[0] + ':');
outputChannel.appendLine(reason[1]);
});
});
}
export async function promptForMissingTool(toolName: string) {
const tool = getTool(toolName);
// If user has declined to install this tool, don't prompt for it.
if (containsTool(declinedInstalls, tool)) {
return;
}
const goVersion = await getGoVersion();
// Show error messages for outdated tools.
if (goVersion.lt('1.9')) {
let outdatedErrorMsg;
switch (tool.name) {
case 'golint':
outdatedErrorMsg = 'golint no longer supports go1.8 or below, update your settings to use golangci-lint as go.lintTool and install golangci-lint';
break;
case 'gotests':
outdatedErrorMsg = 'Generate unit tests feature is not supported as gotests tool needs go1.9 or higher.';
break;
}
if (outdatedErrorMsg) {
vscode.window.showInformationMessage(outdatedErrorMsg);
return;
}
}
const installOptions = ['Install'];
let missing = await getMissingTools(goVersion);
if (!containsTool(missing, tool)) {
return;
}
missing = missing.filter(x => x === tool || tool.isImportant);
if (missing.length > 1) {
// Offer the option to install all tools.
installOptions.push('Install All');
}
const msg = `The "${tool.name}" command is not available. Run "go get -v ${getImportPath(tool, goVersion)}" to install.`;
vscode.window.showInformationMessage(msg, ...installOptions).then(selected => {
switch (selected) {
case 'Install':
installTools([tool], goVersion);
break;
case 'Install All':
installTools(missing, goVersion);
hideGoStatus();
break;
default:
// The user has declined to install this tool.
declinedInstalls.push(tool);
break;
}
});
}
export async function promptForUpdatingTool(toolName: string) {
const tool = getTool(toolName);
// If user has declined to update, then don't prompt.
if (containsTool(declinedUpdates, tool)) {
return;
}
const goVersion = await getGoVersion();
const updateMsg = `Your version of ${tool.name} appears to be out of date. Please update for an improved experience.`;
vscode.window.showInformationMessage(updateMsg, 'Update').then(selected => {
switch (selected) {
case 'Update':
installTools([tool], goVersion);
break;
default:
declinedUpdates.push(tool);
break;
}
});
}
export function updateGoPathGoRootFromConfig(): Promise<void> {
const goroot = getGoConfig()['goroot'];
if (goroot) {
process.env['GOROOT'] = resolvePath(goroot);
}
if (process.env['GOPATH'] && process.env['GOROOT'] && process.env['GOPROXY']) {
return Promise.resolve();
}
// If GOPATH is still not set, then use the one from `go env`
const goRuntimePath = getBinPath('go');
if (!goRuntimePath) {
vscode.window.showErrorMessage(`Failed to run "go env" to find GOPATH as the "go" binary cannot be found in either GOROOT(${process.env['GOROOT']}) or PATH(${envPath})`);
return;
}
const goRuntimeBasePath = path.dirname(goRuntimePath);
// cgo and a few other Go tools expect Go binary to be in the path
let pathEnvVar: string;
if (process.env.hasOwnProperty('PATH')) {
pathEnvVar = 'PATH';
} else if (process.platform === 'win32' && process.env.hasOwnProperty('Path')) {
pathEnvVar = 'Path';
}
if (goRuntimeBasePath
&& pathEnvVar
&& process.env[pathEnvVar]
&& (<string>process.env[pathEnvVar]).split(path.delimiter).indexOf(goRuntimeBasePath) === -1
) {
process.env[pathEnvVar] += path.delimiter + goRuntimeBasePath;
}
return new Promise<void>((resolve, reject) => {
cp.execFile(goRuntimePath, ['env', 'GOPATH', 'GOROOT', 'GOPROXY'], (err, stdout, stderr) => {
if (err) {
return reject();
}
const envOutput = stdout.split('\n');
if (!process.env['GOPATH'] && envOutput[0].trim()) {
process.env['GOPATH'] = envOutput[0].trim();
}
if (!process.env['GOROOT'] && envOutput[1] && envOutput[1].trim()) {
process.env['GOROOT'] = envOutput[1].trim();
}
if (!process.env['GOPROXY'] && envOutput[2] && envOutput[2].trim()) {
process.env['GOPROXY'] = envOutput[2].trim();
}
return resolve();
});
});
}
let alreadyOfferedToInstallTools = false;
export async function offerToInstallTools() {
if (alreadyOfferedToInstallTools) {
return;
}
alreadyOfferedToInstallTools = true;
const goVersion = await getGoVersion();
let missing = await getMissingTools(goVersion);
missing = missing.filter(x => x.isImportant);
if (missing.length > 0) {
showGoStatus('Analysis Tools Missing', 'go.promptforinstall', 'Not all Go tools are available on the GOPATH');
vscode.commands.registerCommand('go.promptforinstall', () => {
promptForInstall(missing, goVersion);
});
}
const usingSourceGraph = getToolFromToolPath(getLanguageServerToolPath()) === 'go-langserver';
if (usingSourceGraph && goVersion.gt('1.10')) {
const promptMsg = 'The language server from Sourcegraph is no longer under active development and it does not support Go modules as well. Please install and use the language server from Google or disable the use of language servers altogether.';
const disableLabel = 'Disable language server';
const installLabel = 'Install';
vscode.window.showInformationMessage(promptMsg, installLabel, disableLabel)
.then(selected => {
if (selected === installLabel) {
installTools([getTool('gopls')], goVersion)
.then(() => {
vscode.window.showInformationMessage('Reload VS Code window to enable the use of Go language server');
});
} else if (selected === disableLabel) {
const goConfig = getGoConfig();
const inspectLanguageServerSetting = goConfig.inspect('useLanguageServer');
if (inspectLanguageServerSetting.globalValue === true) {
goConfig.update('useLanguageServer', false, vscode.ConfigurationTarget.Global);
} else if (inspectLanguageServerSetting.workspaceFolderValue === true) {
goConfig.update('useLanguageServer', false, vscode.ConfigurationTarget.WorkspaceFolder);
}
}
});
}
function promptForInstall(missing: Tool[], goVersion: GoVersion) {
const installItem = {
title: 'Install',
command() {
hideGoStatus();
installTools(missing, goVersion);
}
};
const showItem = {
title: 'Show',
command() {
outputChannel.clear();
outputChannel.appendLine('Below tools are needed for the basic features of the Go extension.');
missing.forEach(x => outputChannel.appendLine(x.name));
}
};
vscode.window.showInformationMessage('Failed to find some of the Go analysis tools. Would you like to install them?', installItem, showItem).then(selection => {
if (selection) {
selection.command();
} else {
hideGoStatus();
}
});
}
}
function getMissingTools(goVersion: GoVersion): Promise<Tool[]> {
const keys = getConfiguredTools(goVersion);
return Promise.all<Tool>(keys.map(tool => new Promise<Tool>((resolve, reject) => {
const toolPath = getBinPath(tool.name);
fs.exists(toolPath, exists => {
resolve(exists ? null : tool);
});
}))).then(res => {
return res.filter(x => x != null);
});
}