From df6c79285b66da75b863ac6f2f284cff54a5da32 Mon Sep 17 00:00:00 2001 From: Konstantin Demin Date: Fri, 15 Mar 2024 15:25:35 +0300 Subject: [PATCH] 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. --- docs/settings.md | 16 ++++++++++++++++ extension/package.json | 24 ++++++++++++++++++++++++ extension/src/goInstallTools.ts | 17 +++++++++++++++-- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/docs/settings.md b/docs/settings.md index 468bfb3a44..2d3c4cb771 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -483,6 +483,22 @@ Default: `"proxy"` 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. Default: `""` + +### `go.toolsManagement.buildFlags` + +Flags to `go install` used to install the Go tools. (e.g. `["-ldflags", "-s -w"]`) + +Default: `[]` +### `go.toolsManagement.buildTags` + +The Go build tags used to install the Go tools. + +Default: `""` +### `go.toolsManagement.envVars` + +Environment variables that will be used to install the Go tools. + +Default: `{}` ### `go.trace.server` Trace the communication between VS Code and the Go language server.
diff --git a/extension/package.json b/extension/package.json index 3b28880a7d..4a098089c0 100644 --- a/extension/package.json +++ b/extension/package.json @@ -114,6 +114,9 @@ "go.inferGopath", "go.toolsGopath", "go.toolsEnvVars", + "go.toolsManagement.buildFlags", + "go.toolsManagement.buildTags", + "go.toolsManagement.envVars", "go.toolsManagement.go" ] } @@ -1544,6 +1547,27 @@ "description": "Automatically update the tools used by the extension, without prompting the user.", "scope": "resource" }, + "go.toolsManagement.buildFlags": { + "type": "array", + "items": { + "type": "string" + }, + "default": [], + "description": "Flags to `go install` used to install the Go tools. (e.g. [\"-ldflags\", \"-s -w\"])", + "scope": "machine-overridable" + }, + "go.toolsManagement.buildTags": { + "type": "string", + "default": "", + "description": "The Go build tags used to install the Go tools.", + "scope": "machine-overridable" + }, + "go.toolsManagement.envVars": { + "type": "object", + "default": {}, + "description": "Environment variables that will be used to install the Go tools.", + "scope": "machine-overridable" + }, "go.enableCodeLens": { "type": "object", "properties": { diff --git a/extension/src/goInstallTools.ts b/extension/src/goInstallTools.ts index 490e77573c..9ba54af706 100644 --- a/extension/src/goInstallTools.ts +++ b/extension/src/goInstallTools.ts @@ -263,7 +263,8 @@ async function installToolWithGo( } } - const env = Object.assign({}, envForTools); + const toolsBuildEnv = getGoConfig().get('toolsManagement.envVars') ?? {}; + const env = Object.assign({}, envForTools, toolsBuildEnv); let version: semver.SemVer | string | undefined | null = tool.version; if (!version && tool.usePrereleaseInPreviewMode && extensionInfo.isPreview) { @@ -291,9 +292,21 @@ async function installToolWithGoInstall(goVersion: GoVersion, env: NodeJS.Dict 0 && buildFlags.indexOf('-tags') === -1) { + buildArgs.push('-tags'); + buildArgs.push(buildTags); + } + buildArgs.push(importPath); + const execFile = util.promisify(cp.execFile); outputChannel.trace(`$ ${goBinary} install -v ${importPath}} (cwd: ${opts.cwd})`); - await execFile(goBinary, ['install', '-v', importPath], opts); + await execFile(goBinary, buildArgs, opts); } export function declinedToolInstall(toolName: string) {