raszi / node-tmp

Temporary file and directory creator for node.js
MIT License
736 stars 92 forks source link

Windows Temp file not working electron #176

Closed do-web closed 4 years ago

do-web commented 5 years ago

Operating System

NodeJS Version

Tmp Version

TBD:Enter tmp version here newest

Expected Behavior

TBD:What have you expected tmp to do? correct temp file.

Experienced Behavior

TBD:What did actually happen?

It generates this file:

let fileName = tmp.tmpNameSync();
undefined\temp\tmp-3476obo4mLnOL8Bp

Then i trying to write to this file:

fs.writeFileSync(fileName, new Buffer(result.data));
no such file or directory, open 'C:\Users\xxx\xx\app-1.0.2\undefined\temp\tmp-3476obo4mLnOL8Bp'
raszi commented 5 years ago

According to the source of os-tmpdir this could only happen if neither of the following environment variables are set: TEMP, TMP, SystemRoot, and windir.

Could you verify that for me please by dumping the process.env.

The following should work:

console.log(JSON.stringify(process.env))
raszi commented 5 years ago

Could you please also show me what os.tmpdir() says to you?

const os = require('os');
console.log(os.tmpdir());
do-web commented 5 years ago

This is working now: tmp.tmpNameSync({ dir: app.getPath('temp') })

raszi commented 5 years ago

Could you please provide the requested information, so we could make sure that tmp works in electron on your Windows platform too? Thank you!

silkentrance commented 5 years ago

@raszi I believe that tmp should fail early whenever it encounters an undefined tmpdir. What do you think?

raszi commented 5 years ago

@silkentrance I believe that is a good idea!

silkentrance commented 4 years ago

This was never fixed, at least not correctly.

It will also break with the changes for #207 and the way that the dir option is being treated, which can no longer be an absolute path outside of the system's default.

The correct behaviour should be for _getTmpDir() to throw an exception if the os.tmpDir() cannot be resolved as a path using path.resolve().

From the official electron docs https://www.electronjs.org/docs/all

Avoid platform-dependent code:

Use path.join() to concatenate filenames.
Use os.tmpdir() rather than /tmp when you need to reference the temporary directory.

So os.tmpdir() should be set and working around using app.getPath('temp') must not be used.