Skip to content

Commit

Permalink
Fixes readdir w/ recursive: true (#5543)
Browse files Browse the repository at this point in the history
**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.
  • Loading branch information
arcanis authored Jun 29, 2023
1 parent 200e84d commit 6cafe40
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 14 deletions.
8 changes: 4 additions & 4 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions .pnp.loader.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions .yarn/versions/97e32997.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
releases:
"@yarnpkg/cli": patch
"@yarnpkg/fslib": patch
"@yarnpkg/pnp": patch

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-exec"
- "@yarnpkg/plugin-file"
- "@yarnpkg/plugin-git"
- "@yarnpkg/plugin-github"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-link"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnp"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- vscode-zipfs
- "@yarnpkg/builder"
- "@yarnpkg/core"
- "@yarnpkg/doctor"
- "@yarnpkg/libzip"
- "@yarnpkg/nm"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
- "@yarnpkg/shell"
8 changes: 4 additions & 4 deletions packages/yarnpkg-fslib/sources/NodeFS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,8 @@ export class NodeFS extends BasePortableFakeFS {
async readdirPromise(p: PortablePath, opts: {recursive: boolean, withFileTypes: boolean}): Promise<Array<Dirent<PortablePath> | DirentNoPath | PortablePath>>;
async readdirPromise(p: PortablePath, opts?: ReaddirOptions | null): Promise<Array<Dirent<PortablePath> | DirentNoPath | PortablePath>> {
return await new Promise<any>((resolve, reject) => {
if (opts?.withFileTypes) {
this.realFs.readdir(npath.fromPortablePath(p), {withFileTypes: true}, this.makeCallback(resolve, reject) as any);
if (opts) {
this.realFs.readdir(npath.fromPortablePath(p), opts as any, this.makeCallback(resolve, reject) as any);
} else {
this.realFs.readdir(npath.fromPortablePath(p), this.makeCallback(value => resolve(value as Array<Filename>), reject));
}
Expand All @@ -453,8 +453,8 @@ export class NodeFS extends BasePortableFakeFS {
readdirSync(p: PortablePath, opts: {recursive: boolean, withFileTypes?: false}): Array<PortablePath>;
readdirSync(p: PortablePath, opts: {recursive: boolean, withFileTypes: boolean}): Array<Dirent<PortablePath> | DirentNoPath | PortablePath>;
readdirSync(p: PortablePath, opts?: ReaddirOptions | null): Array<Dirent<PortablePath> | DirentNoPath | PortablePath> {
if (opts?.withFileTypes) {
return this.realFs.readdirSync(npath.fromPortablePath(p), {withFileTypes: true} as any) as Array<any>;
if (opts) {
return this.realFs.readdirSync(npath.fromPortablePath(p), opts as any) as Array<any>;
} else {
return this.realFs.readdirSync(npath.fromPortablePath(p)) as Array<any>;
}
Expand Down
15 changes: 15 additions & 0 deletions packages/yarnpkg-fslib/tests/NodeFS.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,24 @@ import {xfs, PortablePath, ppath} from '../sources';

const nodeFs = new NodeFS();

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

describe(`NodeFS`, () => {
describe(`readdir`, () => {
ifAtLeastNode20It(`should support recursive directory listing`, async () => {
const tmpdir = await xfs.mktempPromise();

await xfs.mkdirPromise(ppath.join(tmpdir, `foo`));

await xfs.writeFilePromise(ppath.join(tmpdir, `foo/hello`), ``);
await xfs.writeFilePromise(ppath.join(tmpdir, `foo/world`), ``);

expect((await nodeFs.readdirPromise(tmpdir, {recursive: true})).sort()).toEqual([`foo`, `foo/hello`, `foo/world`]);
expect((nodeFs.readdirSync(tmpdir, {recursive: true})).sort()).toEqual([`foo`, `foo/hello`, `foo/world`]);
});
});

describe(`copyPromise`, () => {
it(`should support copying files`, async () => {
const tmpdir = await xfs.mktempPromise();
Expand Down
Loading

0 comments on commit 6cafe40

Please sign in to comment.