Skip to content

Commit b9a5bbc

Browse files
authored
Syntax operations also need to ensure project is present for the open script infos since update could be pending to make sure open script info has project (#50418)
Also convert one relevant test case to baseline Fixes #50131
1 parent 1d4fbbb commit b9a5bbc

File tree

4 files changed

+393
-16
lines changed

4 files changed

+393
-16
lines changed

src/server/session.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1868,9 +1868,9 @@ namespace ts.server {
18681868

18691869
private getFileAndLanguageServiceForSyntacticOperation(args: protocol.FileRequestArgs) {
18701870
// Since this is syntactic operation, there should always be project for the file
1871-
// we wouldnt have to ensure project but rather throw if we dont get project
1871+
// throw if we dont get project
18721872
const file = toNormalizedPath(args.file);
1873-
const project = this.getProject(args.projectFileName) || this.projectService.tryGetDefaultProjectForFile(file);
1873+
const project = this.getProject(args.projectFileName) || this.projectService.ensureDefaultProjectForFile(file);
18741874
if (!project) {
18751875
return Errors.ThrowNoProject();
18761876
}

src/testRunner/unittests/tsserver/projects.ts

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,7 @@ namespace ts.projectSystem {
10541054
assert.equal(options.outDir, "C:/a/b", "");
10551055
});
10561056

1057-
it("files opened, closed affecting multiple projects", () => {
1057+
it("files opened and closed affecting multiple projects", () => {
10581058
const file: File = {
10591059
path: "/a/b/projects/config/file.ts",
10601060
content: `import {a} from "../files/file1"; export let b = a;`
@@ -1074,7 +1074,7 @@ namespace ts.projectSystem {
10741074

10751075
const files = [config, file, filesFile1, filesFile2, libFile];
10761076
const host = createServerHost(files);
1077-
const session = createSession(host);
1077+
const session = createSession(host, { logger: createLoggerWithInMemoryLogs() });
10781078
// Create configured project
10791079
session.executeCommandSeq<protocol.OpenRequest>({
10801080
command: protocol.CommandTypes.Open,
@@ -1083,18 +1083,13 @@ namespace ts.projectSystem {
10831083
}
10841084
});
10851085

1086-
const projectService = session.getProjectService();
1087-
const configuredProject = projectService.configuredProjects.get(config.path)!;
1088-
verifyConfiguredProject();
1089-
10901086
// open files/file1 = should not create another project
10911087
session.executeCommandSeq<protocol.OpenRequest>({
10921088
command: protocol.CommandTypes.Open,
10931089
arguments: {
10941090
file: filesFile1.path
10951091
}
10961092
});
1097-
verifyConfiguredProject();
10981093

10991094
// Close the file = should still have project
11001095
session.executeCommandSeq<protocol.CloseRequest>({
@@ -1103,7 +1098,6 @@ namespace ts.projectSystem {
11031098
file: file.path
11041099
}
11051100
});
1106-
verifyConfiguredProject();
11071101

11081102
// Open files/file2 - should create inferred project and close configured project
11091103
session.executeCommandSeq<protocol.OpenRequest>({
@@ -1112,8 +1106,6 @@ namespace ts.projectSystem {
11121106
file: filesFile2.path
11131107
}
11141108
});
1115-
checkNumberOfProjects(projectService, { inferredProjects: 1 });
1116-
checkProjectActualFiles(projectService.inferredProjects[0], [libFile.path, filesFile2.path]);
11171109

11181110
// Actions on file1 would result in assert
11191111
session.executeCommandSeq<protocol.OccurrencesRequest>({
@@ -1125,10 +1117,7 @@ namespace ts.projectSystem {
11251117
}
11261118
});
11271119

1128-
function verifyConfiguredProject() {
1129-
checkNumberOfProjects(projectService, { configuredProjects: 1 });
1130-
checkProjectActualFiles(configuredProject, [file.path, filesFile1.path, libFile.path, config.path]);
1131-
}
1120+
baselineTsserverLogs("projects", "files opened and closed affecting multiple projects", session);
11321121
});
11331122

11341123
it("requests are done on file on pendingReload but has svc for previous version", () => {
@@ -1620,5 +1609,40 @@ namespace ts.projectSystem {
16201609

16211610
checkNumberOfInferredProjects(projectService, 0);
16221611
});
1612+
1613+
it("file opened is in configured project that will be removed", () => {
1614+
const testsConfig: File = {
1615+
path: `${tscWatch.projectRoot}/playground/tsconfig.json`,
1616+
content: "{}"
1617+
};
1618+
const testsFile: File = {
1619+
path: `${tscWatch.projectRoot}/playground/tests.ts`,
1620+
content: `export function foo() {}`
1621+
};
1622+
const innerFile: File = {
1623+
path: `${tscWatch.projectRoot}/playground/tsconfig-json/tests/spec.ts`,
1624+
content: `export function bar() { }`
1625+
};
1626+
const innerConfig: File = {
1627+
path: `${tscWatch.projectRoot}/playground/tsconfig-json/tsconfig.json`,
1628+
content: JSON.stringify({
1629+
include: ["./src"]
1630+
})
1631+
};
1632+
const innerSrcFile: File = {
1633+
path: `${tscWatch.projectRoot}/playground/tsconfig-json/src/src.ts`,
1634+
content: `export function foobar() { }`
1635+
};
1636+
const host = createServerHost([testsConfig, testsFile, innerFile, innerConfig, innerSrcFile, libFile]);
1637+
const session = createSession(host, { logger: createLoggerWithInMemoryLogs() });
1638+
openFilesForSession([testsFile], session);
1639+
closeFilesForSession([testsFile], session);
1640+
openFilesForSession([innerFile], session);
1641+
session.executeCommandSeq<protocol.OutliningSpansRequest>({
1642+
command: protocol.CommandTypes.GetOutliningSpans,
1643+
arguments: { file: innerFile.path }
1644+
});
1645+
baselineTsserverLogs("projects", "file opened is in configured project that will be removed", session);
1646+
});
16231647
});
16241648
}

0 commit comments

Comments
 (0)