sindresorhus / tempy

Get a random temporary file or directory path
MIT License
423 stars 26 forks source link

Prefix which includes a path.sep fails #43

Closed justinormont closed 1 year ago

justinormont commented 2 years ago

Prefix which includes a path.sep fails.

Repro

This fails as the path will include a path.sep:

const uuid = 'xyz';
const prefixForRun = path.join('runs', `run_${uuid}`);
const outputFolder = temporaryDirectory({prefix: prefixForRun});

This call results in a path of: '/private/var/folders/6t/frtzt7q96fv9xjqp3knl3vqh0000gn/T/runs/run_xyz'

The mkdir for run_xyz fails since the parent, /private/var/folders/6t/frtzt7q96fv9xjqp3knl3vqh0000gn/T/runs/, does not exist.

Error

Error: ENOENT: no such file or directory, mkdir '/private/var/folders/6t/frtzt7q96fv9xjqp3knl3vqh0000gn/T/runs/run_xyz'
    at Object.mkdirSync (node:fs:1324:3)

Solution

In mkdir, add the {recursive: true} option.

Discussion

Incidentally, fixing this gives an indirect method of https://github.com/sindresorhus/tempy/issues/35, as my intention was similar. To organize my temp files/folders for later cleanup, I was adding a parent folder to my requested temp folder.

sindresorhus commented 1 year ago

Closing in favor of https://github.com/sindresorhus/tempy/issues/46