From 417339fa66bc1910c80888c3f909e3d059da8ee5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Ch=C5=82odnicki?= Date: Fri, 14 Oct 2022 21:47:21 +0200 Subject: [PATCH] fix: respect user-provided tsserver.js path from `--tsserver-path` (#610) --- README.md | 3 ++- src/cli.ts | 4 ++-- src/tsServer/versionProvider.ts | 27 ++++++++++++--------------- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index e7933d87..c789f87d 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ Maintained by a [community of contributors](https://github.com/typescript-langua - [Apply Refactoring](#apply-refactoring) - [Organize Imports](#organize-imports) - [Rename File](#rename-file) + - [Configure plugin](#configure-plugin) - [Inlay hints \(`typescript/inlayHints`\) \(experimental\)](#inlay-hints-typescriptinlayhints-experimental) - [Callers and callees \(`textDocument/calls`\) \(experimental\)](#callers-and-callees-textdocumentcalls-experimental) - [Supported Protocol features](#supported-protocol-features) @@ -66,7 +67,7 @@ typescript-language-server --stdio -h, --help output usage information ``` -> Note: The path passed to `--tsserver-path` should ideally be a path to the `/.../typescript/lib/` directory and not to the shell script `/.../node_modules/.bin/tsserver` or `tsserver`. Though for backward-compatibility reasons, the server will try to do the right thing even when passed a path to the shell script. +> Note: The path passed to `--tsserver-path` should be a path to the `[...]/typescript/lib/tssserver.js` file or to the `[...]/typescript/lib/` directory and not to the shell script `[...]/node_modules/.bin/tsserver`. Though for backward-compatibility reasons, the server will try to do the right thing even when passed a path to the shell script. ## initializationOptions diff --git a/src/cli.ts b/src/cli.ts index b549aca7..4eff71e3 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -19,9 +19,9 @@ const program = new Command('typescript-language-server') .version(version) .requiredOption('--stdio', 'use stdio') .option('--log-level ', 'A number indicating the log level (4 = log, 3 = info, 2 = warn, 1 = error). Defaults to `2`.') - .option('--tsserver-log-verbosity ', 'Specify a tsserver log verbosity (terse, normal, verbose). Defaults to `normal`.' + + .option('--tsserver-log-verbosity ', '[deprecated] Specify a tsserver log verbosity (terse, normal, verbose). Defaults to `normal`.' + ' example: --tsserver-log-verbosity verbose') - .option('--tsserver-path ', 'Specify path to tsserver directory. example: --tsserver-path=/Users/me/typescript/lib/') + .option('--tsserver-path ', '[deprecated] Specify path to tsserver.js or the lib directory. example: --tsserver-path=/Users/me/typescript/lib/tsserver.js') .parse(process.argv); const options = program.opts(); diff --git a/src/tsServer/versionProvider.ts b/src/tsServer/versionProvider.ts index 48b617d5..5f290cf0 100644 --- a/src/tsServer/versionProvider.ts +++ b/src/tsServer/versionProvider.ts @@ -31,21 +31,12 @@ export class TypeScriptVersion { public readonly source: TypeScriptVersionSource, public readonly path: string, private readonly logger: Logger, - private readonly _pathLabel?: string, ) { this._api = null; } - public get tscPath(): string { - return path.resolve(this.path, '../bin/tsc'); - } - public get tsServerPath(): string { - return path.resolve(this.path, 'tsserver.js'); - } - - public get pathLabel(): string { - return typeof this._pathLabel === 'undefined' ? this.path : this._pathLabel; + return this.path; } public get isValid(): boolean { @@ -138,6 +129,10 @@ export class TypeScriptVersionProvider { // Get directory path stat = fs.lstatSync(resolvedPath, { throwIfNoEntry: false }); if (stat?.isFile()) { + if (path.basename(resolvedPath) === 'tsserver.js') { + this.logger.log(`Resolved tsserver location: ${resolvedPath}`); + return new TypeScriptVersion(TypeScriptVersionSource.UserSetting, resolvedPath, this.logger); + } resolvedPath = path.dirname(resolvedPath); this.logger.log(`Resolved directory path from a file path: ${resolvedPath}`); } @@ -146,20 +141,21 @@ export class TypeScriptVersionProvider { const packageJsonPath = pkgUpSync({ cwd: resolvedPath }); this.logger.log(`Resolved package.json location: "${packageJsonPath}"`); if (packageJsonPath) { - resolvedPath = path.join(path.dirname(packageJsonPath), 'lib'); - this.logger.log(`Assumed tsserver lib location: "${resolvedPath}"`); + resolvedPath = path.join(path.dirname(packageJsonPath), 'lib', 'tsserver.js'); + this.logger.log(`Resolved tsserver location: "${resolvedPath}"`); } } catch { // ignore } - return new TypeScriptVersion(TypeScriptVersionSource.UserSetting, resolvedPath, this.logger, undefined); + return new TypeScriptVersion(TypeScriptVersionSource.UserSetting, resolvedPath, this.logger); } public getWorkspaceVersion(workspaceFolders: string[]): TypeScriptVersion | null { for (const p of workspaceFolders) { const libFolder = findPathToModule(p, MODULE_FOLDERS); if (libFolder) { - const version = new TypeScriptVersion(TypeScriptVersionSource.Workspace, libFolder, this.logger); + const tsServerPath = path.join(libFolder, 'tsserver.js'); + const version = new TypeScriptVersion(TypeScriptVersionSource.Workspace, tsServerPath, this.logger); if (version.isValid) { return version; } @@ -172,7 +168,8 @@ export class TypeScriptVersionProvider { const require = createRequire(import.meta.url); try { const file = require.resolve('typescript'); - const bundledVersion = new TypeScriptVersion(TypeScriptVersionSource.Bundled, path.dirname(file), this.logger, ''); + const tsServerPath = path.join(path.dirname(file), 'tsserver.js'); + const bundledVersion = new TypeScriptVersion(TypeScriptVersionSource.Bundled, tsServerPath, this.logger); return bundledVersion; } catch (e) { // window.showMessage('Bundled typescript module not found', 'error');