From 8281c7a137348f9e7392d35327283234ff4791a8 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Wed, 17 Jan 2018 14:51:31 -0800 Subject: [PATCH] Fix the invalid file/directory location when getting file system entry for caching read directory results Fixes #20607 --- src/compiler/core.ts | 7 ++++- src/compiler/sys.ts | 2 +- src/compiler/utilities.ts | 1 - .../unittests/tsserverProjectSystem.ts | 26 +++++++++++++++++++ 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/compiler/core.ts b/src/compiler/core.ts index d92a02661e8..5fa9a544f8f 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -20,6 +20,7 @@ namespace ts { /* @internal */ namespace ts { + export const emptyArray: never[] = [] as never[]; /** Create a MapLike with good performance. */ function createDictionaryObject(): MapLike { const map = Object.create(/*prototype*/ null); // tslint:disable-line:no-null-keyword @@ -3069,6 +3070,10 @@ namespace ts { readonly directories: string[]; } + export const emptyFileSystemEntries: FileSystemEntries = { + files: emptyArray, + directories: emptyArray + }; export function createCachedDirectoryStructureHost(host: DirectoryStructureHost): CachedDirectoryStructureHost { const cachedReadDirectoryResult = createMap(); const getCurrentDirectory = memoize(() => host.getCurrentDirectory()); @@ -3210,7 +3215,7 @@ namespace ts { if (path === rootDirPath) { return result; } - return getCachedFileSystemEntries(path) || createCachedFileSystemEntries(dir, path); + return tryReadDirectory(dir, path) || emptyFileSystemEntries; } } diff --git a/src/compiler/sys.ts b/src/compiler/sys.ts index 841dba8a528..5293657a646 100644 --- a/src/compiler/sys.ts +++ b/src/compiler/sys.ts @@ -398,7 +398,7 @@ namespace ts { return { files, directories }; } catch (e) { - return { files: [], directories: [] }; + return emptyFileSystemEntries; } } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index ffcdf42c67e..75083e159c5 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -2,7 +2,6 @@ /* @internal */ namespace ts { - export const emptyArray: never[] = [] as never[]; export const resolvingEmptyArray: never[] = [] as never[]; export const emptyMap: ReadonlyMap = createMap(); export const emptyUnderscoreEscapedMap: ReadonlyUnderscoreEscapedMap = emptyMap as ReadonlyUnderscoreEscapedMap; diff --git a/src/harness/unittests/tsserverProjectSystem.ts b/src/harness/unittests/tsserverProjectSystem.ts index 68fbc1ecba0..2ac0945587a 100644 --- a/src/harness/unittests/tsserverProjectSystem.ts +++ b/src/harness/unittests/tsserverProjectSystem.ts @@ -4168,6 +4168,32 @@ namespace ts.projectSystem { // Since no file from the configured project is open, it would be closed immediately projectService.checkNumberOfProjects({ configuredProjects: 0, inferredProjects: 1 }); }); + + it("should tolerate invalid include files that start in subDirectory", () => { + const projectFolder = "/user/username/projects/myproject"; + const f = { + path: `${projectFolder}/src/server/index.ts`, + content: "let x = 1" + }; + const config = { + path: `${projectFolder}/src/server/tsconfig.json`, + content: JSON.stringify({ + compiler: { + module: "commonjs", + outDir: "../../build" + }, + include: [ + "../src/**/*.ts" + ] + }) + }; + const host = createServerHost([f, config, libFile], { useCaseSensitiveFileNames: true }); + const projectService = createProjectService(host); + + projectService.openClientFile(f.path); + // Since no file from the configured project is open, it would be closed immediately + projectService.checkNumberOfProjects({ configuredProjects: 0, inferredProjects: 1 }); + }); }); describe("tsserverProjectSystem reload", () => {