streamich / fs-monkey

Monkey-patches for file system related things
The Unlicense
109 stars 19 forks source link

Endless recursion when using `patchFs` with `unionfs` #250

Open rrthomas opened 3 years ago

rrthomas commented 3 years ago

The following code:

unionfs = require('unionfs')
ufs = unionfs.ufs
fs = require('fs')
ufs.use(fs)
fsMonkey = require('fs-monkey')
patchFs = fsMonkey.patchFs
patchFs(ufs)

fs.readdirSync('/')

gives the error:

…/node_modules/unionfs/lib/union.js:346
                        throw err;
                        ^

RangeError: Maximum call stack size exceeded
    at Union.readdirSync (/home/rrt/Software/nancy/node_modules/unionfs/lib/union.js:334:38)
    at Union.readdirSync (/home/rrt/Software/nancy/node_modules/unionfs/lib/union.js:329:79)
    at Union.readdirSync (/home/rrt/Software/nancy/node_modules/unionfs/lib/union.js:329:79)
    at Union.readdirSync (/home/rrt/Software/nancy/node_modules/unionfs/lib/union.js:329:79)
    at Union.readdirSync (/home/rrt/Software/nancy/node_modules/unionfs/lib/union.js:329:79)
    at Union.readdirSync (/home/rrt/Software/nancy/node_modules/unionfs/lib/union.js:329:79)
    at Union.readdirSync (/home/rrt/Software/nancy/node_modules/unionfs/lib/union.js:329:79)
    at Union.readdirSync (/home/rrt/Software/nancy/node_modules/unionfs/lib/union.js:329:79)
    at Union.readdirSync (/home/rrt/Software/nancy/node_modules/unionfs/lib/union.js:329:79)
    at Union.readdirSync (/home/rrt/Software/nancy/node_modules/unionfs/lib/union.js:329:79) {
  prev: null
}

I presume the problem arises because fs is patched to use ufs, but that in turn is using fs? But the new fs should use ufs, while ufs should use the old fs? I guess that this sort of patching is quite fiddly and so there may be a gotcha here…

Thanks for fs-monkey!

rahulbakale commented 3 years ago

@rrthomas https://github.com/streamich/fs-monkey/issues/139#issuecomment-511059950 describes a solution that helped me get past this issue. Hope it helps you.