diff --git a/src/emulation/promises.ts b/src/emulation/promises.ts index 6cf2f4a8..7b5ab52d 100644 --- a/src/emulation/promises.ts +++ b/src/emulation/promises.ts @@ -23,7 +23,7 @@ import { Stats } from '../stats'; */ async function doOp(fn: F, resolveSymlinks: boolean, ...[path, ...args]: Parameters): Promise> { path = normalizePath(path); - const { fs, path: resolvedPath } = resolveFS(resolveSymlinks && await exists(path) ? await realpath(path) : path); + const { fs, path: resolvedPath } = resolveFS(resolveSymlinks && (await exists(path)) ? await realpath(path) : path); try { // @ts-expect-error 2556 (since ...args is not correctly picked up as being a tuple) return fs[fn](resolvedPath, ...args) as Promise>; @@ -68,7 +68,7 @@ export async function exists(path: string): Promise { const { fs, path: resolvedPath } = resolveFS(path); return fs.exists(resolvedPath, cred); } catch (e) { - if((e as ApiError).errno == ErrorCode.ENOENT) { + if ((e as ApiError).errno == ErrorCode.ENOENT) { return false; } diff --git a/src/emulation/sync.ts b/src/emulation/sync.ts index 0f0de25d..6cf4bbd5 100644 --- a/src/emulation/sync.ts +++ b/src/emulation/sync.ts @@ -3,7 +3,7 @@ import { FileFlag } from '../file'; import { FileContents, FileSystem } from '../filesystem'; import { Stats } from '../stats'; import type { symlink, ReadSyncOptions } from 'fs'; -import { normalizePath, cred, getFdForFile, normalizeMode, normalizeOptions, fdMap, fd2file, normalizeTime, resolveFS, fixError } from './shared'; +import { normalizePath, cred, getFdForFile, normalizeMode, normalizeOptions, fdMap, fd2file, normalizeTime, resolveFS, fixError, mounts } from './shared'; function doOp(fn: F, resolveSymlinks: boolean, ...[path, ...args]: Parameters): ReturnType { path = normalizePath(path); @@ -50,7 +50,7 @@ export function existsSync(path: string): boolean { const { fs, path: resolvedPath } = resolveFS(path); return fs.existsSync(resolvedPath, cred); } catch (e) { - if((e as ApiError).errno == ErrorCode.ENOENT) { + if ((e as ApiError).errno == ErrorCode.ENOENT) { return false; } @@ -356,7 +356,20 @@ export function mkdirSync(path: string, mode?: number | string): void { * @return [String[]] */ export function readdirSync(path: string): string[] { - return doOp('readdirSync', true, path, cred); + path = normalizePath(path); + const entries = doOp('readdirSync', true, path, cred); + const points = [...mounts.keys()]; + for (const point of points) { + if (point.startsWith(path)) { + const entry = point.slice(path.length); + if (entry.includes('/') || entry.length == 0) { + // ignore FSs mounted in subdirectories and any FS mounted to `path`. + continue; + } + entries.push(entry); + } + } + return entries; } // SYMLINK METHODS