Skip to content

Commit

Permalink
fix: respect user-provided tsserver.js path from --tsserver-path (#610
Browse files Browse the repository at this point in the history
)
  • Loading branch information
rchl committed Oct 14, 2022
1 parent 0120086 commit 417339f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ const program = new Command('typescript-language-server')
.version(version)
.requiredOption('--stdio', 'use stdio')
.option('--log-level <logLevel>', 'A number indicating the log level (4 = log, 3 = info, 2 = warn, 1 = error). Defaults to `2`.')
.option('--tsserver-log-verbosity <tsserverLogVerbosity>', 'Specify a tsserver log verbosity (terse, normal, verbose). Defaults to `normal`.' +
.option('--tsserver-log-verbosity <tsserverLogVerbosity>', '[deprecated] Specify a tsserver log verbosity (terse, normal, verbose). Defaults to `normal`.' +
' example: --tsserver-log-verbosity verbose')
.option('--tsserver-path <path>', 'Specify path to tsserver directory. example: --tsserver-path=/Users/me/typescript/lib/')
.option('--tsserver-path <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();
Expand Down
27 changes: 12 additions & 15 deletions src/tsServer/versionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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}`);
}
Expand All @@ -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;
}
Expand All @@ -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');
Expand Down

0 comments on commit 417339f

Please sign in to comment.