Skip to content

Commit

Permalink
[code] Enable go langserver under non-detach mode. (#42352) (#42675)
Browse files Browse the repository at this point in the history
* [code] Enable go langserver under non-detach mode.

* [code] Enable go langserver under non-detach mode #part 2.

* [code] Enable go langserver under non-detach mode #part 3

We should keep the kibana code data inside the 'kibana/data' . That
means go-langserver has nothing to do with dev machine except '$PATH'.

* [code] Enable go langserver under non-detach mode #part 4

Since we use socket as the process communication media, remove the
unused parameter.
  • Loading branch information
Henry Wong authored Aug 6, 2019
1 parent 5b262cd commit ed6eb0c
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 20 deletions.
8 changes: 4 additions & 4 deletions x-pack/legacy/plugins/code/server/lsp/abstract_launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
* you may not use this file except in compliance with the Elastic License.
*/

import fs from 'fs';
import { ChildProcess } from 'child_process';
import fs from 'fs';
import { ResponseError } from 'vscode-jsonrpc';
import { ILanguageServerLauncher } from './language_server_launcher';
import { LanguageServerStartFailed } from '../../common/lsp_error_codes';
import { Logger } from '../log';
import { ServerOptions } from '../server_options';
import { LoggerFactory } from '../utils/log_factory';
import { Logger } from '../log';
import { ILanguageServerLauncher } from './language_server_launcher';
import { LanguageServerProxy } from './proxy';
import { RequestExpander } from './request_expander';
import { LanguageServerStartFailed } from '../../common/lsp_error_codes';

let seqNo = 1;

Expand Down
89 changes: 77 additions & 12 deletions x-pack/legacy/plugins/code/server/lsp/go_launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { ChildProcess } from 'child_process';
import { spawn } from 'child_process';
import fs from 'fs';
import getPort from 'get-port';
import { Logger, MarkupKind } from 'vscode-languageserver-protocol';
import * as glob from 'glob';
import path from 'path';
import { MarkupKind } from 'vscode-languageserver-protocol';
import { Logger } from '../log';
import { ServerOptions } from '../server_options';
import { LoggerFactory } from '../utils/log_factory';
import { AbstractLauncher } from './abstract_launcher';
Expand All @@ -24,13 +28,6 @@ export class GoServerLauncher extends AbstractLauncher {
super('go', targetHost, options, loggerFactory);
}

async getPort() {
if (!this.options.lsp.detach) {
return await getPort();
}
return GO_LANG_DETACH_PORT;
}

createExpander(
proxy: LanguageServerProxy,
builtinWorkspace: boolean,
Expand All @@ -53,8 +50,76 @@ export class GoServerLauncher extends AbstractLauncher {
this.log
);
}
// TODO(henrywong): Once go langugage server ready to release, we should support this mode.
async spawnProcess(installationPath: string, port: number, log: Logger): Promise<ChildProcess> {
throw new Error('Go language server currently only support detach mode');

async startConnect(proxy: LanguageServerProxy) {
await proxy.connect();
}

async getPort() {
if (!this.options.lsp.detach) {
return await getPort();
}
return GO_LANG_DETACH_PORT;
}

private async getBundledGoToolchain(installationPath: string, log: Logger) {
const GoToolchain = glob.sync('**/go/**', {
cwd: installationPath,
});
if (!GoToolchain.length) {
return undefined;
}
return path.resolve(installationPath, GoToolchain[0]);
}

async spawnProcess(installationPath: string, port: number, log: Logger) {
const launchersFound = glob.sync('go-langserver', {
cwd: installationPath,
});
if (!launchersFound.length) {
throw new Error('Cannot find executable go language server');
}

let envPath = process.env.PATH;
const goToolchain = await this.getBundledGoToolchain(installationPath, log);
if (!goToolchain) {
throw new Error('Cannot find go toolchain in bundle installation');
}
// Construct $GOROOT from the bundled go toolchain.
const goRoot = goToolchain;
const goHome = path.resolve(goToolchain, 'bin');
envPath = envPath + ':' + goHome;
// Construct $GOPATH under 'kibana/data/code'.
const goPath = this.options.goPath;
if (!fs.existsSync(goPath)) {
fs.mkdirSync(goPath);
}

const params: string[] = ['-port=' + port.toString()];
const golsp = path.resolve(installationPath, launchersFound[0]);
const p = spawn(golsp, params, {
detached: false,
stdio: 'pipe',
env: {
...process.env,
CLIENT_HOST: '127.0.0.1',
CLIENT_PORT: port.toString(),
GOROOT: goRoot,
GOPATH: goPath,
PATH: envPath,
GO111MODULE: 'on',
CGO_ENABLED: '0',
},
});
p.stdout.on('data', data => {
log.stdout(data.toString());
});
p.stderr.on('data', data => {
log.stderr(data.toString());
});
log.info(
`Launch Go Language Server at port ${port.toString()}, pid:${p.pid}, GOROOT:${goRoot}`
);
return p;
}
}
12 changes: 8 additions & 4 deletions x-pack/legacy/plugins/code/server/lsp/language_servers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { ServerFacade } from '../..';
import { InstallationType } from '../../common/installation';
import { LanguageServer } from '../../common/language_server';
import { CTAGS_SUPPORT_LANGS, LanguageServer } from '../../common/language_server';
import { CtagsLauncher } from './ctags_launcher';
import { GoServerLauncher } from './go_launcher';
import { JavaLauncher } from './java_launcher';
import { LauncherConstructor } from './language_server_launcher';
import { TypescriptServerLauncher } from './ts_launcher';
import { CTAGS_SUPPORT_LANGS } from '../../common/language_server';
import { ServerFacade } from '../..';

export interface LanguageServerDefinition extends LanguageServer {
builtinWorkspaceFolders: boolean;
Expand Down Expand Up @@ -53,8 +52,13 @@ export const GO: LanguageServerDefinition = {
languages: ['go'],
launcher: GoServerLauncher,
installationType: InstallationType.Plugin,
installationPluginName: 'goLanguageServer',
installationPluginName: 'go-langserver',
priority: 2,
installationFolderName: 'golsp',
downloadUrl: (version: string, devMode?: boolean) =>
devMode!
? `https://snapshots.elastic.co/downloads/go-langserver-plugins/go-langserver/go-langserver-${version}-SNAPSHOT-$OS.zip`
: `https://artifacts.elastic.co/downloads/go-langserver-plugins/go-langserver/go-langserver-${version}-$OS.zip`,
};
export const CTAGS: LanguageServerDefinition = {
name: 'Ctags',
Expand Down
2 changes: 2 additions & 0 deletions x-pack/legacy/plugins/code/server/server_options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export class ServerOptions {

public readonly jdtConfigPath = resolve(this.config.get('path.data'), 'code/jdt_config');

public readonly goPath = resolve(this.config.get('path.data'), 'code/gopath');

public readonly updateFrequencyMs: number = this.options.updateFrequencyMs;

public readonly indexFrequencyMs: number = this.options.indexFrequencyMs;
Expand Down

0 comments on commit ed6eb0c

Please sign in to comment.