Skip to content
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

stop AutoInstallerFs from thrashing forevermore and fix typings installer #225648

Merged
merged 1 commit into from
Aug 16, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { PackageManager } from '@vscode/ts-package-manager';
import { basename, join } from 'path';
import * as vscode from 'vscode';
import { URI } from 'vscode-uri';
import { Throttler } from '../utils/async';
import { Disposable } from '../utils/dispose';
import { MemFs } from './memFs';
import { Logger } from '../logging/logger';
Expand All @@ -19,9 +18,7 @@ export class AutoInstallerFs extends Disposable implements vscode.FileSystemProv

private readonly memfs: MemFs;
private readonly packageManager: PackageManager;
private readonly _projectCache = new Map</* root */ string, {
readonly throttler: Throttler;
}>();
private readonly _projectCache = new Map</* root */ string, Promise<void> | undefined>();

private readonly _emitter = this._register(new vscode.EventEmitter<vscode.FileChangeEvent[]>());
readonly onDidChangeFile = this._emitter.event;
Expand Down Expand Up @@ -78,9 +75,8 @@ export class AutoInstallerFs extends Disposable implements vscode.FileSystemProv
}

watch(resource: vscode.Uri): vscode.Disposable {
const mapped = URI.file(new MappedUri(resource).path);
this.logger.trace(`AutoInstallerFs.watch. Original: ${resource.toString()}, Mapped: ${mapped.toString()}`);
return this.memfs.watch(mapped);
this.logger.trace(`AutoInstallerFs.watch. Resource: ${resource.toString()}}`);
return this.memfs.watch(resource);
}

async stat(uri: vscode.Uri): Promise<vscode.FileStat> {
Expand Down Expand Up @@ -151,36 +147,37 @@ export class AutoInstallerFs extends Disposable implements vscode.FileSystemProv
throw vscode.FileSystemError.FileNotFound();
}

const root = this.getProjectRoot(incomingUri.path);
const root = await this.getProjectRoot(incomingUri.original);
if (!root) {
return;
}

this.logger.trace(`AutoInstallerFs.ensurePackageContents. Path: ${incomingUri.path}, Root: ${root}`);

let projectEntry = this._projectCache.get(root);
if (!projectEntry) {
projectEntry = { throttler: new Throttler() };
this._projectCache.set(root, projectEntry);
const existingInstall = this._projectCache.get(root);
if (existingInstall) {
this.logger.trace(`AutoInstallerFs.ensurePackageContents. Found ongoing install for: ${root}/node_modules`);
return existingInstall;
}

projectEntry.throttler.queue(async () => {
const installing = (async () => {
const proj = await this.packageManager.resolveProject(root, await this.getInstallOpts(incomingUri.original, root));
try {
await proj.restore();
} catch (e) {
console.error(`failed to restore package at ${incomingUri.path}: `, e);
throw e;
}
});
})();
this._projectCache.set(root, installing);
await installing;
}

private async getInstallOpts(originalUri: URI, root: string) {
const vsfs = vscode.workspace.fs;
let pkgJson;
try {
pkgJson = TEXT_DECODER.decode(await vsfs.readFile(originalUri.with({ path: join(root, 'package.json') })));
} catch (e) { }

// We definitely need a package.json to be there.
const pkgJson = TEXT_DECODER.decode(await vsfs.readFile(originalUri.with({ path: join(root, 'package.json') })));

let kdlLock;
try {
Expand All @@ -199,9 +196,19 @@ export class AutoInstallerFs extends Disposable implements vscode.FileSystemProv
};
}

private getProjectRoot(path: string): string | undefined {
const pkgPath = path.match(/(^.*)\/node_modules/);
return pkgPath?.[1];
private async getProjectRoot(incomingUri: URI): Promise<string | undefined> {
const vsfs = vscode.workspace.fs;
const pkgPath = incomingUri.path.match(/^(.*?)\/node_modules/);
const ret = pkgPath?.[1];
if (!ret) {
return;
}
try {
await vsfs.stat(incomingUri.with({ path: join(ret, 'package.json') }));
return ret;
} catch (e) {
return;
}
}
}

Expand All @@ -214,7 +221,7 @@ class MappedUri {

const parts = uri.path.match(/^\/([^\/]+)\/([^\/]*)(?:\/(.+))?$/);
if (!parts) {
throw new Error(`Invalid path: ${uri.path}`);
throw new Error(`Invalid uri: ${uri.toString()}, ${uri.path}`);
}

const scheme = parts[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ export class FileWatcherManager {
this.watchFiles.set(path, { callback, pollingInterval, options });
const watchIds = [++this.watchId];
this.watchPort.postMessage({ type: 'watchFile', uri: uri, id: watchIds[0] });
if (this.enabledExperimentalTypeAcquisition && looksLikeNodeModules(path)) {
if (this.enabledExperimentalTypeAcquisition && looksLikeNodeModules(path) && uri.scheme !== 'vscode-global-typings') {
watchIds.push(++this.watchId);
this.watchPort.postMessage({ type: 'watchFile', uri: mapUri(uri, 'vscode-node-modules'), id: watchIds[1] });
this.watchPort.postMessage({ type: 'watchFile', uri: mapUri(uri, 'vscode-global-typings'), id: watchIds[1] });
}
return {
close: () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ export class WebTypingsInstallerClient implements ts.server.ITypingsInstaller {
break;
case 'event::beginInstallTypes':
case 'event::endInstallTypes':
// TODO(@zkat): maybe do something with this?
case 'action::watchTypingLocations':
// Don't care.
break;
default:
throw new Error(`unexpected response: ${response}`);
throw new Error(`unexpected response: ${JSON.stringify(response)}`);
}
}

Expand Down
Loading