Handle untitled files from vscode which are of format: untitled:^Untitled-1 (#36240)

* Test for #36200

* Handle dynamic files by vscode
Fixes #36200
This commit is contained in:
Sheetal Nandi
2020-01-16 15:38:55 -08:00
committed by GitHub
parent 797c5362a2
commit b2a7d42032
5 changed files with 48 additions and 5 deletions

View File

@@ -1304,7 +1304,7 @@ namespace ts.server {
// Closing file should trigger re-reading the file content from disk. This is
// because the user may chose to discard the buffer content before saving
// to the disk, and the server's version of the file can be out of sync.
const fileExists = this.host.fileExists(info.fileName);
const fileExists = info.isDynamic ? false : this.host.fileExists(info.fileName);
info.close(fileExists);
this.stopWatchingConfigFilesForClosedScriptInfo(info);

View File

@@ -272,7 +272,8 @@ namespace ts.server {
/*@internal*/
export function isDynamicFileName(fileName: NormalizedPath) {
return fileName[0] === "^";
return fileName[0] === "^" ||
(stringContains(fileName, ":^") && !stringContains(fileName, directorySeparator));
}
/*@internal*/

View File

@@ -221,6 +221,7 @@ namespace ts.projectSystem {
checkNumberOfExternalProjects(projectService, 1);
checkNumberOfInferredProjects(projectService, 0);
verifyDynamic(projectService, "/^scriptdocument1 file1.ts");
externalFiles[0].content = "let x =1;";
projectService.applyChangesInOpenFiles(arrayIterator(externalFiles));

View File

@@ -1094,6 +1094,7 @@ namespace ts.projectSystem {
const project = projectService.inferredProjects[0];
checkProjectRootFiles(project, [file.path]);
checkProjectActualFiles(project, [file.path, libFile.path]);
verifyDynamic(projectService, `/${file.path}`);
assert.strictEqual(projectService.ensureDefaultProjectForFile(server.toNormalizedPath(file.path)), project);
const indexOfX = file.content.indexOf("x");
@@ -1124,6 +1125,7 @@ var x = 10;`
const host = createServerHost([libFile]);
const projectService = createProjectService(host);
projectService.openClientFile(file.path, file.content);
verifyDynamic(projectService, projectService.toPath(file.path));
projectService.checkNumberOfProjects({ inferredProjects: 1 });
const project = projectService.inferredProjects[0];
@@ -1152,6 +1154,7 @@ var x = 10;`
const projectService = session.getProjectService();
checkNumberOfProjects(projectService, { inferredProjects: 1 });
checkProjectActualFiles(projectService.inferredProjects[0], [file.path, libFile.path]);
verifyDynamic(projectService, `${tscWatch.projectRoot}/${file.path}`);
session.executeCommandSeq<protocol.OutliningSpansRequest>({
command: protocol.CommandTypes.GetOutliningSpans,

View File

@@ -1,20 +1,25 @@
namespace ts.projectSystem {
export function verifyDynamic(service: server.ProjectService, path: string) {
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 })))}`);
assert.isTrue(info.isDynamic);
}
describe("unittests:: tsserver:: Untitled files", () => {
const untitledFile = "untitled:^Untitled-1";
it("Can convert positions to locations", () => {
const aTs: File = { path: "/proj/a.ts", content: "" };
const tsconfig: File = { path: "/proj/tsconfig.json", content: "{}" };
const session = createSession(createServerHost([aTs, tsconfig]));
const session = createSession(createServerHost([aTs, tsconfig]), { useInferredProjectPerProjectRoot: true });
openFilesForSession([aTs], session);
const untitledFile = "untitled:^Untitled-1";
executeSessionRequestNoResponse<protocol.OpenRequest>(session, protocol.CommandTypes.Open, {
file: untitledFile,
fileContent: `/// <reference path="../../../../../../typings/@epic/Core.d.ts" />\nlet foo = 1;\nfooo/**/`,
scriptKindName: "TS",
projectRootPath: "/proj",
});
verifyDynamic(session.getProjectService(), `/proj/untitled:^untitled-1`);
const response = executeSessionRequest<protocol.CodeFixRequest, protocol.CodeFixResponse>(session, protocol.CommandTypes.GetCodeFixes, {
file: untitledFile,
startLine: 3,
@@ -41,5 +46,38 @@ namespace ts.projectSystem {
},
]);
});
it("opening untitled files", () => {
const config: File = {
path: `${tscWatch.projectRoot}/tsconfig.json`,
content: "{}"
};
const host = createServerHost([config, libFile], { useCaseSensitiveFileNames: true, currentDirectory: tscWatch.projectRoot });
const service = createProjectService(host);
service.openClientFile(untitledFile, "const x = 10;", /*scriptKind*/ undefined, tscWatch.projectRoot);
checkNumberOfProjects(service, { inferredProjects: 1 });
checkProjectActualFiles(service.inferredProjects[0], [untitledFile, libFile.path]);
verifyDynamic(service, `${tscWatch.projectRoot}/${untitledFile}`);
const untitled: File = {
path: `${tscWatch.projectRoot}/Untitled-1.ts`,
content: "const x = 10;"
};
host.writeFile(untitled.path, untitled.content);
host.checkTimeoutQueueLength(0);
service.openClientFile(untitled.path, untitled.content, /*scriptKind*/ undefined, tscWatch.projectRoot);
checkNumberOfProjects(service, { configuredProjects: 1, inferredProjects: 1 });
checkProjectActualFiles(service.configuredProjects.get(config.path)!, [untitled.path, libFile.path, config.path]);
checkProjectActualFiles(service.inferredProjects[0], [untitledFile, libFile.path]);
service.closeClientFile(untitledFile);
checkProjectActualFiles(service.configuredProjects.get(config.path)!, [untitled.path, libFile.path, config.path]);
checkProjectActualFiles(service.inferredProjects[0], [untitledFile, libFile.path]);
service.openClientFile(untitledFile, "const x = 10;", /*scriptKind*/ undefined, tscWatch.projectRoot);
verifyDynamic(service, `${tscWatch.projectRoot}/${untitledFile}`);
checkProjectActualFiles(service.configuredProjects.get(config.path)!, [untitled.path, libFile.path, config.path]);
checkProjectActualFiles(service.inferredProjects[0], [untitledFile, libFile.path]);
});
});
}