Skip to content

Defer the initializations in TI which results in many fileExistancy checks for existing packages and reading type registry to at use time #52817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 27 additions & 21 deletions src/typingsInstaller/nodeTypingsInstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ type ExecSync = (command: string, options: ExecSyncOptions) => string;
export class NodeTypingsInstaller extends TypingsInstaller {
private readonly nodeExecSync: ExecSync;
private readonly npmPath: string;
readonly typesRegistry: Map<string, MapLike<string>>;
typesRegistry: Map<string, MapLike<string>> = undefined!;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is only referenced once; can we just make it potentially undefined and then assertDefined at its one usage, rather than adding undefined!?


private delayedInitializationError: InitializationFailedResponse | undefined;

Expand All @@ -131,31 +131,36 @@ export class NodeTypingsInstaller extends TypingsInstaller {
this.log.writeLine(`validateDefaultNpmLocation: ${validateDefaultNpmLocation}`);
}
({ execSync: this.nodeExecSync } = require("child_process"));
}

this.ensurePackageDirectoryExists(globalTypingsCacheLocation);
override ensureInitialized(): void {
if (!this.initDone) {
super.ensureInitialized();

try {
if (this.log.isEnabled()) {
this.log.writeLine(`Updating ${typesRegistryPackageName} npm package...`);
}
this.execSyncAndLog(`${this.npmPath} install --ignore-scripts ${typesRegistryPackageName}@${this.latestDistTag}`, { cwd: globalTypingsCacheLocation });
if (this.log.isEnabled()) {
this.log.writeLine(`Updated ${typesRegistryPackageName} npm package`);
this.ensurePackageDirectoryExists(this.globalCachePath);
try {
if (this.log.isEnabled()) {
this.log.writeLine(`Updating ${typesRegistryPackageName} npm package...`);
}
this.execSyncAndLog(`${this.npmPath} install --ignore-scripts ${typesRegistryPackageName}@${this.latestDistTag}`, { cwd: this.globalCachePath });
if (this.log.isEnabled()) {
this.log.writeLine(`Updated ${typesRegistryPackageName} npm package`);
}
}
}
catch (e) {
if (this.log.isEnabled()) {
this.log.writeLine(`Error updating ${typesRegistryPackageName} package: ${(e as Error).message}`);
catch (e) {
if (this.log.isEnabled()) {
this.log.writeLine(`Error updating ${typesRegistryPackageName} package: ${(e as Error).message}`);
}
// store error info to report it later when it is known that server is already listening to events from typings installer
this.delayedInitializationError = {
kind: "event::initializationFailed",
message: (e as Error).message,
stack: (e as Error).stack,
};
}
// store error info to report it later when it is known that server is already listening to events from typings installer
this.delayedInitializationError = {
kind: "event::initializationFailed",
message: (e as Error).message,
stack: (e as Error).stack,
};
}

this.typesRegistry = loadTypesRegistryFile(getTypesRegistryFileLocation(globalTypingsCacheLocation), this.installTypingHost, this.log);
this.typesRegistry = loadTypesRegistryFile(getTypesRegistryFileLocation(this.globalCachePath), this.installTypingHost, this.log);
}
}

listen() {
Expand All @@ -173,6 +178,7 @@ export class NodeTypingsInstaller extends TypingsInstaller {
this.closeProject(req);
break;
case "typesRegistry": {
this.ensureInitialized();
const typesRegistry: { [key: string]: MapLike<string> } = {};
this.typesRegistry.forEach((value, key) => {
typesRegistry[key] = value;
Expand Down
12 changes: 10 additions & 2 deletions src/typingsInstallerCore/typingsInstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,11 @@ export abstract class TypingsInstaller {
abstract readonly typesRegistry: Map<string, MapLike<string>>;
/** @internal */
private readonly watchFactory: WatchFactory<string, ProjectWatchers>;
protected initDone?: true;

constructor(
protected readonly installTypingHost: InstallTypingHost,
private readonly globalCachePath: string,
protected readonly globalCachePath: string,
private readonly safeListPath: Path,
private readonly typesMapLocation: Path,
private readonly throttleLimit: number,
Expand All @@ -171,7 +172,13 @@ export abstract class TypingsInstaller {
this.log.writeLine(`Global cache location '${globalCachePath}', safe file path '${safeListPath}', types map path ${typesMapLocation}`);
}
this.watchFactory = getWatchFactory(this.installTypingHost as WatchFactoryHost, isLoggingEnabled ? WatchLogLevel.Verbose : WatchLogLevel.None, s => this.log.writeLine(s), getDetailWatchInfo);
this.processCacheLocation(this.globalCachePath);
}

ensureInitialized() {
if (!this.initDone) {
this.processCacheLocation(this.globalCachePath);
this.initDone = true;
}
}

closeProject(req: CloseProject) {
Expand Down Expand Up @@ -202,6 +209,7 @@ export abstract class TypingsInstaller {
this.log.writeLine(`Got install request ${JSON.stringify(req)}`);
}

this.ensureInitialized();
// load existing typing information from the cache
if (req.cachePath) {
if (this.log.isEnabled()) {
Expand Down