Skip to content

Commit

Permalink
fix: add more logging for resolving user-specified tsserver (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
rchl committed Feb 27, 2022
1 parent 6f7ac94 commit 7139a32
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/lsp-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class LspServer {
}

private findTypescriptVersion(): TypeScriptVersion | null {
const typescriptVersionProvider = new TypeScriptVersionProvider(this.options);
const typescriptVersionProvider = new TypeScriptVersionProvider(this.options, this.logger);
// User-provided tsserver path.
const userSettingVersion = typescriptVersionProvider.getUserSettingVersion();
if (userSettingVersion) {
Expand Down
26 changes: 22 additions & 4 deletions src/utils/versionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import pkgUp from 'pkg-up';
import API from './api';
import { IServerOptions } from './configuration';
import { findPathToModule } from './modules-resolver';
import type { Logger } from '../logger';

export const enum TypeScriptVersionSource {
Bundled = 'bundled',
Expand All @@ -21,7 +22,8 @@ export class TypeScriptVersion {
constructor(
public readonly source: TypeScriptVersionSource,
public readonly path: string,
private readonly _pathLabel?: string
private readonly _pathLabel?: string,
private readonly logger?: Logger
) {
this._api = null;
}
Expand Down Expand Up @@ -56,12 +58,15 @@ export class TypeScriptVersion {
}

private getTypeScriptVersion(serverPath: string): API | null {
this.logger?.info(`Resolving TypeScript version from path "${serverPath}"...`);
if (!fs.existsSync(serverPath)) {
this.logger?.info('Server path does not exist on disk');
return null;
}

const p = serverPath.split(path.sep);
if (p.length <= 2) {
this.logger?.info('Server path is invalid (has less than two path components).');
return null;
}
const p2 = p.slice(0, -2);
Expand All @@ -74,63 +79,76 @@ export class TypeScriptVersion {
}
}
if (!fs.existsSync(fileName)) {
this.logger?.info(`Failed to find package.json at path "${fileName}"`);
return null;
}

this.logger?.info(`Reading version from package.json at "${fileName}"`);
const contents = fs.readFileSync(fileName).toString();
let desc: any = null;
try {
desc = JSON.parse(contents);
} catch (err) {
this.logger?.info('Failed parsing contents of package.json.');
return null;
}
if (!desc || !desc.version) {
this.logger?.info('Failed reading version number from package.json.');
return null;
}
return desc.version ? API.fromVersionString(desc.version) : null;
this.logger?.info(`Resolved TypeScript version to "${desc.version}"`);
return API.fromVersionString(desc.version);
}
}

export const MODULE_FOLDERS = ['node_modules/typescript/lib', '.vscode/pnpify/typescript/lib', '.yarn/sdks/typescript/lib'];

export class TypeScriptVersionProvider {
public constructor(private configuration?: IServerOptions) {}
public constructor(private configuration?: IServerOptions, private logger?: Logger) {}

public getUserSettingVersion(): TypeScriptVersion | null {
const { tsserverPath } = this.configuration || {};
if (!tsserverPath) {
return null;
}
this.logger?.info(`Resolving user-provided tsserver path "${tsserverPath}"...`);
let resolvedPath = tsserverPath;
// Resolve full path to the binary if path is not absolute.
if (!path.isAbsolute(resolvedPath)) {
const binaryPath = which.sync(tsserverPath, { nothrow:true });
if (binaryPath) {
resolvedPath = binaryPath;
}
this.logger?.info(`Non-absolute tsserver path resolved to "${binaryPath ? resolvedPath : '<failed>'}"`);
}
// Resolve symbolic link.
let stat = fs.lstatSync(resolvedPath, { throwIfNoEntry: false });
if (stat?.isSymbolicLink()) {
resolvedPath = fs.realpathSync(resolvedPath);
this.logger?.info(`Symbolic link tsserver path resolved to "${resolvedPath}"`);
}
// Get directory path
stat = fs.lstatSync(resolvedPath, { throwIfNoEntry: false });
if (stat?.isFile()) {
resolvedPath = path.dirname(resolvedPath);
this.logger?.info(`Resolved directory path from a file path: ${resolvedPath}`);
}
// Resolve path to the "lib" dir.
try {
const packageJsonPath = pkgUp.sync({ cwd: resolvedPath });
this.logger?.info(`Resolved package.json location: "${packageJsonPath}"`);
if (packageJsonPath) {
resolvedPath = path.join(path.dirname(packageJsonPath), 'lib');
this.logger?.info(`Assumed tsserver lib location: "${resolvedPath}"`);
}
} catch {
// ignore
}
return new TypeScriptVersion(
TypeScriptVersionSource.UserSetting,
resolvedPath
resolvedPath,
undefined,
this.logger
);
}

Expand Down

0 comments on commit 7139a32

Please sign in to comment.