Skip to content

Commit fd1233f

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 a8fb60c commit fd1233f

4 files changed

Lines changed: 74 additions & 4 deletions

File tree

docs/settings.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,21 @@ Default: `"proxy"`
528528

529529
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.
530530

531+
Default: `""`
532+
### `go.toolsManagement.buildEnvVars`
533+
534+
Environment variables that will be used to install the Go tools.
535+
536+
Default: `{}`
537+
### `go.toolsManagement.buildFlags`
538+
539+
Flags to `go install` used to install the Go tools. (e.g. `["-ldflags", "-s -w"]`)
540+
541+
Default: `[]`
542+
### `go.toolsManagement.buildTags`
543+
544+
The Go build tags used to install the Go tools.
545+
531546
Default: `""`
532547
### `go.trace.server`
533548

extension/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,6 +1639,27 @@
16391639
"description": "Automatically update the tools used by the extension, without prompting the user.",
16401640
"scope": "resource"
16411641
},
1642+
"go.toolsManagement.buildEnvVars": {
1643+
"type": "object",
1644+
"default": {},
1645+
"description": "Environment variables that will be used to install the Go tools.",
1646+
"scope": "machine-overridable"
1647+
},
1648+
"go.toolsManagement.buildFlags": {
1649+
"type": "array",
1650+
"items": {
1651+
"type": "string"
1652+
},
1653+
"default": [],
1654+
"description": "Flags to `go install` used to install the Go tools. (e.g. [\"-ldflags\", \"-s -w\"])",
1655+
"scope": "machine-overridable"
1656+
},
1657+
"go.toolsManagement.buildTags": {
1658+
"type": "string",
1659+
"default": "",
1660+
"description": "The Go build tags used to install the Go tools.",
1661+
"scope": "machine-overridable"
1662+
},
16421663
"go.enableCodeLens": {
16431664
"type": "object",
16441665
"properties": {

extension/src/goEnv.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,38 @@ export function toolInstallationEnvironment(): NodeJS.Dict<string> {
4545
delete env['GOENV'];
4646
delete env['GO111MODULE']; // we require module mode (default) for tools installation.
4747

48+
const toolsBuildEnv = getGoConfig().get('toolsManagement.buildEnvVars') ?? {};
49+
if (toolsBuildEnv && typeof toolsBuildEnv === 'object') {
50+
Object.keys(toolsBuildEnv).forEach(
51+
(key) =>
52+
(env[key] =
53+
typeof toolsBuildEnv[key] === 'string'
54+
? resolvePath(substituteEnv(toolsBuildEnv[key]))
55+
: toolsBuildEnv[key])
56+
);
57+
}
58+
4859
return env;
4960
}
5061

62+
export function toolInstallationArgs(buildArgs?: string[]): string[] {
63+
const goConfig = getGoConfig();
64+
const buildFlags: string[] = goConfig.get('toolsManagement.buildFlags') ?? [];
65+
const buildTags: string = goConfig.get('toolsManagement.buildTags') ?? "";
66+
67+
const args: string[] = ['install'];
68+
args.push(...buildFlags);
69+
if (buildTags.length > 0 && buildFlags.indexOf('-tags') === -1) {
70+
args.push('-tags');
71+
args.push(buildTags);
72+
}
73+
if (buildArgs) {
74+
args.push(...buildArgs);
75+
}
76+
77+
return args;
78+
}
79+
5180
// toolExecutionEnvironment returns the environment in which tools should
5281
// be executed. It always returns a new object.
5382
export function toolExecutionEnvironment(uri?: vscode.Uri, addProcessEnv = true): NodeJS.Dict<string> {

extension/src/goInstallTools.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import path = require('path');
1414
import semver = require('semver');
1515
import { ConfigurationTarget } from 'vscode';
1616
import { extensionInfo, getGoConfig, getGoplsConfig } from './config';
17-
import { toolExecutionEnvironment, toolInstallationEnvironment } from './goEnv';
17+
import { toolExecutionEnvironment, toolInstallationEnvironment, toolInstallationArgs } from './goEnv';
1818
import { addGoRuntimeBaseToPATH, clearGoRuntimeBaseFromPATH } from './goEnvironmentStatus';
1919
import { GoExtensionContext } from './context';
2020
import { addGoStatus, initGoStatusBar, outputChannel, removeGoStatus } from './goStatus';
@@ -372,9 +372,11 @@ async function installToolWithGoInstall(goVersion: GoVersion, env: NodeJS.Dict<s
372372
cwd: getWorkspaceFolderPath()
373373
};
374374

375+
const args: string[] = toolInstallationArgs(['-v', importPath]);
376+
375377
const execFile = util.promisify(cp.execFile);
376378
outputChannel.trace(`${goBinary} install -v ${importPath} (cwd: ${opts.cwd})`);
377-
await execFile(goBinary, ['install', '-v', importPath], opts);
379+
await execFile(goBinary, args, opts);
378380
}
379381

380382
export function declinedToolInstall(toolName: string): boolean {
@@ -1009,8 +1011,11 @@ export async function maybeInstallVSCGO(
10091011
if (!goBinary) {
10101012
throw new Error('"go" binary is not found');
10111013
}
1012-
const args = ['install', '-trimpath', `${importPath}${version}`];
1013-
console.log(`installing vscgo: ${args.join(' ')}`);
1014+
1015+
const originArgs: string[] = ['-trimpath', `${importPath}${version}`];
1016+
const args: string[] = toolInstallationArgs(originArgs);
1017+
1018+
console.log(`installing vscgo: install ${originArgs.join(' ')}`);
10141019
await execFile(goBinary, args, { cwd, env });
10151020
return progPath;
10161021
} catch (e) {

0 commit comments

Comments
 (0)