Skip to content

Commit

Permalink
Merge pull request goplus#47 from visualfc/goxls3
Browse files Browse the repository at this point in the history
check and install goxls
  • Loading branch information
xushiwei authored Dec 12, 2023
2 parents f3895bf + a8dc331 commit 5002f45
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2108,6 +2108,11 @@
"default": "gopls",
"description": "Alternate tool to use instead of the gopls binary or alternate path to use for the gopls binary."
},
"goxls": {
"type": "string",
"default": "goxls",
"description": "Alternate tool to use instead of the goxls binary or alternate path to use for the goxls binary."
},
"dlv": {
"type": "string",
"default": "dlv",
Expand Down
23 changes: 20 additions & 3 deletions src/goInstallTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export async function installTools(

const failures: { tool: ToolAtVersion; reason: string }[] = [];
for (const tool of missing) {
const failed = await installToolWithGo(tool, goForInstall, envForTools);
const failed = await installToolWithGo(tool, goForInstall, envForTools, tool.name === 'goxls');
if (failed) {
failures.push({ tool, reason: failed });
} else if (tool.name === 'goxls' || tool.name === 'gopls') {
Expand Down Expand Up @@ -243,7 +243,8 @@ export async function installTool(tool: ToolAtVersion): Promise<string | undefin
async function installToolWithGo(
tool: ToolAtVersion,
goVersion: GoVersion, // go version to be used for installation.
envForTools: NodeJS.Dict<string>
envForTools: NodeJS.Dict<string>,
useGop?: boolean
): Promise<string | undefined> {
// Some tools may have to be closed before we reinstall them.
if (tool.close) {
Expand All @@ -264,6 +265,8 @@ async function installToolWithGo(
try {
if (hasModSuffix(tool)) {
await installToolWithGoGet(tool, goVersion, env, importPath);
} else if (useGop) {
await installToolWithGopInstall(goVersion, env, importPath);
} else {
await installToolWithGoInstall(goVersion, env, importPath);
}
Expand All @@ -290,6 +293,20 @@ async function installToolWithGoInstall(goVersion: GoVersion, env: NodeJS.Dict<s
await execFile(goBinary, ['install', '-v', importPath], opts);
}

async function installToolWithGopInstall(goVersion: GoVersion, env: NodeJS.Dict<string>, importPath: string) {
// Unlike installToolWithGoGet, `go install` in module mode
// can run in the current directory safely. So, use the user-specified go tool path.
const goBinary = getBinPath('gop');
const opts = {
env,
cwd: getWorkspaceFolderPath()
};

const execFile = util.promisify(cp.execFile);
logVerbose(`$ ${goBinary} install -v ${importPath}} (cwd: ${opts.cwd})`);
await execFile(goBinary, ['install', '-v', importPath], opts);
}

async function installToolWithGoGet(
tool: ToolAtVersion,
goVersion: GoVersion,
Expand Down Expand Up @@ -470,7 +487,7 @@ export async function promptForUpdatingTool(
}

let choices: string[] = ['Update'];
if (toolName === 'goxls' || toolName === 'gopls') {
if (toolName === 'goxls') {
choices = ['Always Update', 'Update Once', 'Release Notes'];
}
if (toolName === 'dlv') {
Expand Down
3 changes: 2 additions & 1 deletion src/goTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { getFormatTool, usingCustomFormatTool } from './language/legacy/goFormat
import { goLiveErrorsEnabled } from './language/legacy/goLiveErrors';
import { allToolsInformation } from './goToolsInformation';
import { getBinPath, GoVersion } from './util';
import { conf } from './language/goLanguageServer';

export interface Tool {
name: string;
Expand Down Expand Up @@ -206,7 +207,7 @@ export function getConfiguredTools(
// Even though we arranged this to run after the first attempt to start gopls
// this is still useful if we've fail to start gopls.
if (useLanguageServer) {
maybeAddTool('gopls');
maybeAddTool(conf.lsName);
}

if (goLiveErrorsEnabled()) {
Expand Down
4 changes: 2 additions & 2 deletions src/language/goLanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ export function getLanguageServerToolPath(): string | undefined {
const alternateTools = goConfig['alternateTools'];
if (alternateTools) {
// The user's alternate language server was not found.
const goplsAlternate = alternateTools['gopls'];
const goplsAlternate = alternateTools[conf.lsName];
if (goplsAlternate) {
vscode.window.showErrorMessage(
`Cannot find the alternate tool ${goplsAlternate} configured for gopls.
Expand All @@ -1054,7 +1054,7 @@ Please install it and reload this VS Code window.`
}

// Prompt the user to install gopls.
promptForMissingTool('gopls');
promptForMissingTool(conf.lsName);
}

function allFoldersHaveSameGopath(): boolean {
Expand Down

0 comments on commit 5002f45

Please sign in to comment.