Repository: vercel/next.js
Pull request: Remove uses of `read_glob` from `node_native_bindings`

Remove two calls to `read_glob` that could be trivially satisfied by `read_dir`.  This makes it clearer that the reads are shallow and avoids allocating extra maps and `globs` (smaller vc-cell keys, etc)


Closes PACK-4606

Changed files:
filename: turbopack/crates/turbopack-core/src/target.rs
patch: @@ -60,12 +60,13 @@ impl CompileTarget {
         }
     }
 
+    /// Returns the expected extension of the dynamic library, including the `.`.
     pub fn dylib_ext(&self) -> &'static str {
         let platform = self.platform;
         match platform {
-            Platform::Win32 => "dll",
-            Platform::Darwin => "dylib",
-            _ => "so",
+            Platform::Win32 => ".dll",
+            Platform::Darwin => ".dylib",
+            _ => ".so",
         }
     }
 

filename: turbopack/crates/turbopack-resolve/src/node_native_binding.rs
patch: @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
 use turbo_rcstr::RcStr;
 use turbo_tasks::{FxIndexMap, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, ValueToString, Vc};
 use turbo_tasks_fs::{
-    DirectoryEntry, FileContent, FileSystemEntryType, FileSystemPath, glob::Glob,
+    DirectoryContent, DirectoryEntry, FileContent, FileSystemEntryType, FileSystemPath,
     json::parse_json_rope_with_source_context,
 };
 use turbopack_core::{
@@ -132,23 +132,22 @@ pub async fn resolve_node_pre_gyp_files(
                         )
                         .into();
 
-                    for (key, entry) in config_file_dir
+                    // Find all dynamic libraries in the given directory.
+                    if let DirectoryContent::Entries(entries) = &*config_file_dir
                         .join(native_binding_path.clone())
-                        .read_glob(
-                            Glob::new(format!("*.{}", compile_target.dylib_ext()).into()),
-                            false,
-                        )
+                        .read_dir()
                         .await?
-                        .results
-                        .iter()
                     {
-                        if let &DirectoryEntry::File(dylib) | &DirectoryEntry::Symlink(dylib) =
-                            entry
-                        {
-                            sources.insert(
-                                format!("{native_binding_path}/{key}").into(),
-                                Vc::upcast(FileSource::new(*dylib)),
-                            );
+                        let extension = compile_target.dylib_ext();
+                        for (key, entry) in entries.iter().filter(|(k, _)| k.ends_with(extension)) {
+                            if let &DirectoryEntry::File(dylib) | &DirectoryEntry::Symlink(dylib) =
+                                entry
+                            {
+                                sources.insert(
+                                    format!("{native_binding_path}/{key}").into(),
+                                    Vc::upcast(FileSource::new(*dylib)),
+                                );
+                            }
                         }
                     }
 
@@ -165,33 +164,33 @@ pub async fn resolve_node_pre_gyp_files(
                         );
                     }
                 }
-                for (key, entry) in config_file_dir
+                if let DirectoryContent::Entries(entries) = &*config_file_dir
                     // TODO
                     // read the dependencies path from `bindings.gyp`
                     .join("deps/lib".into())
-                    .read_glob(Glob::new("*".into()), false)
+                    .read_dir()
                     .await?
-                    .results
-                    .iter()
                 {
-                    match *entry {
-                        DirectoryEntry::File(dylib) => {
-                            sources.insert(
-                                format!("deps/lib/{key}").into(),
-                                Vc::upcast(FileSource::new(*dylib)),
-                            );
-                        }
-                        DirectoryEntry::Symlink(dylib) => {
-                            let realpath_with_links = dylib.realpath_with_links().await?;
-                            for &symlink in realpath_with_links.symlinks.iter() {
-                                affecting_paths.push(*symlink);
+                    for (key, entry) in entries.iter() {
+                        match *entry {
+                            DirectoryEntry::File(dylib) => {
+                                sources.insert(
+                                    format!("deps/lib/{key}").into(),
+                                    Vc::upcast(FileSource::new(*dylib)),
+                                );
+                            }
+                            DirectoryEntry::Symlink(dylib) => {
+                                let realpath_with_links = dylib.realpath_with_links().await?;
+                                for &symlink in realpath_with_links.symlinks.iter() {
+                                    affecting_paths.push(*symlink);
+                                }
+                                sources.insert(
+                                    format!("deps/lib/{key}").into(),
+                                    Vc::upcast(FileSource::new(*realpath_with_links.path)),
+                                );
                             }
-                            sources.insert(
-                                format!("deps/lib/{key}").into(),
-                                Vc::upcast(FileSource::new(*realpath_with_links.path)),
-                            );
+                            _ => {}
                         }
-                        _ => {}
                     }
                 }
                 return Ok(*ModuleResolveResult::modules_with_affecting_sources(