Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work around write-file-atomic Windows EPERM issue #15

Merged
merged 2 commits into from
May 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,29 @@ function wrap(opts) {

onHash(input, metadata, hash);

try {
return fs.readFileSync(cachedPath, encoding);
} catch (error) {
const result = transform(input, metadata, hash);
writeFileAtomic.sync(cachedPath, result, {encoding});
return result;
let result;
let retry = 0;
/* eslint-disable-next-line no-constant-condition */
while (true) {
try {
return fs.readFileSync(cachedPath, encoding);
} catch (readError) {
if (!result) {
result = transform(input, metadata, hash);
}

try {
writeFileAtomic.sync(cachedPath, result, {encoding});
return result;
} catch (error) {
coreyfarrell marked this conversation as resolved.
Show resolved Hide resolved
/* Likely https://github.com/npm/write-file-atomic/issues/28
* Make up to 3 attempts to read or write the cache. */
retry++;
if (retry > 3) {
throw error;
}
}
}
}
};
}
Expand Down