From 04cee98166d4b495b29454f4e2a774788442ecd0 Mon Sep 17 00:00:00 2001 From: mofeiZ <34200447+mofeiZ@users.noreply.github.com> Date: Thu, 11 May 2023 13:53:06 -0400 Subject: [PATCH] [snap] patch bug in clearing require cache Snap currently has a bug in which the require cache is not correctly cleared when running in filter mode (#tests < 2 * #workers). - We're currently clearing all entries in the require cache of worker threads, including `jest-worker` and `snap/dist/...` files - jest-worker seems to `require` these files on every dispatch (i.e. `worker.compile` seems to call `require(`compiler-worker`).compile`) --- .../packages/snap/src/compiler-worker.ts | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/compiler/forget/packages/snap/src/compiler-worker.ts b/compiler/forget/packages/snap/src/compiler-worker.ts index cc33a06507..ce65610b35 100644 --- a/compiler/forget/packages/snap/src/compiler-worker.ts +++ b/compiler/forget/packages/snap/src/compiler-worker.ts @@ -11,10 +11,26 @@ import { exists } from "./utils"; const originalConsoleError = console.error; +// Subpaths to ignore when clearing the require cache +const ignoredRequireSubpaths: Array = [ + // compiler worker runner files + "node_modules/jest-worker", + // snap source files + "packages/snap", +]; +const ignoredRequirePaths: Set = new Set( + Object.keys(require.cache).filter( + (path) => + !ignoredRequireSubpaths.every((ignored) => !path.includes(ignored)) + ) +); + let version: number | null = null; export function clearRequireCache() { - Object.keys(require.cache).forEach(function (key) { - delete require.cache[key]; + Object.keys(require.cache).forEach(function (path) { + if (!ignoredRequirePaths.has(path)) { + delete require.cache[path]; + } }); }