memfs icon indicating copy to clipboard operation
memfs copied to clipboard

fs.readFile reads wrong file after renaming parent dir

Open Nishkalkashyap opened this issue 6 years ago • 3 comments

Steps to reproduce :

  1. fs.mkdirSync('/dir');

  2. fs.writeFileSync('/dir/file.ext');

  3. fs.readFileSync('/dir/file.ext'); //returns Buffer[]

  4. fs.renameSync('/dir/file.ext', '/dir/newfile.ext');

  5. fx.readFileSync('/dir/newfile.ext'); // Throws Error

Error: ENOENT: no such file or directory, open '/dir/file.ext'

Even though new file name is entered in readFileSync, it still tries to look for the old file?

Nishkalkashyap avatar Dec 16 '18 14:12 Nishkalkashyap

If in step 5 you rename fx to fs, does it work?

This works for me

var fs = require("memfs")

fs.mkdirSync('/dir');
fs.writeFileSync('/dir/file.ext', 'asdf');
fs.readFileSync('/dir/file.ext');
fs.renameSync('/dir/file.ext', '/dir/newfile.ext');

console.log(fs.readFileSync('/dir/newfile.ext', 'utf8'));

streamich avatar Dec 16 '18 18:12 streamich

@streamich try this reproduction, please: 🙏

const fs = require('memfs');

fs.mkdirSync('dir', { recursive: true });
fs.writeFileSync('dir/file.ext', 'asdf');

fs.renameSync('dir', 'newdir');
fs.readFileSync('newdir/file.ext', 'utf8');

Still tries to read from the old path:

Error: ENOENT: no such file or directory, open '/(...)/dir/file.ext'

Note: it works just fine with const fs = require('fs');

falkenhawk avatar Jul 09 '20 13:07 falkenhawk

Also note: statSync('newdir/file.ext'), accessSync, existsSync all work fine after rename! But vol.toJSON() still lists the file under the "old" path: { '/(...)/dir/file.ext': 'asdf' }

falkenhawk avatar Jul 09 '20 13:07 falkenhawk

@falkenhawk your reproduction works fine for me using the latest version of memfs so I'm going to close this as hopefully its been fixed - feel free to open a new issue if you have any further problems.

G-Rath avatar Nov 03 '22 19:11 G-Rath