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

fix: eliminate race in compilation cache #26353

Merged
merged 2 commits into from
Aug 8, 2023
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
10 changes: 10 additions & 0 deletions packages/playwright-core/src/utils/fileUtils.ts
Original file line number Diff line number Diff line change
@@ -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)));

@@ -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
@@ -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;
@@ -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 });
}
};
2 changes: 1 addition & 1 deletion packages/playwright-test/src/transform/transform.ts
Original file line number Diff line number Diff line change
@@ -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.