Skip to content

Commit df6c792

Browse files
committed
extension: customize build for tools
This change allows one to specify build settings used to install tools. E.g. the one may specify "GOAMD64=v3" for tools (in order to match current machine specs) but proceed with defaults (i.e. "GOAMD64=v1") for builds/tests globally.
1 parent 19afd64 commit df6c792

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

docs/settings.md

+16
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,22 @@ Default: `"proxy"`
483483
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.
484484

485485
Default: `""`
486+
487+
### `go.toolsManagement.buildFlags`
488+
489+
Flags to `go install` used to install the Go tools. (e.g. `["-ldflags", "-s -w"]`)
490+
491+
Default: `[]`
492+
### `go.toolsManagement.buildTags`
493+
494+
The Go build tags used to install the Go tools.
495+
496+
Default: `""`
497+
### `go.toolsManagement.envVars`
498+
499+
Environment variables that will be used to install the Go tools.
500+
501+
Default: `{}`
486502
### `go.trace.server`
487503

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

extension/package.json

+24
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@
114114
"go.inferGopath",
115115
"go.toolsGopath",
116116
"go.toolsEnvVars",
117+
"go.toolsManagement.buildFlags",
118+
"go.toolsManagement.buildTags",
119+
"go.toolsManagement.envVars",
117120
"go.toolsManagement.go"
118121
]
119122
}
@@ -1544,6 +1547,27 @@
15441547
"description": "Automatically update the tools used by the extension, without prompting the user.",
15451548
"scope": "resource"
15461549
},
1550+
"go.toolsManagement.buildFlags": {
1551+
"type": "array",
1552+
"items": {
1553+
"type": "string"
1554+
},
1555+
"default": [],
1556+
"description": "Flags to `go install` used to install the Go tools. (e.g. [\"-ldflags\", \"-s -w\"])",
1557+
"scope": "machine-overridable"
1558+
},
1559+
"go.toolsManagement.buildTags": {
1560+
"type": "string",
1561+
"default": "",
1562+
"description": "The Go build tags used to install the Go tools.",
1563+
"scope": "machine-overridable"
1564+
},
1565+
"go.toolsManagement.envVars": {
1566+
"type": "object",
1567+
"default": {},
1568+
"description": "Environment variables that will be used to install the Go tools.",
1569+
"scope": "machine-overridable"
1570+
},
15471571
"go.enableCodeLens": {
15481572
"type": "object",
15491573
"properties": {

extension/src/goInstallTools.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ async function installToolWithGo(
263263
}
264264
}
265265

266-
const env = Object.assign({}, envForTools);
266+
const toolsBuildEnv = getGoConfig().get('toolsManagement.envVars') ?? {};
267+
const env = Object.assign({}, envForTools, toolsBuildEnv);
267268

268269
let version: semver.SemVer | string | undefined | null = tool.version;
269270
if (!version && tool.usePrereleaseInPreviewMode && extensionInfo.isPreview) {
@@ -291,9 +292,21 @@ async function installToolWithGoInstall(goVersion: GoVersion, env: NodeJS.Dict<s
291292
cwd: getWorkspaceFolderPath()
292293
};
293294

295+
const goConfig = getGoConfig();
296+
const buildFlags: string[] = goConfig.get('toolsManagement.buildFlags') ?? [];
297+
const buildTags: string = goConfig.get('toolsManagement.buildTags') ?? "";
298+
299+
const buildArgs: string[] = ['install', '-v'];
300+
buildArgs.push(...buildFlags);
301+
if (buildTags.length > 0 && buildFlags.indexOf('-tags') === -1) {
302+
buildArgs.push('-tags');
303+
buildArgs.push(buildTags);
304+
}
305+
buildArgs.push(importPath);
306+
294307
const execFile = util.promisify(cp.execFile);
295308
outputChannel.trace(`$ ${goBinary} install -v ${importPath}} (cwd: ${opts.cwd})`);
296-
await execFile(goBinary, ['install', '-v', importPath], opts);
309+
await execFile(goBinary, buildArgs, opts);
297310
}
298311

299312
export function declinedToolInstall(toolName: string) {

0 commit comments

Comments
 (0)