node-proper-lockfile icon indicating copy to clipboard operation
node-proper-lockfile copied to clipboard

Throws error if path leading to a file doesn't exist yet.

Open ZeeCoder opened this issue 5 years ago • 4 comments

First of all: thanks for the great lib!

I've encountered a weird issue.

This works fine even if the file doesn't exist:

lockfile.lock('./file.txt');

This however throws errors, when the path doesn't exist yet:

lockfile.lock('./path/that/does/not/exist/file.txt');

For the time being I've just made sure to use a wrapper that ensures directory paths leading to files that are about to be locked (whether existing or not yet) exist.

ZeeCoder avatar Jan 28 '20 14:01 ZeeCoder

Hello! That’s because we try to resolve the real path of the file. You may disable the realpath option to disable this behavior.

satazor avatar Jan 28 '20 14:01 satazor

Yeah I tried that. 🤔

With realpath: false I get this error:

ENOENT: no such file or directory, mkdir (...)

With realpath: true, I get this:

Error: ENOENT: no such file or directory, lstat (...)

ZeeCoder avatar Jan 28 '20 14:01 ZeeCoder

Oh I misunderstood the question. At the moment, the path to the file must exist.

Perhaps we could create the directory first with the mkdirp package, perhaps under and option. Are you willing to take some time to contribute? I'm happy to review a PR that implements this.

satazor avatar Jan 28 '20 14:01 satazor

Yeah I might, I've simply used fs-extra's ensureDir function at the moment, not sure if graceful-fs has anything like it: https://github.com/jprichardson/node-fs-extra/blob/master/docs/ensureDir.md

ZeeCoder avatar Jan 28 '20 14:01 ZeeCoder