Skip to content

Commit 8281c7a

Browse files
committed
Fix the invalid file/directory location when getting file system entry for caching read directory results
Fixes #20607
1 parent e248d08 commit 8281c7a

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

src/compiler/core.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace ts {
2020

2121
/* @internal */
2222
namespace ts {
23+
export const emptyArray: never[] = [] as never[];
2324
/** Create a MapLike with good performance. */
2425
function createDictionaryObject<T>(): MapLike<T> {
2526
const map = Object.create(/*prototype*/ null); // tslint:disable-line:no-null-keyword
@@ -3069,6 +3070,10 @@ namespace ts {
30693070
readonly directories: string[];
30703071
}
30713072

3073+
export const emptyFileSystemEntries: FileSystemEntries = {
3074+
files: emptyArray,
3075+
directories: emptyArray
3076+
};
30723077
export function createCachedDirectoryStructureHost(host: DirectoryStructureHost): CachedDirectoryStructureHost {
30733078
const cachedReadDirectoryResult = createMap<MutableFileSystemEntries>();
30743079
const getCurrentDirectory = memoize(() => host.getCurrentDirectory());
@@ -3210,7 +3215,7 @@ namespace ts {
32103215
if (path === rootDirPath) {
32113216
return result;
32123217
}
3213-
return getCachedFileSystemEntries(path) || createCachedFileSystemEntries(dir, path);
3218+
return tryReadDirectory(dir, path) || emptyFileSystemEntries;
32143219
}
32153220
}
32163221

src/compiler/sys.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ namespace ts {
398398
return { files, directories };
399399
}
400400
catch (e) {
401-
return { files: [], directories: [] };
401+
return emptyFileSystemEntries;
402402
}
403403
}
404404

src/compiler/utilities.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
/* @internal */
44
namespace ts {
5-
export const emptyArray: never[] = [] as never[];
65
export const resolvingEmptyArray: never[] = [] as never[];
76
export const emptyMap: ReadonlyMap<never> = createMap<never>();
87
export const emptyUnderscoreEscapedMap: ReadonlyUnderscoreEscapedMap<never> = emptyMap as ReadonlyUnderscoreEscapedMap<never>;

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4168,6 +4168,32 @@ namespace ts.projectSystem {
41684168
// Since no file from the configured project is open, it would be closed immediately
41694169
projectService.checkNumberOfProjects({ configuredProjects: 0, inferredProjects: 1 });
41704170
});
4171+
4172+
it("should tolerate invalid include files that start in subDirectory", () => {
4173+
const projectFolder = "/user/username/projects/myproject";
4174+
const f = {
4175+
path: `${projectFolder}/src/server/index.ts`,
4176+
content: "let x = 1"
4177+
};
4178+
const config = {
4179+
path: `${projectFolder}/src/server/tsconfig.json`,
4180+
content: JSON.stringify({
4181+
compiler: {
4182+
module: "commonjs",
4183+
outDir: "../../build"
4184+
},
4185+
include: [
4186+
"../src/**/*.ts"
4187+
]
4188+
})
4189+
};
4190+
const host = createServerHost([f, config, libFile], { useCaseSensitiveFileNames: true });
4191+
const projectService = createProjectService(host);
4192+
4193+
projectService.openClientFile(f.path);
4194+
// Since no file from the configured project is open, it would be closed immediately
4195+
projectService.checkNumberOfProjects({ configuredProjects: 0, inferredProjects: 1 });
4196+
});
41714197
});
41724198

41734199
describe("tsserverProjectSystem reload", () => {

0 commit comments

Comments
 (0)