refactor(utils): rename and refactor clearModuleCache to recursively clear dependencies
The function has been renamed to clearRequireCache and updated to recursively clear the require cache for a module and all its dependencies, preventing cycles with a visited set. This improves reliability after rebuilding native modules by ensuring complete cache invalidation. Removed unused createRequire import as it's no longer needed.
This commit is contained in:
@@ -7,7 +7,6 @@
|
|||||||
import { execSync } from 'child_process';
|
import { execSync } from 'child_process';
|
||||||
import { dirname, join } from 'path';
|
import { dirname, join } from 'path';
|
||||||
import { existsSync } from 'fs';
|
import { existsSync } from 'fs';
|
||||||
import { createRequire } from 'module';
|
|
||||||
import { logger } from './logger.js';
|
import { logger } from './logger.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -121,30 +120,36 @@ export function attemptAutoRebuild(error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clear the require cache for a module to force re-import
|
* Recursively clear a module and its dependencies from the require cache
|
||||||
* This is needed after rebuilding a native module
|
* This is needed after rebuilding a native module to force re-import
|
||||||
* @param {string} moduleName - The module name (e.g., 'better-sqlite3')
|
* @param {string} modulePath - Resolved path to the module
|
||||||
|
* @param {object} cache - The require.cache object
|
||||||
|
* @param {Set} [visited] - Set of already-visited paths to prevent cycles
|
||||||
*/
|
*/
|
||||||
export function clearModuleCache(moduleName) {
|
export function clearRequireCache(modulePath, cache, visited = new Set()) {
|
||||||
const require = createRequire(import.meta.url);
|
if (visited.has(modulePath)) return;
|
||||||
try {
|
visited.add(modulePath);
|
||||||
const resolved = require.resolve(moduleName);
|
|
||||||
// Clear the main module and its dependencies
|
const mod = cache[modulePath];
|
||||||
const mod = require.cache[resolved];
|
if (!mod) return;
|
||||||
if (mod) {
|
|
||||||
|
// Recursively clear children first
|
||||||
|
if (mod.children) {
|
||||||
|
for (const child of mod.children) {
|
||||||
|
clearRequireCache(child.id, cache, visited);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Remove from parent's children array
|
// Remove from parent's children array
|
||||||
if (mod.parent) {
|
if (mod.parent && mod.parent.children) {
|
||||||
const idx = mod.parent.children.indexOf(mod);
|
const idx = mod.parent.children.indexOf(mod);
|
||||||
if (idx !== -1) {
|
if (idx !== -1) {
|
||||||
mod.parent.children.splice(idx, 1);
|
mod.parent.children.splice(idx, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete from cache
|
// Delete from cache
|
||||||
delete require.cache[resolved];
|
delete cache[modulePath];
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// Module might not be in cache, that's okay
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@@ -153,5 +158,5 @@ export default {
|
|||||||
findPackageRoot,
|
findPackageRoot,
|
||||||
rebuildModule,
|
rebuildModule,
|
||||||
attemptAutoRebuild,
|
attemptAutoRebuild,
|
||||||
clearModuleCache
|
clearRequireCache
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user