fs-extra-promise icon indicating copy to clipboard operation
fs-extra-promise copied to clipboard

writeFile, writeJson resolve before finishing writing

Open 1mike12 opened this issue 9 years ago • 9 comments
trafficstars

When I try to use these methods they will work, but they call resolve before they actually write their files. So what happens is that the next chain expects the file to be there, and work on it, except I get a ENOENT.

The only way I cant get this to work is to go back to default fs.

var Promise = require("bluebird")

var fs = require("fs");
//works
return new Promise(function(resolve, reject){
    fs.writeFile(currentJsonPath, "{}", function(e, data){
        if (e){
            reject(e)
        } else {
            resolve()
        }
    });
});

//writes file eventually, but calls resolve early
var fsExtra = Promise.promisifyAll(require("fs-extra"));
return fsExtra.writeFile(currentJsonPath, "{}");

//writes file eventually, but calls resolve early
var fsExtraPromise= require("fs-extra-promise");
return fsExtraPromise.writeFile(currentJsonPath, "{}");

1mike12 avatar Sep 30 '16 06:09 1mike12

Oh shit! This is a really bad bug.

But I suspect this is a bug in fs-extra rather than fs-extra-promise.

Just to make sure this has nothing to do with promisification, could you possibly try this:

var fsExtra = require('fs-extra');
fsExtra.writeFile( currentJsonPath, "{}", function(e) {
    if (e) throw e;

    // check file has been written
} );

overlookmotel avatar Sep 30 '16 10:09 overlookmotel

Yeah, I think I agree it's probably fs-extra.

I'm using https://www.npmjs.com/package/json-file-pluspackage, and the file is still not there when I write it as a callback

var jsonFile = require("json-file-plus")
 var fsExtra = require('fs-extra');
            fsExtra.writeFile( currentJsonPath, "{}", function(e) {
                if (e) throw e; //is null

                jsonFile(currentJsonPath) //but file is not actually there
            });


            fsExtra.writeFile( currentJsonPath, "{}", function(e) {
                if (e) throw e;

                setTimeout(jsonFile(currentJsonPath), 500) //no error, and works as expected
            });

1mike12 avatar Sep 30 '16 10:09 1mike12

Just to remove the possibility of json-file-plus being the location of the fault, could you please try using a vanilla fs method like fs.readFile to check the file's existence?

But yes, if that still fails, it's definitely a bug in fs-extra. And a pretty bad one if you ask me.

overlookmotel avatar Sep 30 '16 12:09 overlookmotel

@1mike12 How you getting on with this? Would be good to narrow this down so can raise an issue on fs-extra if that's where the fault is.

overlookmotel avatar Oct 07 '16 17:10 overlookmotel

By the way, what OS and version of Node are you using?

overlookmotel avatar Oct 07 '16 17:10 overlookmotel

Ping @1mike12!

overlookmotel avatar Oct 16 '16 14:10 overlookmotel

Ping @1mike12! Did you ever work out what the problem was here? If it's a bug in fs-extra-promise, I'd like to fix it, but it did look like the culprit was more likely to be in fs-extra. Please let me know so I can close this issue if it's solved.

overlookmotel avatar Dec 26 '16 23:12 overlookmotel

yeah I've been super busy, I was trying to write a quick test this morning with a forked vers of this, but ran into some trouble. I'll try to get it up b4 this weekend ends

1mike12 avatar Jan 14 '17 20:01 1mike12

@1mike12 Any progress with this? NB Have just released v1.0.0 which has latest versions of fs-extra and bluebird. So they may have solved this problem in the meantime.

overlookmotel avatar Apr 08 '17 16:04 overlookmotel