Skip to content

Commit b6187d8

Browse files
committed
package.json: add go.toolsManagement.go
This new setting allows users to specify the go command for tools installation. The go version selected for go.toolsManagement.go is not yet used for version check in goInstallTools.ts inspectGoToolVersion yet. For #825 Change-Id: Ic7a401b2089fe1e412ede32be474ee0f309c32d4 Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/387956 Trust: Hyang-Ah Hana Kim <hyangah@gmail.com> Reviewed-by: Suzy Mueller <suzmue@golang.org>
1 parent 6399238 commit b6187d8

File tree

4 files changed

+52
-18
lines changed

4 files changed

+52
-18
lines changed

Diff for: docs/settings.md

+5
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,11 @@ Allowed Options:
454454

455455

456456
Default: `"proxy"`
457+
### `go.toolsManagement.go`
458+
459+
The path to the `go` binary used to install the Go tools. If it's empty, the same `go` binary chosen for the project will be used for tool installation.
460+
461+
Default: `""`
457462
### `go.trace.server`
458463

459464
Trace the communication between VS Code and the Go language server.<br/>

Diff for: package.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@
116116
"go.goroot",
117117
"go.inferGopath",
118118
"go.toolsGopath",
119-
"go.toolsEnvVars"
119+
"go.toolsEnvVars",
120+
"go.toolsManagement.go"
120121
]
121122
}
122123
},
@@ -1528,6 +1529,12 @@
15281529
"description": "The logging level the extension logs at, defaults to 'error'",
15291530
"scope": "machine-overridable"
15301531
},
1532+
"go.toolsManagement.go": {
1533+
"type": "string",
1534+
"default": "",
1535+
"description": "The path to the `go` binary used to install the Go tools. If it's empty, the same `go` binary chosen for the project will be used for tool installation.",
1536+
"scope": "machine-overridable"
1537+
},
15311538
"go.toolsManagement.checkForUpdates": {
15321539
"type": "string",
15331540
"default": "proxy",

Diff for: src/goEnvironmentStatus.ts

+7-12
Original file line numberDiff line numberDiff line change
@@ -213,18 +213,13 @@ async function downloadGo(goOption: GoEnvironmentOption): Promise<GoEnvironmentO
213213
outputChannel.clear();
214214
outputChannel.show();
215215
outputChannel.appendLine(`go install ${goOption.binpath}@latest`);
216-
const result = await installTool(
217-
{
218-
name: newExecutableName,
219-
importPath: goOption.binpath,
220-
modulePath: goOption.binpath,
221-
description: newExecutableName,
222-
isImportant: false
223-
},
224-
await getGoVersion(),
225-
toolInstallationEnvironment(),
226-
true
227-
);
216+
const result = await installTool({
217+
name: newExecutableName,
218+
importPath: goOption.binpath,
219+
modulePath: goOption.binpath,
220+
description: newExecutableName,
221+
isImportant: false
222+
});
228223
if (result) {
229224
outputChannel.appendLine(`Error installing ${goOption.binpath}: ${result}`);
230225
throw new Error('Could not install ${goOption.binpath}');

Diff for: src/goInstallTools.ts

+32-5
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import {
3939
GoVersion,
4040
rmdirRecursive
4141
} from './util';
42-
import { correctBinname, envPath, getCurrentGoRoot, setCurrentGoRoot } from './utils/pathUtils';
42+
import { correctBinname, envPath, executableFileExists, getCurrentGoRoot, setCurrentGoRoot } from './utils/pathUtils';
4343
import util = require('util');
4444
import vscode = require('vscode');
4545

@@ -96,12 +96,30 @@ export async function installAllTools(updateExistingToolsOnly = false) {
9696
);
9797
}
9898

99+
export async function getGoForInstall(goVersion?: GoVersion, silent?: boolean): Promise<GoVersion> {
100+
const configured = getGoConfig().get<string>('toolsManagement.go');
101+
if (!configured) {
102+
return goVersion;
103+
}
104+
if (executableFileExists(configured)) {
105+
const go = await getGoVersion(configured);
106+
if (go) return go;
107+
}
108+
if (!silent) {
109+
outputChannel.appendLine(
110+
`Ignoring misconfigured 'go.toolsManagement.go' (${configured}). Provide a valid Go command.`
111+
);
112+
}
113+
return goVersion;
114+
}
115+
99116
/**
100117
* Installs given array of missing tools. If no input is given, the all tools are installed
101118
*
102119
* @param missing array of tool names and optionally, their versions to be installed.
103120
* If a tool's version is not specified, it will install the latest.
104-
* @param goVersion version of Go that affects how to install the tool. (e.g. modules vs legacy GOPATH mode)
121+
* @param goVersion version of Go used in the project. If go used for tools installation
122+
* is not configured or misconfigured, this is used as a fallback.
105123
* @returns a list of tools that failed to install.
106124
*/
107125
export async function installTools(
@@ -118,6 +136,7 @@ export async function installTools(
118136
}
119137
outputChannel.clear();
120138

139+
const goForInstall = await getGoForInstall(goVersion);
121140
const envForTools = toolInstallationEnvironment();
122141
const toolsGopath = envForTools['GOPATH'];
123142
let envMsg = `Tools environment: GOPATH=${toolsGopath}`;
@@ -162,7 +181,7 @@ export async function installTools(
162181
for (const tool of missing) {
163182
const modulesOffForTool = modulesOff;
164183

165-
const failed = await installTool(tool, goVersion, envForTools, !modulesOffForTool);
184+
const failed = await installToolWithGo(tool, goForInstall, envForTools, !modulesOffForTool);
166185
if (failed) {
167186
failures.push({ tool, reason: failed });
168187
} else if (tool.name === 'gopls') {
@@ -201,9 +220,17 @@ async function tmpDirForToolInstallation() {
201220
return toolsTmpDir;
202221
}
203222

204-
export async function installTool(
223+
// installTool installs the specified tool.
224+
export async function installTool(tool: ToolAtVersion): Promise<string> {
225+
const goVersion = await getGoForInstall(await getGoVersion());
226+
const envForTools = toolInstallationEnvironment();
227+
228+
return await installToolWithGo(tool, goVersion, envForTools, true);
229+
}
230+
231+
async function installToolWithGo(
205232
tool: ToolAtVersion,
206-
goVersion: GoVersion,
233+
goVersion: GoVersion, // go version to be used for installation.
207234
envForTools: NodeJS.Dict<string>,
208235
modulesOn: boolean
209236
): Promise<string> {

0 commit comments

Comments
 (0)