fix(NA): use filesystem apis on kbn/optimizer populate_bundle_cache plugin (#211231)

This PR solves an issue detected in the populate bundle cache plugin
after the webpack v5 migration. On the new version webpack v5 returns a
lot of incomplete paths when we walk over file dependencies or internal
modules. The heuristic logic used previously was faulty so the fixes
turns to use a cached filesystem api instead.

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Tiago Costa 2025-02-14 17:07:41 +00:00 committed by GitHub
parent 44ef445f8a
commit 16a9136b93
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -37,6 +37,7 @@ interface InputFileSystem {
encoding: null | undefined,
callback: (err: Error | null, stats: Buffer) => void
) => void;
statSync: (path: string) => any;
}
/**
@ -48,6 +49,22 @@ interface InputFileSystem {
*/
const EXTRA_SCSS_WORK_UNITS = 100;
const fileCheckCache = new Map();
function isFile(inputFileSystem: InputFileSystem, path: string) {
if (fileCheckCache.has(path)) {
return fileCheckCache.get(path);
}
try {
const result = inputFileSystem.statSync(path).isFile();
fileCheckCache.set(path, result);
return result;
} catch (err) {
fileCheckCache.set(path, false);
return false; // Path does not exist or is not a file
}
}
export class PopulateBundleCachePlugin {
constructor(
private readonly workerConfig: WorkerConfig,
@ -78,7 +95,7 @@ export class PopulateBundleCachePlugin {
// in webpack v5 there a lot of paths collected that are not real files
// but instead folders or partial paths.
// Here we're verifying if what we have as indeed a filepath
if (Path.extname(path).length > 0) {
if (isFile(inputFs, path)) {
realFileDeps.push(path);
allFileDepsPathSet.add(path);
}
@ -127,7 +144,7 @@ export class PopulateBundleCachePlugin {
for (const module of compilation.modules) {
if (isNormalModule(module)) {
const path = getModulePath(module);
if (Path.extname(path).length === 0) {
if (!isFile(inputFs, path)) {
continue;
}