Skip to content

Commit 3a02b99

Browse files
authored
Try to request workspace content caching on commit (microsoft#185920)
For microsoft#175972 Here's what believe is happening: - Workspace opens. We download the archive for the current commit - User makes a commit - The commit has now changed, which invalidates the archive. However TS doesn't know this (all the uris seem to stay the same). This means that we silently start going to the network again on file system operations To fix this, we're going to try re-requesting the archive when a file system change event is fired for the workspace root. This should happen when a commit is made
1 parent 578ffcf commit 3a02b99

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

extensions/typescript-language-features/src/extension.browser.ts

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { Logger } from './logging/logger';
2323
import { getPackageInfo } from './utils/packageInfo';
2424
import { isWebAndHasSharedArrayBuffers } from './utils/platform';
2525
import { PluginManager } from './tsServer/plugins';
26+
import { Disposable } from './utils/dispose';
2627

2728
class StaticVersionProvider implements ITypeScriptVersionProvider {
2829

@@ -96,13 +97,13 @@ export async function activate(context: vscode.ExtensionContext): Promise<Api> {
9697
});
9798

9899
context.subscriptions.push(lazilyActivateClient(lazyClientHost, pluginManager, activeJsTsEditorTracker, async () => {
99-
await preload(logger);
100+
await startPreloadWorkspaceContentsIfNeeded(context, logger);
100101
}));
101102

102103
return getExtensionApi(onCompletionAccepted.event, pluginManager);
103104
}
104105

105-
async function preload(logger: Logger): Promise<void> {
106+
async function startPreloadWorkspaceContentsIfNeeded(context: vscode.ExtensionContext, logger: Logger): Promise<void> {
106107
if (!isWebAndHasSharedArrayBuffers()) {
107108
return;
108109
}
@@ -113,15 +114,46 @@ async function preload(logger: Logger): Promise<void> {
113114
return;
114115
}
115116

116-
try {
117-
const remoteHubApi = await RemoteRepositories.getApi();
118-
if (await remoteHubApi.loadWorkspaceContents?.(workspaceUri)) {
119-
logger.info(`Successfully loaded workspace content for repository ${workspaceUri.toString()}`);
120-
} else {
121-
logger.info(`Failed to load workspace content for repository ${workspaceUri.toString()}`);
117+
const loader = new RemoteWorkspaceContentsPreloader(workspaceUri, logger);
118+
context.subscriptions.push(loader);
119+
return loader.triggerPreload();
120+
}
121+
122+
class RemoteWorkspaceContentsPreloader extends Disposable {
123+
124+
private _preload: Promise<void> | undefined;
125+
126+
constructor(
127+
private readonly workspaceUri: vscode.Uri,
128+
private readonly logger: Logger,
129+
) {
130+
super();
131+
132+
const fsWatcher = this._register(vscode.workspace.createFileSystemWatcher(new vscode.RelativePattern(workspaceUri, '*')));
133+
this._register(fsWatcher.onDidChange(uri => {
134+
if (uri.toString() === workspaceUri.toString()) {
135+
this._preload = undefined;
136+
this.triggerPreload();
137+
}
138+
}));
139+
}
140+
141+
async triggerPreload() {
142+
this._preload ??= this.doPreload();
143+
return this._preload;
144+
}
145+
146+
private async doPreload(): Promise<void> {
147+
try {
148+
const remoteHubApi = await RemoteRepositories.getApi();
149+
if (await remoteHubApi.loadWorkspaceContents?.(this.workspaceUri)) {
150+
this.logger.info(`Successfully loaded workspace content for repository ${this.workspaceUri.toString()}`);
151+
} else {
152+
this.logger.info(`Failed to load workspace content for repository ${this.workspaceUri.toString()}`);
153+
}
154+
} catch (error) {
155+
this.logger.info(`Loading workspace content for repository ${this.workspaceUri.toString()} failed: ${error instanceof Error ? error.toString() : 'Unknown reason'}`);
156+
console.error(error);
122157
}
123-
} catch (error) {
124-
logger.info(`Loading workspace content for repository ${workspaceUri.toString()} failed: ${error instanceof Error ? error.toString() : 'Unknown reason'}`);
125-
console.error(error);
126158
}
127159
}

0 commit comments

Comments
 (0)