Skip to content

fix: set Dirent.parentPath in asar fs.readdir withFileTypes#51845

Open
sanidhyasin wants to merge 1 commit into
electron:mainfrom
sanidhyasin:sanidhyasin/asar-readdir-dirent-parentpath
Open

fix: set Dirent.parentPath in asar fs.readdir withFileTypes#51845
sanidhyasin wants to merge 1 commit into
electron:mainfrom
sanidhyasin:sanidhyasin/asar-readdir-dirent-parentpath

Conversation

@sanidhyasin

Copy link
Copy Markdown

Description of Change

Dirent.parentPath was undefined for directory reads inside an asar archive when withFileTypes: true was used.

The asar fs wrapper constructs Dirent objects in its non-recursive readdir handlers (fs.readdir, fs.readdirSync, and fs.promises.readdir) with new fs.Dirent(file, stats.type), omitting the third path argument. Node's Dirent constructor signature is (name, type, path), where path becomes parentPath, so the property was left unset.

This also affected fs.glob / fs.globSync with withFileTypes: true: Node's glob implementation reads directories through these wrapped readdir functions and yields the returned Dirent objects directly, so it inherited the missing parentPath. The property was already correct outside of asar, and on the wrapper's recursive readdir path, which uses the internal getDirent(path, name, type) helper.

The fix replaces new fs.Dirent(file, stats.type) with getDirent(pathArgument, file, stats.type) in the three non-recursive handlers, matching both the recursive path and Node's own behavior, so parentPath is set to the directory that was read.

Tests were added/updated in spec/asar-spec.ts: the existing readdir withFileTypes specs now assert parentPath, and new fs.glob / fs.globSync specs cover the originally reported scenario.

Note: this is a small, self-contained JS-level change in the asar wrapper; I did not run the full Electron test suite locally (it requires a full native build). CI will exercise the new specs.

Fixes #51838

Checklist

Release Notes

Notes: Fixed Dirent.parentPath being undefined for fs.readdir, fs.readdirSync, fs.glob, and fs.globSync with withFileTypes: true inside asar archives.

The asar fs wrapper built Dirent objects with `new fs.Dirent(name, type)`
in the non-recursive readdir handlers, which left `parentPath` undefined.
As a result `Dirent.parentPath` was missing for `fs.readdir` /
`readdirSync` (and for `fs.glob` / `globSync`, which read directories
through these handlers) when operating inside an asar archive, while it
was correctly populated outside of asar and on the recursive readdir
code path.

Use the internal `getDirent(path, name, type)` helper instead, matching
both the recursive readdir path and Node's own behavior, so `parentPath`
is set to the directory that was read.

Fixes electron#51838
@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Jun 2, 2026
@sanidhyasin

Copy link
Copy Markdown
Author

@MarshallOfSound when you get a chance, would you mind taking a look at this? It's a small, self-contained fix in the asar fs wrapper for the missing Dirent.parentPath on readdir/glob with withFileTypes inside asar archives (#51838).

Happy to adjust anything — tweak the approach, add/expand test coverage, or split it differently if you'd prefer. Just let me know if there's anything else needed to move it forward. Thanks!

@felixrieseberg felixrieseberg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix and the thorough writeup, this looks right to me.

Switching to getDirent(pathArgument, …) lines these three branches up with the recursive path and with Node's own behavior, and the asar stats.type is always a concrete dirent type so there's no risk of getDirent falling into its UV_DIRENT_UNKNOWN lstat branch. Nice that this also fixes the deprecated .path alias for free.

Tagging as semver/patch.

@felixrieseberg felixrieseberg added the semver/patch backwards-compatible bug fixes label Jun 8, 2026
@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Jun 8, 2026
@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Jun 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

42-x-y 43-x-y semver/patch backwards-compatible bug fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dirent.parentPath is missing from fs.glob/globSync withFileTypes under asar

2 participants