From f6affeacb9aa944a01ed4bafa84e8c8a24c272fc Mon Sep 17 00:00:00 2001 From: Justin Bay Date: Mon, 9 Oct 2017 21:33:02 -0400 Subject: [PATCH] ignore cache write EPERM errors on windows if we're reasonably sure they came from a cross-process race condition --- .../jest-runtime/src/script_transformer.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/jest-runtime/src/script_transformer.js b/packages/jest-runtime/src/script_transformer.js index cea308731189..21c176b65204 100644 --- a/packages/jest-runtime/src/script_transformer.js +++ b/packages/jest-runtime/src/script_transformer.js @@ -402,6 +402,10 @@ const writeCacheFile = (cachePath: Path, fileData: string) => { try { writeFileAtomic.sync(cachePath, fileData, {encoding: 'utf8'}); } catch (e) { + if (cacheWriteErrorSafeToIgnore(e, cachePath)) { + return; + } + e.message = 'jest: failed to cache transform results in: ' + cachePath + @@ -412,6 +416,20 @@ const writeCacheFile = (cachePath: Path, fileData: string) => { } }; +/** + * On Windows, renames are not atomic, leading to EPERM exceptions when two + * processes attempt to rename to the same target file at the same time. + * If the target file exists we can be reasonably sure another process has + * legitimately won a cache write race and ignore the error. + */ +const cacheWriteErrorSafeToIgnore = (e: Error, cachePath: Path) => { + return ( + process.platform === 'win32' && + e.code === 'EPERM' && + fs.existsSync(cachePath) + ); +}; + const readCacheFile = (cachePath: Path): ?string => { if (!fs.existsSync(cachePath)) { return null;