[8.x] fix(NA): use filesystem apis on kbn/optimizer populate_bundle_cache plugin (#211231) (#211285)

# Backport

This will backport the following commits from `main` to `8.x`:
- [fix(NA): use filesystem apis on kbn/optimizer populate_bundle_cache
plugin (#211231)](https://github.com/elastic/kibana/pull/211231)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Tiago
Costa","email":"tiago.costa@elastic.co"},"sourceCommit":{"committedDate":"2025-02-14T17:07:41Z","message":"fix(NA):
use filesystem apis on kbn/optimizer populate_bundle_cache plugin
(#211231)\n\nThis PR solves an issue detected in the populate bundle
cache plugin\nafter the webpack v5 migration. On the new version webpack
v5 returns a\nlot of incomplete paths when we walk over file
dependencies or internal\nmodules. The heuristic logic used previously
was faulty so the fixes\nturns to use a cached filesystem api
instead.\n\n---------\n\nCo-authored-by: kibanamachine
<42973632+kibanamachine@users.noreply.github.com>","sha":"16a9136b93e2b9bc2e5d86ff7167a8ae0a563cdb","branchLabelMapping":{"^v9.1.0$":"main","^v8.19.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["chore","Team:Operations","release_note:skip","backport:prev-major","v9.1.0"],"title":"fix(NA):
use filesystem apis on kbn/optimizer populate_bundle_cache
plugin","number":211231,"url":"https://github.com/elastic/kibana/pull/211231","mergeCommit":{"message":"fix(NA):
use filesystem apis on kbn/optimizer populate_bundle_cache plugin
(#211231)\n\nThis PR solves an issue detected in the populate bundle
cache plugin\nafter the webpack v5 migration. On the new version webpack
v5 returns a\nlot of incomplete paths when we walk over file
dependencies or internal\nmodules. The heuristic logic used previously
was faulty so the fixes\nturns to use a cached filesystem api
instead.\n\n---------\n\nCo-authored-by: kibanamachine
<42973632+kibanamachine@users.noreply.github.com>","sha":"16a9136b93e2b9bc2e5d86ff7167a8ae0a563cdb"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/211231","number":211231,"mergeCommit":{"message":"fix(NA):
use filesystem apis on kbn/optimizer populate_bundle_cache plugin
(#211231)\n\nThis PR solves an issue detected in the populate bundle
cache plugin\nafter the webpack v5 migration. On the new version webpack
v5 returns a\nlot of incomplete paths when we walk over file
dependencies or internal\nmodules. The heuristic logic used previously
was faulty so the fixes\nturns to use a cached filesystem api
instead.\n\n---------\n\nCo-authored-by: kibanamachine
<42973632+kibanamachine@users.noreply.github.com>","sha":"16a9136b93e2b9bc2e5d86ff7167a8ae0a563cdb"}}]}]
BACKPORT-->

Co-authored-by: Tiago Costa <tiago.costa@elastic.co>
This commit is contained in:
Kibana Machine 2025-02-15 04:33:07 +11:00 committed by GitHub
parent 3768c353b4
commit 73a5339547
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;
}