|
| 1 | +import * as vscode from 'vscode' |
| 2 | +import { EXTENSION_PREFIX } from '../../util'; |
| 3 | + |
| 4 | +async function getGoExtension() { |
| 5 | + const go = vscode.extensions.getExtension('golang.go'); |
| 6 | + if (go && !go.isActive) await go.activate(); |
| 7 | + return go?.exports; |
| 8 | +} |
| 9 | + |
| 10 | +export async function initGo(): Promise<vscode.Disposable> { |
| 11 | + // in the future, do any needed init work with the golang.go extension instance here |
| 12 | + return new vscode.Disposable(() => {}); |
| 13 | +} |
| 14 | + |
| 15 | +const warned = new Set<string>(); |
| 16 | + |
| 17 | +export async function getGoExecutable(fileName?: string): Promise<string | void> { |
| 18 | + // no executable in virtual workspace |
| 19 | + if (vscode.workspace.workspaceFolders?.every(f => f.uri.scheme !== 'file')) return |
| 20 | + const workspaceConfig = vscode.workspace.getConfiguration(EXTENSION_PREFIX); |
| 21 | + const pathOverride = workspaceConfig.get<string>('goExecutable'); |
| 22 | + if (pathOverride) { |
| 23 | + return Promise.resolve(vscode.workspace.fs.stat(vscode.Uri.file(pathOverride))).then( |
| 24 | + st => { |
| 25 | + if (st.type & vscode.FileType.File) return pathOverride; |
| 26 | + throw new Error('not a file') |
| 27 | + } |
| 28 | + ).catch(err => { |
| 29 | + vscode.window.showErrorMessage(`Failed to find Go binary at '${pathOverride}'. Please update ${EXTENSION_PREFIX}.goExecutable.`) |
| 30 | + }) |
| 31 | + } |
| 32 | + const ext = await getGoExtension(); |
| 33 | + const cmd = await ext?.settings.getExecutionCommand( |
| 34 | + 'go', |
| 35 | + fileName && vscode.Uri.file(fileName) |
| 36 | + ) |
| 37 | + if (cmd) return cmd.binPath |
| 38 | + const workspaceID = vscode.workspace.name || |
| 39 | + vscode.workspace.workspaceFolders?.map(f => f.uri.fsPath).join(',') || |
| 40 | + vscode.window.activeTextEditor?.document.uri.fsPath; |
| 41 | + if (workspaceID) { |
| 42 | + if (warned.has(workspaceID)) return; |
| 43 | + warned.add(workspaceID); |
| 44 | + } |
| 45 | + const installGo = 'Install Go extension'; |
| 46 | + const configGo = 'Configure Go extension'; |
| 47 | + const setPath = `Configure Socket`; |
| 48 | + vscode.window.showErrorMessage( |
| 49 | + `Socket failed to find a Go installation; please ${ext ? 'install the Go toolchain with' : 'install'} the Go extension or set ${EXTENSION_PREFIX}.goExecutable.`, |
| 50 | + ext ? configGo : installGo, |
| 51 | + setPath |
| 52 | + ).then(async res => { |
| 53 | + if (res === installGo) { |
| 54 | + vscode.env.openExternal(vscode.Uri.parse('vscode:extension/golang.go')); |
| 55 | + } else if (res === configGo) { |
| 56 | + vscode.commands.executeCommand('go.tools.install'); |
| 57 | + } else if (res === setPath) { |
| 58 | + await workspaceConfig.update('goExecutable', '', vscode.ConfigurationTarget.Global) |
| 59 | + vscode.commands.executeCommand('workbench.action.openSettingsJson'); |
| 60 | + } |
| 61 | + }) |
| 62 | +} |
0 commit comments