Skip to content

Commit b2a7d42

Browse files
authored
Handle untitled files from vscode which are of format: untitled:^Untitled-1 (#36240)
* Test for #36200 * Handle dynamic files by vscode Fixes #36200
1 parent 797c536 commit b2a7d42

File tree

5 files changed

+48
-5
lines changed

5 files changed

+48
-5
lines changed

src/server/editorServices.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1304,7 +1304,7 @@ namespace ts.server {
13041304
// Closing file should trigger re-reading the file content from disk. This is
13051305
// because the user may chose to discard the buffer content before saving
13061306
// to the disk, and the server's version of the file can be out of sync.
1307-
const fileExists = this.host.fileExists(info.fileName);
1307+
const fileExists = info.isDynamic ? false : this.host.fileExists(info.fileName);
13081308
info.close(fileExists);
13091309
this.stopWatchingConfigFilesForClosedScriptInfo(info);
13101310

src/server/scriptInfo.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ namespace ts.server {
272272

273273
/*@internal*/
274274
export function isDynamicFileName(fileName: NormalizedPath) {
275-
return fileName[0] === "^";
275+
return fileName[0] === "^" ||
276+
(stringContains(fileName, ":^") && !stringContains(fileName, directorySeparator));
276277
}
277278

278279
/*@internal*/

src/testRunner/unittests/tsserver/externalProjects.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ namespace ts.projectSystem {
221221

222222
checkNumberOfExternalProjects(projectService, 1);
223223
checkNumberOfInferredProjects(projectService, 0);
224+
verifyDynamic(projectService, "/^scriptdocument1 file1.ts");
224225

225226
externalFiles[0].content = "let x =1;";
226227
projectService.applyChangesInOpenFiles(arrayIterator(externalFiles));

src/testRunner/unittests/tsserver/projects.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,7 @@ namespace ts.projectSystem {
10941094
const project = projectService.inferredProjects[0];
10951095
checkProjectRootFiles(project, [file.path]);
10961096
checkProjectActualFiles(project, [file.path, libFile.path]);
1097+
verifyDynamic(projectService, `/${file.path}`);
10971098

10981099
assert.strictEqual(projectService.ensureDefaultProjectForFile(server.toNormalizedPath(file.path)), project);
10991100
const indexOfX = file.content.indexOf("x");
@@ -1124,6 +1125,7 @@ var x = 10;`
11241125
const host = createServerHost([libFile]);
11251126
const projectService = createProjectService(host);
11261127
projectService.openClientFile(file.path, file.content);
1128+
verifyDynamic(projectService, projectService.toPath(file.path));
11271129

11281130
projectService.checkNumberOfProjects({ inferredProjects: 1 });
11291131
const project = projectService.inferredProjects[0];
@@ -1152,6 +1154,7 @@ var x = 10;`
11521154
const projectService = session.getProjectService();
11531155
checkNumberOfProjects(projectService, { inferredProjects: 1 });
11541156
checkProjectActualFiles(projectService.inferredProjects[0], [file.path, libFile.path]);
1157+
verifyDynamic(projectService, `${tscWatch.projectRoot}/${file.path}`);
11551158

11561159
session.executeCommandSeq<protocol.OutliningSpansRequest>({
11571160
command: protocol.CommandTypes.GetOutliningSpans,

src/testRunner/unittests/tsserver/untitledFiles.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
namespace ts.projectSystem {
2+
export function verifyDynamic(service: server.ProjectService, path: string) {
3+
const info = Debug.assertDefined(service.filenameToScriptInfo.get(path), `Expected ${path} in :: ${JSON.stringify(arrayFrom(service.filenameToScriptInfo.entries(), ([key, f]) => ({ key, fileName: f.fileName, path: f.path })))}`);
4+
assert.isTrue(info.isDynamic);
5+
}
6+
27
describe("unittests:: tsserver:: Untitled files", () => {
8+
const untitledFile = "untitled:^Untitled-1";
39
it("Can convert positions to locations", () => {
410
const aTs: File = { path: "/proj/a.ts", content: "" };
511
const tsconfig: File = { path: "/proj/tsconfig.json", content: "{}" };
6-
const session = createSession(createServerHost([aTs, tsconfig]));
12+
const session = createSession(createServerHost([aTs, tsconfig]), { useInferredProjectPerProjectRoot: true });
713

814
openFilesForSession([aTs], session);
915

10-
const untitledFile = "untitled:^Untitled-1";
1116
executeSessionRequestNoResponse<protocol.OpenRequest>(session, protocol.CommandTypes.Open, {
1217
file: untitledFile,
1318
fileContent: `/// <reference path="../../../../../../typings/@epic/Core.d.ts" />\nlet foo = 1;\nfooo/**/`,
1419
scriptKindName: "TS",
1520
projectRootPath: "/proj",
1621
});
17-
22+
verifyDynamic(session.getProjectService(), `/proj/untitled:^untitled-1`);
1823
const response = executeSessionRequest<protocol.CodeFixRequest, protocol.CodeFixResponse>(session, protocol.CommandTypes.GetCodeFixes, {
1924
file: untitledFile,
2025
startLine: 3,
@@ -41,5 +46,38 @@ namespace ts.projectSystem {
4146
},
4247
]);
4348
});
49+
50+
it("opening untitled files", () => {
51+
const config: File = {
52+
path: `${tscWatch.projectRoot}/tsconfig.json`,
53+
content: "{}"
54+
};
55+
const host = createServerHost([config, libFile], { useCaseSensitiveFileNames: true, currentDirectory: tscWatch.projectRoot });
56+
const service = createProjectService(host);
57+
service.openClientFile(untitledFile, "const x = 10;", /*scriptKind*/ undefined, tscWatch.projectRoot);
58+
checkNumberOfProjects(service, { inferredProjects: 1 });
59+
checkProjectActualFiles(service.inferredProjects[0], [untitledFile, libFile.path]);
60+
verifyDynamic(service, `${tscWatch.projectRoot}/${untitledFile}`);
61+
62+
const untitled: File = {
63+
path: `${tscWatch.projectRoot}/Untitled-1.ts`,
64+
content: "const x = 10;"
65+
};
66+
host.writeFile(untitled.path, untitled.content);
67+
host.checkTimeoutQueueLength(0);
68+
service.openClientFile(untitled.path, untitled.content, /*scriptKind*/ undefined, tscWatch.projectRoot);
69+
checkNumberOfProjects(service, { configuredProjects: 1, inferredProjects: 1 });
70+
checkProjectActualFiles(service.configuredProjects.get(config.path)!, [untitled.path, libFile.path, config.path]);
71+
checkProjectActualFiles(service.inferredProjects[0], [untitledFile, libFile.path]);
72+
73+
service.closeClientFile(untitledFile);
74+
checkProjectActualFiles(service.configuredProjects.get(config.path)!, [untitled.path, libFile.path, config.path]);
75+
checkProjectActualFiles(service.inferredProjects[0], [untitledFile, libFile.path]);
76+
77+
service.openClientFile(untitledFile, "const x = 10;", /*scriptKind*/ undefined, tscWatch.projectRoot);
78+
verifyDynamic(service, `${tscWatch.projectRoot}/${untitledFile}`);
79+
checkProjectActualFiles(service.configuredProjects.get(config.path)!, [untitled.path, libFile.path, config.path]);
80+
checkProjectActualFiles(service.inferredProjects[0], [untitledFile, libFile.path]);
81+
});
4482
});
4583
}

0 commit comments

Comments
 (0)