Skip to content

Commit 24e58c3

Browse files
authored
Merge pull request #23841 from Microsoft/sourceFileChangeAssert
Fix the assert for orphan script info source change event
2 parents 724e656 + a196f16 commit 24e58c3

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3181,7 +3181,7 @@ namespace ts.projectSystem {
31813181
const projectLocation = "/user/username/projects/project";
31823182
const file1: FileOrFolder = {
31833183
path: `${projectLocation}/src/file1.ts`,
3184-
content: `import { y } from "./file1"; let x = 10;`
3184+
content: `import { y } from "./file2"; let x = 10;`
31853185
};
31863186
const file2: FileOrFolder = {
31873187
path: `${projectLocation}/src/file2.ts`,
@@ -3259,6 +3259,53 @@ namespace ts.projectSystem {
32593259
// Allow allowNonTsExtensions will be set to true for deferred extensions.
32603260
assert.isTrue(configuredProject.getCompilerOptions().allowNonTsExtensions);
32613261
});
3262+
3263+
it("Orphan source files are handled correctly on watch trigger", () => {
3264+
const projectLocation = "/user/username/projects/project";
3265+
const file1: FileOrFolder = {
3266+
path: `${projectLocation}/src/file1.ts`,
3267+
content: `export let x = 10;`
3268+
};
3269+
const file2: FileOrFolder = {
3270+
path: `${projectLocation}/src/file2.ts`,
3271+
content: "export let y = 10;"
3272+
};
3273+
const configContent1 = JSON.stringify({
3274+
files: ["src/file1.ts", "src/file2.ts"]
3275+
});
3276+
const config: FileOrFolder = {
3277+
path: `${projectLocation}/tsconfig.json`,
3278+
content: configContent1
3279+
};
3280+
const files = [file1, file2, libFile, config];
3281+
const host = createServerHost(files);
3282+
const service = createProjectService(host);
3283+
service.openClientFile(file1.path);
3284+
checkProjectActualFiles(service.configuredProjects.get(config.path), [file1.path, file2.path, libFile.path, config.path]);
3285+
3286+
const configContent2 = JSON.stringify({
3287+
files: ["src/file1.ts"]
3288+
});
3289+
config.content = configContent2;
3290+
host.reloadFS(files);
3291+
host.runQueuedTimeoutCallbacks();
3292+
3293+
checkProjectActualFiles(service.configuredProjects.get(config.path), [file1.path, libFile.path, config.path]);
3294+
verifyFile2InfoIsOrphan();
3295+
3296+
file2.content += "export let z = 10;";
3297+
host.reloadFS(files);
3298+
host.runQueuedTimeoutCallbacks();
3299+
3300+
checkProjectActualFiles(service.configuredProjects.get(config.path), [file1.path, libFile.path, config.path]);
3301+
verifyFile2InfoIsOrphan();
3302+
3303+
function verifyFile2InfoIsOrphan() {
3304+
const info = service.getScriptInfoForPath(file2.path as Path);
3305+
assert.isDefined(info);
3306+
assert.equal(info.containingProjects.length, 0);
3307+
}
3308+
});
32623309
});
32633310

32643311
describe("tsserverProjectSystem Proper errors", () => {

src/server/editorServices.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -594,10 +594,12 @@ namespace ts.server {
594594
}
595595

596596
private delayUpdateProjectGraphs(projects: ReadonlyArray<Project>) {
597-
for (const project of projects) {
598-
this.delayUpdateProjectGraph(project);
597+
if (projects.length) {
598+
for (const project of projects) {
599+
this.delayUpdateProjectGraph(project);
600+
}
601+
this.delayEnsureProjectForOpenFiles();
599602
}
600-
this.delayEnsureProjectForOpenFiles();
601603
}
602604

603605
setCompilerOptionsForInferredProjects(projectCompilerOptions: protocol.ExternalProjectCompilerOptions, projectRootPath?: string): void {
@@ -708,7 +710,6 @@ namespace ts.server {
708710
this.handleDeletedFile(info);
709711
}
710712
else if (!info.isScriptOpen()) {
711-
Debug.assert(info.containingProjects.length !== 0);
712713
// file has been changed which might affect the set of referenced files in projects that include
713714
// this file and set of inferred projects
714715
info.delayReloadNonMixedContentFile();

0 commit comments

Comments
 (0)