Skip to content

Commit fce9b6f

Browse files
authoredJun 29, 2023
Fixes readdir w/ recursive: true (#5543)
**What's the problem this PR addresses?** The `NodeFS` implementation of `readdir` was making an assumption regarding the name of the options which I didn't notice. **How did you fix it?** We now accept any option bag, not just those with `withFileTypes`. **Checklist** <!--- Don't worry if you miss something, chores are automatically tested. --> <!--- This checklist exists to help you remember doing the chores when you submit a PR. --> <!--- Put an `x` in all the boxes that apply. --> - [x] I have read the [Contributing Guide](https://yarnpkg.com/advanced/contributing). <!-- See https://yarnpkg.com/advanced/contributing#preparing-your-pr-to-be-released for more details. --> <!-- Check with `yarn version check` and fix with `yarn version check -i` --> - [x] I have set the packages that need to be released for my changes to be effective. <!-- The "Testing chores" workflow validates that your PR follows our guidelines. --> <!-- If it doesn't pass, click on it to see details as to what your PR might be missing. --> - [x] I will check that all automated PR checks pass before the PR gets reviewed.
1 parent 0f38966 commit fce9b6f

File tree

7 files changed

+66
-14
lines changed

7 files changed

+66
-14
lines changed
 

‎.pnp.cjs

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.pnp.loader.mjs

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.yarn/versions/97e32997.yml

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
releases:
2+
"@yarnpkg/cli": patch
3+
"@yarnpkg/fslib": patch
4+
"@yarnpkg/pnp": patch
5+
6+
declined:
7+
- "@yarnpkg/plugin-compat"
8+
- "@yarnpkg/plugin-constraints"
9+
- "@yarnpkg/plugin-dlx"
10+
- "@yarnpkg/plugin-essentials"
11+
- "@yarnpkg/plugin-exec"
12+
- "@yarnpkg/plugin-file"
13+
- "@yarnpkg/plugin-git"
14+
- "@yarnpkg/plugin-github"
15+
- "@yarnpkg/plugin-init"
16+
- "@yarnpkg/plugin-interactive-tools"
17+
- "@yarnpkg/plugin-link"
18+
- "@yarnpkg/plugin-nm"
19+
- "@yarnpkg/plugin-npm"
20+
- "@yarnpkg/plugin-npm-cli"
21+
- "@yarnpkg/plugin-pack"
22+
- "@yarnpkg/plugin-patch"
23+
- "@yarnpkg/plugin-pnp"
24+
- "@yarnpkg/plugin-pnpm"
25+
- "@yarnpkg/plugin-stage"
26+
- "@yarnpkg/plugin-typescript"
27+
- "@yarnpkg/plugin-version"
28+
- "@yarnpkg/plugin-workspace-tools"
29+
- vscode-zipfs
30+
- "@yarnpkg/builder"
31+
- "@yarnpkg/core"
32+
- "@yarnpkg/doctor"
33+
- "@yarnpkg/libzip"
34+
- "@yarnpkg/nm"
35+
- "@yarnpkg/pnpify"
36+
- "@yarnpkg/sdks"
37+
- "@yarnpkg/shell"

‎packages/yarnpkg-fslib/sources/NodeFS.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,8 @@ export class NodeFS extends BasePortableFakeFS {
434434
async readdirPromise(p: PortablePath, opts: {recursive: boolean, withFileTypes: boolean}): Promise<Array<Dirent<PortablePath> | DirentNoPath | PortablePath>>;
435435
async readdirPromise(p: PortablePath, opts?: ReaddirOptions | null): Promise<Array<Dirent<PortablePath> | DirentNoPath | PortablePath>> {
436436
return await new Promise<any>((resolve, reject) => {
437-
if (opts?.withFileTypes) {
438-
this.realFs.readdir(npath.fromPortablePath(p), {withFileTypes: true}, this.makeCallback(resolve, reject) as any);
437+
if (opts) {
438+
this.realFs.readdir(npath.fromPortablePath(p), opts as any, this.makeCallback(resolve, reject) as any);
439439
} else {
440440
this.realFs.readdir(npath.fromPortablePath(p), this.makeCallback(value => resolve(value as Array<Filename>), reject));
441441
}
@@ -453,8 +453,8 @@ export class NodeFS extends BasePortableFakeFS {
453453
readdirSync(p: PortablePath, opts: {recursive: boolean, withFileTypes?: false}): Array<PortablePath>;
454454
readdirSync(p: PortablePath, opts: {recursive: boolean, withFileTypes: boolean}): Array<Dirent<PortablePath> | DirentNoPath | PortablePath>;
455455
readdirSync(p: PortablePath, opts?: ReaddirOptions | null): Array<Dirent<PortablePath> | DirentNoPath | PortablePath> {
456-
if (opts?.withFileTypes) {
457-
return this.realFs.readdirSync(npath.fromPortablePath(p), {withFileTypes: true} as any) as Array<any>;
456+
if (opts) {
457+
return this.realFs.readdirSync(npath.fromPortablePath(p), opts as any) as Array<any>;
458458
} else {
459459
return this.realFs.readdirSync(npath.fromPortablePath(p)) as Array<any>;
460460
}

‎packages/yarnpkg-fslib/tests/NodeFS.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,24 @@ import {xfs, PortablePath, ppath} from '../sources';
33

44
const nodeFs = new NodeFS();
55

6+
const ifAtLeastNode20It = !process.version.match(/^v1[89]\./) ? it : it.skip;
67
const ifNotWin32It = process.platform !== `win32` ? it : it.skip;
78

89
describe(`NodeFS`, () => {
10+
describe(`readdir`, () => {
11+
ifAtLeastNode20It(`should support recursive directory listing`, async () => {
12+
const tmpdir = await xfs.mktempPromise();
13+
14+
await xfs.mkdirPromise(ppath.join(tmpdir, `foo`));
15+
16+
await xfs.writeFilePromise(ppath.join(tmpdir, `foo/hello`), ``);
17+
await xfs.writeFilePromise(ppath.join(tmpdir, `foo/world`), ``);
18+
19+
expect((await nodeFs.readdirPromise(tmpdir, {recursive: true})).sort()).toEqual([`foo`, `foo/hello`, `foo/world`]);
20+
expect((nodeFs.readdirSync(tmpdir, {recursive: true})).sort()).toEqual([`foo`, `foo/hello`, `foo/world`]);
21+
});
22+
});
23+
924
describe(`copyPromise`, () => {
1025
it(`should support copying files`, async () => {
1126
const tmpdir = await xfs.mktempPromise();

‎packages/yarnpkg-pnp/sources/esm-loader/built-loader.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/yarnpkg-pnp/sources/hook.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.