Skip to content

Commit

Permalink
extension: customize build for tools
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rockdrilla committed Mar 15, 2024
1 parent 19afd64 commit df6c792
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
16 changes: 16 additions & 0 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br/>
Expand Down
24 changes: 24 additions & 0 deletions extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@
"go.inferGopath",
"go.toolsGopath",
"go.toolsEnvVars",
"go.toolsManagement.buildFlags",
"go.toolsManagement.buildTags",
"go.toolsManagement.envVars",
"go.toolsManagement.go"
]
}
Expand Down Expand Up @@ -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": {
Expand Down
17 changes: 15 additions & 2 deletions extension/src/goInstallTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -291,9 +292,21 @@ async function installToolWithGoInstall(goVersion: GoVersion, env: NodeJS.Dict<s
cwd: getWorkspaceFolderPath()
};

const goConfig = getGoConfig();
const buildFlags: string[] = goConfig.get('toolsManagement.buildFlags') ?? [];
const buildTags: string = goConfig.get('toolsManagement.buildTags') ?? "";

const buildArgs: string[] = ['install', '-v'];
buildArgs.push(...buildFlags);
if (buildTags.length > 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) {
Expand Down

0 comments on commit df6c792

Please sign in to comment.