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

fix: prevent completion using empty cache #2258

Merged
merged 2 commits into from
Jan 15, 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
11 changes: 8 additions & 3 deletions packages/language-server/src/plugins/typescript/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -719,11 +719,16 @@ async function createLanguageService(

dirty = false;

if (!oldProgram) {
// https://github.com/microsoft/TypeScript/blob/23faef92703556567ddbcb9afb893f4ba638fc20/src/server/project.ts#L1624
// host.getCachedExportInfoMap will create the cache if it doesn't exist
// so we need to check the property instead
const exportMapCache = project?.exportMapCache;
if (!oldProgram || !exportMapCache || exportMapCache.isEmpty()) {
changedFilesForExportCache.clear();
return;
}

exportMapCache.releaseSymbols();
for (const fileName of changedFilesForExportCache) {
const oldFile = oldProgram.getSourceFile(fileName);
const newFile = program?.getSourceFile(fileName);
Expand All @@ -734,10 +739,10 @@ async function createLanguageService(
}

if (oldFile && newFile) {
host.getCachedExportInfoMap?.().onFileChanged?.(oldFile, newFile, false);
exportMapCache.onFileChanged?.(oldFile, newFile, false);
} else {
// new file or deleted file
host.getCachedExportInfoMap?.().clear();
exportMapCache.clear();
}
}
changedFilesForExportCache.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1460,6 +1460,55 @@ describe('CompletionProviderImpl', function () {
assert.strictEqual(detail, 'Add import from "./Bar.svelte"\n\nclass Bar');
});

it("doesn't use empty cache", async () => {
const virtualTestDir = getRandomVirtualDirPath(testFilesDir);
const { document, lsAndTsDocResolver, lsConfigManager, docManager } =
setupVirtualEnvironment({
filename: 'index.svelte',
fileContent: '<script>b</script>',
testDir: virtualTestDir
});

const completionProvider = new CompletionsProviderImpl(lsAndTsDocResolver, lsConfigManager);

await lsAndTsDocResolver.getLSAndTSDoc(document);

docManager.updateDocument(document, [
{
range: Range.create(
Position.create(0, document.content.length),
Position.create(0, document.content.length)
),
text: ' '
}
]);

docManager.openClientDocument({
text: '',
uri: pathToUrl(join(virtualTestDir, 'Bar.svelte'))
});

docManager.updateDocument(document, [
{
range: Range.create(
Position.create(0, document.content.length),
Position.create(0, document.content.length)
),
text: ' '
}
]);

const completions = await completionProvider.getCompletions(document, {
line: 0,
character: 9
});

const item2 = completions?.items.find((item) => item.label === 'Bar');
const { detail } = await completionProvider.resolveCompletion(document, item2!);

assert.strictEqual(detail, 'Add import from "./Bar.svelte"\n\nclass Bar');
});

it('can auto import new export', async () => {
const virtualTestDir = getRandomVirtualDirPath(testFilesDir);
const { document, lsAndTsDocResolver, lsConfigManager, virtualSystem } =
Expand Down