diff --git a/src/node_modules.cc b/src/node_modules.cc index 38d2c65c7f3282..2841c5815240e6 100644 --- a/src/node_modules.cc +++ b/src/node_modules.cc @@ -100,10 +100,18 @@ const BindingData::PackageConfig* BindingData::GetPackageJSON( return &cache_entry->second; } + if (binding_data->missing_package_configs_.contains(std::string(path))) { + return nullptr; + } + PackageConfig package_config{}; package_config.file_path = path; // No need to exclude BOM since simdjson will skip it. if (ReadFileSync(&package_config.raw_json, path.data()) < 0) { + // Cache the failed read so that other queries in the same folder don't + // keep probing the file system + binding_data->missing_package_configs_.insert( + std::string(path)); return nullptr; } // In some systems, std::string is annotated to generate an diff --git a/src/node_modules.h b/src/node_modules.h index 17909b2270454b..f70cefb1850996 100644 --- a/src/node_modules.h +++ b/src/node_modules.h @@ -74,6 +74,7 @@ class BindingData : public SnapshotableObject { private: std::unordered_map package_configs_; + std::unordered_set missing_package_configs_; simdjson::ondemand::parser json_parser; // returns null on error static const PackageConfig* GetPackageJSON(