Skip to content

Commit

Permalink
fix: eliminate race in compilation cache
Browse files Browse the repository at this point in the history
Fixes #24569
  • Loading branch information
aslushnikov committed Aug 8, 2023
1 parent 0e20d35 commit cae9985
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
10 changes: 10 additions & 0 deletions packages/playwright-core/src/utils/fileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*/

import fs from 'fs';
import type { WriteFileOptions } from 'fs';
import path from 'path';
import { rimraf } from '../utilsBundle';
import { createGuid } from './crypto';

export const existsAsync = (path: string): Promise<boolean> => new Promise(resolve => fs.stat(path, err => resolve(!err)));

Expand Down Expand Up @@ -53,3 +55,11 @@ export async function copyFileAndMakeWritable(from: string, to: string) {
export function sanitizeForFilePath(s: string) {
return s.replace(/[\x00-\x2C\x2E-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+/g, '-');
}

export function writeFileSyncAtomic(aPath: string, data: Buffer | string, options: WriteFileOptions) {
const dirName = path.dirname(aPath);
const fileName = path.basename(aPath);
const tmpPath = path.join(dirName, fileName + '-' + createGuid());
fs.writeFileSync(tmpPath, data, options);
fs.renameSync(tmpPath, aPath);
}
5 changes: 3 additions & 2 deletions packages/playwright-test/src/transform/compilationCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import fs from 'fs';
import os from 'os';
import path from 'path';
import { sourceMapSupport } from '../utilsBundle';
import { writeFileSyncAtomic } from 'playwright-core/lib/utils';

export type MemoryCache = {
codePath: string;
Expand Down Expand Up @@ -85,8 +86,8 @@ export function getFromCompilationCache(filename: string, hash: string, moduleUr
addToCache: (code: string, map: any) => {
fs.mkdirSync(path.dirname(cachePath), { recursive: true });
if (map)
fs.writeFileSync(sourceMapPath, JSON.stringify(map), 'utf8');
fs.writeFileSync(codePath, code, 'utf8');
writeFileSyncAtomic(sourceMapPath, JSON.stringify(map), 'utf8');
writeFileSyncAtomic(codePath, code, 'utf8');
_innerAddToCompilationCache(filename, { codePath, sourceMapPath, moduleUrl });
}
};
Expand Down
2 changes: 1 addition & 1 deletion packages/playwright-test/src/transform/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export function transformHook(originalCode: string, filename: string, moduleUrl?
const pluginsEpilogue = hasPreprocessor ? [[process.env.PW_TEST_SOURCE_TRANSFORM!]] as BabelPlugin[] : [];
const hash = calculateHash(originalCode, filename, !!moduleUrl, pluginsPrologue, pluginsEpilogue);
const { cachedCode, addToCache } = getFromCompilationCache(filename, hash, moduleUrl);
if (cachedCode)
if (cachedCode !== undefined)
return cachedCode;

// We don't use any browserslist data, but babel checks it anyway.
Expand Down

0 comments on commit cae9985

Please sign in to comment.