mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
fix: eliminate race in compilation cache (#26353)
Fixes https://github.com/microsoft/playwright/issues/24569
This commit is contained in:
parent
44f9b10ff2
commit
ffd6cf60eb
@ -15,8 +15,10 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
import type { WriteFileOptions } from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { rimraf } from '../utilsBundle';
|
import { rimraf } from '../utilsBundle';
|
||||||
|
import { createGuid } from './crypto';
|
||||||
|
|
||||||
export const existsAsync = (path: string): Promise<boolean> => new Promise(resolve => fs.stat(path, err => resolve(!err)));
|
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) {
|
export function sanitizeForFilePath(s: string) {
|
||||||
return s.replace(/[\x00-\x2C\x2E-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+/g, '-');
|
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);
|
||||||
|
}
|
||||||
|
@ -18,6 +18,7 @@ import fs from 'fs';
|
|||||||
import os from 'os';
|
import os from 'os';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { sourceMapSupport } from '../utilsBundle';
|
import { sourceMapSupport } from '../utilsBundle';
|
||||||
|
import { writeFileSyncAtomic } from 'playwright-core/lib/utils';
|
||||||
|
|
||||||
export type MemoryCache = {
|
export type MemoryCache = {
|
||||||
codePath: string;
|
codePath: string;
|
||||||
@ -85,8 +86,8 @@ export function getFromCompilationCache(filename: string, hash: string, moduleUr
|
|||||||
addToCache: (code: string, map: any) => {
|
addToCache: (code: string, map: any) => {
|
||||||
fs.mkdirSync(path.dirname(cachePath), { recursive: true });
|
fs.mkdirSync(path.dirname(cachePath), { recursive: true });
|
||||||
if (map)
|
if (map)
|
||||||
fs.writeFileSync(sourceMapPath, JSON.stringify(map), 'utf8');
|
writeFileSyncAtomic(sourceMapPath, JSON.stringify(map), 'utf8');
|
||||||
fs.writeFileSync(codePath, code, 'utf8');
|
writeFileSyncAtomic(codePath, code, 'utf8');
|
||||||
_innerAddToCompilationCache(filename, { codePath, sourceMapPath, moduleUrl });
|
_innerAddToCompilationCache(filename, { codePath, sourceMapPath, moduleUrl });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -166,7 +166,7 @@ export function transformHook(originalCode: string, filename: string, moduleUrl?
|
|||||||
const pluginsEpilogue = hasPreprocessor ? [[process.env.PW_TEST_SOURCE_TRANSFORM!]] as BabelPlugin[] : [];
|
const pluginsEpilogue = hasPreprocessor ? [[process.env.PW_TEST_SOURCE_TRANSFORM!]] as BabelPlugin[] : [];
|
||||||
const hash = calculateHash(originalCode, filename, !!moduleUrl, pluginsPrologue, pluginsEpilogue);
|
const hash = calculateHash(originalCode, filename, !!moduleUrl, pluginsPrologue, pluginsEpilogue);
|
||||||
const { cachedCode, addToCache } = getFromCompilationCache(filename, hash, moduleUrl);
|
const { cachedCode, addToCache } = getFromCompilationCache(filename, hash, moduleUrl);
|
||||||
if (cachedCode)
|
if (cachedCode !== undefined)
|
||||||
return cachedCode;
|
return cachedCode;
|
||||||
|
|
||||||
// We don't use any browserslist data, but babel checks it anyway.
|
// We don't use any browserslist data, but babel checks it anyway.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user