fix: set Dirent.parentPath in asar fs.readdir withFileTypes#51845
fix: set Dirent.parentPath in asar fs.readdir withFileTypes#51845sanidhyasin wants to merge 1 commit into
Conversation
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
|
@MarshallOfSound when you get a chance, would you mind taking a look at this? It's a small, self-contained fix in the asar 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
left a comment
There was a problem hiding this comment.
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.
Description of Change
Dirent.parentPathwasundefinedfor directory reads inside an asar archive whenwithFileTypes: truewas used.The asar
fswrapper constructsDirentobjects in its non-recursivereaddirhandlers (fs.readdir,fs.readdirSync, andfs.promises.readdir) withnew fs.Dirent(file, stats.type), omitting the thirdpathargument. Node'sDirentconstructor signature is(name, type, path), wherepathbecomesparentPath, so the property was left unset.This also affected
fs.glob/fs.globSyncwithwithFileTypes: true: Node's glob implementation reads directories through these wrappedreaddirfunctions and yields the returnedDirentobjects directly, so it inherited the missingparentPath. The property was already correct outside of asar, and on the wrapper's recursivereaddirpath, which uses the internalgetDirent(path, name, type)helper.The fix replaces
new fs.Dirent(file, stats.type)withgetDirent(pathArgument, file, stats.type)in the three non-recursive handlers, matching both the recursive path and Node's own behavior, soparentPathis set to the directory that was read.Tests were added/updated in
spec/asar-spec.ts: the existingreaddirwithFileTypesspecs now assertparentPath, and newfs.glob/fs.globSyncspecs 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.parentPathbeingundefinedforfs.readdir,fs.readdirSync,fs.glob, andfs.globSyncwithwithFileTypes: trueinside asar archives.