From afef282bb35200561913dd482d3e05f699e7b455 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Thu, 11 Nov 2021 14:21:33 -0800 Subject: [PATCH] Revert "Pass absolute path to directoryExists (#46086)" This reverts commit 55b4928e820c0a16698eff41ba08bfe550a75604. --- src/compiler/utilities.ts | 28 ++++++++++++------------ src/testRunner/unittests/publicApi.ts | 31 --------------------------- 2 files changed, 14 insertions(+), 45 deletions(-) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index aa3e8e0ee92b7..ea71b61acc8ff 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -6629,7 +6629,7 @@ namespace ts { includeFilePattern: getRegularExpressionForWildcard(includes, absolutePath, "files"), includeDirectoryPattern: getRegularExpressionForWildcard(includes, absolutePath, "directories"), excludePattern: getRegularExpressionForWildcard(excludes, absolutePath, "exclude"), - basePaths: getBasePaths(absolutePath, includes, useCaseSensitiveFileNames) + basePaths: getBasePaths(path, includes, useCaseSensitiveFileNames) }; } @@ -6653,22 +6653,22 @@ namespace ts { const results: string[][] = includeFileRegexes ? includeFileRegexes.map(() => []) : [[]]; const visited = new Map(); const toCanonical = createGetCanonicalFileName(useCaseSensitiveFileNames); - for (const absoluteBasePath of patterns.basePaths) { - if (directoryExists(absoluteBasePath)) { - visitDirectory(absoluteBasePath, depth); + for (const basePath of patterns.basePaths) { + if (directoryExists(basePath)) { + visitDirectory(basePath, combinePaths(currentDirectory, basePath), depth); } } return flatten(results); - function visitDirectory(absolutePath: string, depth: number | undefined) { + function visitDirectory(path: string, absolutePath: string, depth: number | undefined) { const canonicalPath = toCanonical(realpath(absolutePath)); if (visited.has(canonicalPath)) return; visited.set(canonicalPath, true); - const { files, directories } = getFileSystemEntries(absolutePath); + const { files, directories } = getFileSystemEntries(path); for (const current of sort(files, compareStringsCaseSensitive)) { - const name = combinePaths(absolutePath, current); + const name = combinePaths(path, current); const absoluteName = combinePaths(absolutePath, current); if (extensions && !fileExtensionIsOneOf(name, extensions)) continue; if (excludeRegex && excludeRegex.test(absoluteName)) continue; @@ -6691,10 +6691,11 @@ namespace ts { } for (const current of sort(directories, compareStringsCaseSensitive)) { + const name = combinePaths(path, current); const absoluteName = combinePaths(absolutePath, current); if ((!includeDirectoryRegex || includeDirectoryRegex.test(absoluteName)) && (!excludeRegex || !excludeRegex.test(absoluteName))) { - visitDirectory(absoluteName, depth); + visitDirectory(name, absoluteName, depth); } } } @@ -6702,11 +6703,10 @@ namespace ts { /** * Computes the unique non-wildcard base paths amongst the provided include patterns. - * @returns Absolute directory paths */ - function getBasePaths(absoluteTsconfigPath: string, includes: readonly string[] | undefined, useCaseSensitiveFileNames: boolean): string[] { + function getBasePaths(path: string, includes: readonly string[] | undefined, useCaseSensitiveFileNames: boolean): string[] { // Storage for our results in the form of literal paths (e.g. the paths as written by the user). - const basePaths: string[] = [absoluteTsconfigPath]; + const basePaths: string[] = [path]; if (includes) { // Storage for literal base paths amongst the include patterns. @@ -6714,9 +6714,9 @@ namespace ts { for (const include of includes) { // We also need to check the relative paths by converting them to absolute and normalizing // in case they escape the base path (e.g "..\somedirectory") - const absoluteIncludePath: string = isRootedDiskPath(include) ? include : normalizePath(combinePaths(absoluteTsconfigPath, include)); + const absolute: string = isRootedDiskPath(include) ? include : normalizePath(combinePaths(path, include)); // Append the literal and canonical candidate base paths. - includeBasePaths.push(getIncludeBasePath(absoluteIncludePath)); + includeBasePaths.push(getIncludeBasePath(absolute)); } // Sort the offsets array using either the literal or canonical path representations. @@ -6725,7 +6725,7 @@ namespace ts { // Iterate over each include base path and include unique base paths that are not a // subpath of an existing base path for (const includeBasePath of includeBasePaths) { - if (every(basePaths, basePath => !containsPath(basePath, includeBasePath, absoluteTsconfigPath, !useCaseSensitiveFileNames))) { + if (every(basePaths, basePath => !containsPath(basePath, includeBasePath, path, !useCaseSensitiveFileNames))) { basePaths.push(includeBasePath); } } diff --git a/src/testRunner/unittests/publicApi.ts b/src/testRunner/unittests/publicApi.ts index e741e902b4e31..883b979f9c72e 100644 --- a/src/testRunner/unittests/publicApi.ts +++ b/src/testRunner/unittests/publicApi.ts @@ -182,34 +182,3 @@ describe("unittests:: Public APIs:: getChild* methods on EndOfFileToken with JSD assert.equal(endOfFileToken.getChildCount(), 1); assert.notEqual(endOfFileToken.getChildAt(0), /*expected*/ undefined); }); - -describe("unittests:: Public APIs:: sys", () => { - it("readDirectory", () => { - // #45990, testing passing a non-absolute path - // `sys.readDirectory` is just `matchFiles` plugged into the real FS - const read = ts.matchFiles( - /*path*/ "", - /*extensions*/ [".ts", ".tsx"], - /*excludes*/ ["node_modules", "dist"], - /*includes*/ ["**/*"], - /*useCaseSensitiveFileNames*/ true, - /*currentDirectory*/ "/", - /*depth*/ undefined, - /*getFileSystemEntries*/ path => { - switch (path) { - case "/": return { directories: [], files: ["file.ts"] }; - default: return { directories: [], files: [] }; - } - }, - /*realpath*/ ts.identity, - /*directoryExists*/ path => { - switch (path) { - case "/": return true; - default: return false; - } - } - ); - - assert.deepEqual(read, ["/file.ts"]); - }); -});