Skip to content

Add tests for completions crash #53472

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

Merged
merged 4 commits into from
Mar 23, 2023
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
90 changes: 90 additions & 0 deletions src/testRunner/unittests/tsserver/exportMapCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
File,
} from "../virtualFileSystemWithWatch";
import {
baselineTsserverLogs,
configuredProjectAt,
createLoggerWithInMemoryLogs,
createSession,
openFilesForSession,
} from "./helpers";
Expand Down Expand Up @@ -129,6 +131,94 @@ describe("unittests:: tsserver:: exportMapCache", () => {
assert.ok(sigintPropAfter);
assert.notEqual(symbolIdBefore, ts.getSymbolId(sigintPropAfter![0].symbol));
});

it("invalidates the cache when a file is opened with different contents", () => {
const utilsTs: File = {
path: "/utils.ts",
content: `export class Element {
// ...
}

export abstract class Component {
abstract render(): Element;
}`
};
const classesTs: File = {
path: "/classes.ts",
content: `import { Component } from "./utils.js";

export class MyComponent extends Component {
render/**/
}`
};
const host = createServerHost([utilsTs, classesTs, tsconfig]);
const session = createSession(host, { canUseEvents: true, logger: createLoggerWithInMemoryLogs(host) });
const projectService = session.getProjectService();
openFilesForSession([classesTs], session);
session.executeCommandSeq<ts.server.protocol.ConfigureRequest>({
command: ts.server.protocol.CommandTypes.Configure,
arguments: {
preferences: {
includeCompletionsForModuleExports: true,
includeCompletionsWithClassMemberSnippets: true,
includeCompletionsWithInsertText: true,
},
}
});
session.executeCommandSeq<ts.server.protocol.CompletionsRequest>({
command: ts.server.protocol.CommandTypes.CompletionInfo,
arguments: {
file: classesTs.path,
line: 4,
offset: 23,
prefix: "render",
includeExternalModuleExports: true,
includeInsertTextCompletions: true,
}
});

const project = configuredProjectAt(projectService, 0);
const exportMapCache = project.getCachedExportInfoMap();
assert.ok(exportMapCache.isUsableByFile(classesTs.path as ts.Path));
assert.ok(!exportMapCache.isEmpty());

openFilesForSession([{ file: utilsTs.path, content: utilsTs.content.replace("render", "render2") }], session);
session.executeCommandSeq<ts.server.protocol.UpdateOpenRequest>({
command: ts.server.protocol.CommandTypes.UpdateOpen,
arguments: {
changedFiles: [{
fileName: classesTs.path,
textChanges: [{
newText: "",
start: { line: 4, offset: 22 },
end: { line: 4, offset: 23 },
}]
}]
}
});

host.runQueuedTimeoutCallbacks();
project.getPackageJsonAutoImportProvider();

// Cache is invalidated because other file has changed
assert.ok(!exportMapCache.isUsableByFile(classesTs.path as ts.Path));
assert.ok(exportMapCache.isEmpty());

// Does not crash
session.executeCommandSeq<ts.server.protocol.CompletionsRequest>({
command: ts.server.protocol.CommandTypes.CompletionInfo,
arguments: {
file: classesTs.path,
line: 4,
offset: 22,
prefix: "rende",
includeExternalModuleExports: true,
includeInsertTextCompletions: true,
}
});

baselineTsserverLogs("exportMapCache", "invalidates the cache when a file is opened with different contents", session);
});
});

function setup() {
Expand Down
Loading