From 7102c7d5ade6bc70fd277139da54953c4833b762 Mon Sep 17 00:00:00 2001 From: vanuan Date: Fri, 12 May 2017 18:13:45 +0300 Subject: [PATCH] Fix #1874 race condition Use file locking to fix the race condition when writing the cache --- packages/jest-runtime/package.json | 1 + packages/jest-runtime/src/ScriptTransformer.js | 7 +++++++ packages/jest-runtime/yarn.lock | 11 +++++++++++ 3 files changed, 19 insertions(+) diff --git a/packages/jest-runtime/package.json b/packages/jest-runtime/package.json index ddf33d2a5b8c..1e9b6fe05d13 100644 --- a/packages/jest-runtime/package.json +++ b/packages/jest-runtime/package.json @@ -21,6 +21,7 @@ "jest-util": "^20.0.1", "json-stable-stringify": "^1.0.1", "micromatch": "^2.3.11", + "proper-lockfile": "^2.0.1", "strip-bom": "3.0.0", "yargs": "^7.0.2" }, diff --git a/packages/jest-runtime/src/ScriptTransformer.js b/packages/jest-runtime/src/ScriptTransformer.js index 5d561145017f..88b321c824b3 100644 --- a/packages/jest-runtime/src/ScriptTransformer.js +++ b/packages/jest-runtime/src/ScriptTransformer.js @@ -20,6 +20,7 @@ const path = require('path'); const vm = require('vm'); const {createDirectory} = require('jest-util'); const fs = require('graceful-fs'); +const lockfile = require('proper-lockfile'); const {getCacheFilePath} = require('jest-haste-map'); const stableStringify = require('json-stable-stringify'); const slash = require('slash'); @@ -342,8 +343,11 @@ const stripShebang = content => { }; const writeCacheFile = (cachePath: Path, fileData: string) => { + let release; try { + release = lockfile.lockSync(cachePath, {realpath: false}); fs.writeFileSync(cachePath, fileData, 'utf8'); + release(); } catch (e) { e.message = 'jest: failed to cache transform results in: ' + @@ -351,6 +355,9 @@ const writeCacheFile = (cachePath: Path, fileData: string) => { '\nFailure message: ' + e.message; removeFile(cachePath); + if (release) { + release(); + } throw e; } }; diff --git a/packages/jest-runtime/yarn.lock b/packages/jest-runtime/yarn.lock index c0121df49acf..4eaff6853f6d 100644 --- a/packages/jest-runtime/yarn.lock +++ b/packages/jest-runtime/yarn.lock @@ -654,6 +654,13 @@ private@^0.1.6: version "0.1.7" resolved "https://registry.yarnpkg.com/private/-/private-0.1.7.tgz#68ce5e8a1ef0a23bb570cc28537b5332aba63ef1" +proper-lockfile@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/proper-lockfile/-/proper-lockfile-2.0.1.tgz#159fb06193d32003f4b3691dd2ec1a634aa80d1d" + dependencies: + graceful-fs "^4.1.2" + retry "^0.10.0" + randomatic@^1.1.3: version "1.1.6" resolved "https://registry.yarnpkg.com/randomatic/-/randomatic-1.1.6.tgz#110dcabff397e9dcff7c0789ccc0a49adf1ec5bb" @@ -713,6 +720,10 @@ require-main-filename@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/require-main-filename/-/require-main-filename-1.0.1.tgz#97f717b69d48784f5f526a6c5aa8ffdda055a4d1" +retry@^0.10.0: + version "0.10.1" + resolved "https://registry.yarnpkg.com/retry/-/retry-0.10.1.tgz#e76388d217992c252750241d3d3956fed98d8ff4" + "semver@2 || 3 || 4 || 5", semver@^5.3.0: version "5.3.0" resolved "https://registry.yarnpkg.com/semver/-/semver-5.3.0.tgz#9b2ce5d3de02d17c6012ad326aa6b4d0cf54f94f"