Skip to content

Commit

Permalink
Merge pull request #18127 from RyanCavanaugh/port18120_release25
Browse files Browse the repository at this point in the history
Don't crash when a JS file appears in an inferred context
  • Loading branch information
RyanCavanaugh committed Aug 29, 2017
2 parents 6ffe829 + 6425ea2 commit 20da159
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
28 changes: 25 additions & 3 deletions src/harness/unittests/tsserverProjectSystem.ts
Expand Up @@ -1760,6 +1760,28 @@ namespace ts.projectSystem {
checkProjectActualFiles(projectService.externalProjects[0], [file1.path, file2.path, file3.path]);
});

it("regression test for crash in acquireOrUpdateDocument", () => {
const tsFile = {
fileName: "/a/b/file1.ts",
path: "/a/b/file1.ts",
content: ""
};
const jsFile = {
path: "/a/b/file1.js",
content: "var x = 10;",
fileName: "/a/b/file1.js",
scriptKind: "JS" as "JS"
};

const host = createServerHost([]);
const projectService = createProjectService(host);
projectService.applyChangesInOpenFiles([tsFile], [], []);
const projs = projectService.synchronizeProjectList([]);
projectService.findProject(projs[0].info.projectName).getLanguageService().getNavigationBarItems(tsFile.fileName);
projectService.synchronizeProjectList([projs[0].info]);
projectService.applyChangesInOpenFiles([jsFile], [], []);
});

it("config file is deleted", () => {
const file1 = {
path: "/a/b/f1.ts",
Expand Down Expand Up @@ -1860,7 +1882,7 @@ namespace ts.projectSystem {

// Open HTML file
projectService.applyChangesInOpenFiles(
/*openFiles*/ [{ fileName: file2.path, hasMixedContent: true, scriptKind: ScriptKind.JS, content: `var hello = "hello";` }],
/*openFiles*/[{ fileName: file2.path, hasMixedContent: true, scriptKind: ScriptKind.JS, content: `var hello = "hello";` }],
/*changedFiles*/ undefined,
/*closedFiles*/ undefined);

Expand All @@ -1877,7 +1899,7 @@ namespace ts.projectSystem {
projectService.applyChangesInOpenFiles(
/*openFiles*/ undefined,
/*changedFiles*/ undefined,
/*closedFiles*/ [file2.path]);
/*closedFiles*/[file2.path]);

// HTML file is still included in project
checkNumberOfProjects(projectService, { configuredProjects: 1 });
Expand Down Expand Up @@ -3308,7 +3330,7 @@ namespace ts.projectSystem {
const error1Result = <protocol.Diagnostic[]>session.executeCommand(dTsFile1GetErrRequest).response;
assert.isTrue(error1Result.length === 0);

const dTsFile2GetErrRequest = makeSessionRequest<protocol.SemanticDiagnosticsSyncRequestArgs>(
const dTsFile2GetErrRequest = makeSessionRequest<protocol.SemanticDiagnosticsSyncRequestArgs>(
CommandNames.SemanticDiagnosticsSync,
{ file: dTsFile2.path }
);
Expand Down
2 changes: 1 addition & 1 deletion src/server/editorServices.ts
Expand Up @@ -1609,7 +1609,7 @@ namespace ts.server {
if (openFiles) {
for (const file of openFiles) {
const scriptInfo = this.getScriptInfo(file.fileName);
Debug.assert(!scriptInfo || !scriptInfo.isScriptOpen());
Debug.assert(!scriptInfo || !scriptInfo.isScriptOpen(), "Script should not exist and not be open already");
const normalizedPath = scriptInfo ? scriptInfo.fileName : toNormalizedPath(file.fileName);
this.openClientFileWithNormalizedPath(normalizedPath, file.content, tryConvertScriptKindName(file.scriptKind), file.hasMixedContent);
}
Expand Down
20 changes: 9 additions & 11 deletions src/services/documentRegistry.ts
Expand Up @@ -173,14 +173,12 @@ namespace ts {
const bucket = getBucketForCompilationSettings(key, /*createIfMissing*/ true);
let entry = bucket.get(path);
if (!entry) {
Debug.assert(acquiring, "How could we be trying to update a document that the registry doesn't have?");

// Have never seen this file with these settings. Create a new source file for it.
const sourceFile = createLanguageServiceSourceFile(fileName, scriptSnapshot, compilationSettings.target, version, /*setNodeParents*/ false, scriptKind);

entry = {
sourceFile,
languageServiceRefCount: 0,
languageServiceRefCount: 1,
owners: []
};
bucket.set(path, entry);
Expand All @@ -193,15 +191,15 @@ namespace ts {
entry.sourceFile = updateLanguageServiceSourceFile(entry.sourceFile, scriptSnapshot, version,
scriptSnapshot.getChangeRange(entry.sourceFile.scriptSnapshot));
}
}

// If we're acquiring, then this is the first time this LS is asking for this document.
// Increase our ref count so we know there's another LS using the document. If we're
// not acquiring, then that means the LS is 'updating' the file instead, and that means
// it has already acquired the document previously. As such, we do not need to increase
// the ref count.
if (acquiring) {
entry.languageServiceRefCount++;
// If we're acquiring, then this is the first time this LS is asking for this document.
// Increase our ref count so we know there's another LS using the document. If we're
// not acquiring, then that means the LS is 'updating' the file instead, and that means
// it has already acquired the document previously. As such, we do not need to increase
// the ref count.
if (acquiring) {
entry.languageServiceRefCount++;
}
}

return entry.sourceFile;
Expand Down

0 comments on commit 20da159

Please sign in to comment.