Skip to content

fix: add yarn pnp support to read_dir #11124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions crates/rspack_fs/src/native_fs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
fs::{self, File},
io::{BufRead, BufReader, BufWriter, Read, Write},
path::{Path, PathBuf},
};

use pnp::fs::{FileType, LruZipCache, VPath, VPathInfo, ZipCache};
Expand Down Expand Up @@ -231,10 +232,36 @@ impl ReadableFileSystem for NativeFileSystem {
#[instrument(skip(self), level = "debug")]
fn read_dir_sync(&self, dir: &Utf8Path) -> Result<Vec<String>> {
let mut res = vec![];
let dir = if self.options.pnp {
let path = dir.as_std_path();
match VPath::from(path)? {
VPath::Zip(info) => {
self.pnp_lru.act(info.physical_base_path(), |zip| {
for path in zip.dirs.iter().chain(zip.files.keys()) {
let pathbuf = PathBuf::from(path);
if let Some(file_name) = pathbuf.file_name() {
let parent_path = pathbuf.parent().unwrap_or(Path::new("."));
if PathBuf::from(&info.zip_path) == parent_path {
res.push(file_name.to_string_lossy().to_string());
}
}
}
})?;

return Ok(res);
}
VPath::Virtual(info) => info.physical_base_path(),
VPath::Native(path) => path,
}
} else {
dir.into()
};

for entry in fs::read_dir(dir)? {
let entry = entry?;
res.push(entry.file_name().to_string_lossy().to_string());
}

Ok(res)
}
#[instrument(skip(self), level = "debug")]
Expand Down
Loading