fix(vfs): keep a dangling Windows junction from listing as a folder (#7486)

On Windows, a dangling directory junction showed in the Project view as an expandable folder that listed nothing. lstat of a junction reports size 0, and the UI reads a size-0 link as a link to a folder. read_dir now sizes a dangling link by its target's length, which is what lstat already reports on macOS and Linux, so their behaviour is unchanged. A Windows test creates real junctions with mklink /J and lists them the way the Project view does. The Git workflow fixture names its extra clones by a hash so long paths stay under MAX_PATH.

Refs #7419

Co-authored-by: wildcatDownstairs <[email protected]>
Цей коміт міститься в:
LU XIANGRUIтаwildcatDownstairs authored and GitHub committed 2026-12-02 23:27:01 +08:00
1 джерело 3f7b83e8fd
коміт 8ad3fc4faf
3 files changed
+88 -2

No files matched your search

+9 -1
Переглянути файл
@@ -70,7 +70,15 @@ impl Fixture {
}
/// Another clone of `bare` that commits `rel` and pushes main.
fn push_from_other(&self, bare: &Path, rel: &str, content: &str) {
let other = self.tmp.path().join(format!("other-{}", rel.replace('/', "_")));
// Named by a hash of `rel`: a long `rel` repeated in the folder name would push the clone's
// paths past Windows' 260-char `MAX_PATH`, which Git for Windows enforces.
let key = {
use std::hash::{Hash, Hasher};
let mut h = std::collections::hash_map::DefaultHasher::new();
rel.hash(&mut h);
h.finish()
};
let other = self.tmp.path().join(format!("other-{key:016x}"));
if !other.exists() {
let (bare, other_dir) = (bare.to_str().unwrap(), other.to_str().unwrap());
git_in(self.tmp.path(), &["clone", "-q", "-c", "core.autocrlf=false", bare, other_dir]);
+10 -1
Переглянути файл
@@ -188,6 +188,15 @@ pub(crate) fn read_dir_with(
(EntryKind::File, lmeta, None)
};
let is_dir = meta.is_dir();
// A dangling link is sized by its target's length, as `lstat` reports on Unix. A Windows
// junction's own size is 0, which the UI reads as a link to a folder (issue #7419).
let size = match (kind, target_kind) {
(EntryKind::Symlink, None) if meta.len() == 0 => {
fs::read_link(&path).map_or(1, |t| t.as_os_str().len().max(1) as u64)
}
_ if is_dir => 0,
_ => meta.len(),
};
let rel = path.strip_prefix(&base).unwrap_or(&path);
let excluded = excludes.is_excluded(rel);
let ignored = excluded || gitignore.is_ignored(&path, is_dir);
@@ -195,7 +204,7 @@ pub(crate) fn read_dir_with(
is_dir,
FileEntry {
hidden: name.starts_with('.'),
size: if is_dir { 0 } else { meta.len() },
size,
mtime_ms: mtime_ms(&meta),
name,
path: path.into(),
+69
Переглянути файл
@@ -688,3 +688,72 @@ fn a_fallback_without_ascii_only_decodes_non_utf8_text() {
assert_eq!((loaded.text.as_str(), loaded.encoding.as_str()), ("greeting=Ol\u{e1}\n", "windows-1252"));
assert_eq!(detect_encoding(b"greeting=Ol\xe1\n", decode_only), encoding_rs::WINDOWS_1252);
}
/// Issue #7419: pnpm links `node_modules` entries with directory junctions (absolute targets).
#[cfg(windows)]
#[test]
fn windows_junctions_list_like_folders() {
fn junction(link: &Path, target: &Path) {
// `mklink` reads `/` as a switch.
let win = |p: &Path| p.to_string_lossy().replace('/', "\\");
let out =
std::process::Command::new("cmd").args(["/C", "mklink", "/J", &win(link), &win(target)]).output().unwrap();
assert!(out.status.success(), "mklink /J {}: {}", link.display(), String::from_utf8_lossy(&out.stderr));
}
// The UI sends `/`-separated paths.
let ui = |p: &Path| PathBuf::from(p.to_string_lossy().replace('\\', "/"));
let names = |entries: &[FileEntry]| entries.iter().map(|e| e.name.clone()).collect::<Vec<_>>();
let (_t, r) = root();
let (_o, outside) = root();
write(&r.join(".git/HEAD"), "ref: refs/heads/main\n");
write(&r.join(".gitignore"), "node_modules\n");
let store = r.join("node_modules/.pnpm/@[email protected][email protected]/node_modules/@angular/cdk");
write(&store.join("package.json"), "{}");
write(&store.join("a11y/index.d.ts"), "x");
write(&r.join("packages/pkg/index.ts"), "x");
write(&outside.join("shared/readme.md"), "x");
fs::create_dir_all(r.join("node_modules/@angular")).unwrap();
fs::create_dir_all(r.join("node_modules/@scope")).unwrap();
junction(&r.join("node_modules/@angular/cdk"), &store);
junction(&r.join("node_modules/@scope/pkg"), &r.join("packages/pkg"));
junction(&r.join("node_modules/outside"), &outside.join("shared"));
junction(&r.join("node_modules/cycle"), &r.join("node_modules"));
fs::create_dir(r.join("gone")).unwrap();
junction(&r.join("node_modules/dangling"), &r.join("gone"));
fs::remove_dir(r.join("gone")).unwrap();
// A real directory symlink needs Developer Mode or elevation: covered when the runner allows it.
let symlinked = std::os::windows::fs::symlink_dir(r.join("packages/pkg"), r.join("node_modules/sym")).is_ok();
let sink: SharedSink = Arc::new(|_| {});
let watcher = Watcher::start(&r, vec![], sink, Box::new(|_| {})).unwrap();
let list = |dir: &Path| {
let plain = read_dir(&ui(dir), &[]).unwrap_or_else(|e| panic!("read_dir {}: {e}", dir.display()));
let shared = watcher.read_dir(&ui(dir), &[]).unwrap_or_else(|e| panic!("watcher {}: {e}", dir.display()));
assert_eq!(names(&plain), names(&shared));
shared
};
let modules = list(&r.join("node_modules"));
let get = |name: &str| modules.iter().find(|e| e.name == name).unwrap_or_else(|| panic!("{name}: {modules:?}"));
for link in ["cycle", "outside"] {
let e = get(link);
assert_eq!((e.kind, e.target_kind, e.size), (EntryKind::Symlink, Some(EntryKind::Dir), 0), "{link}");
}
let cdk = list(&r.join("node_modules/@angular")).into_iter().find(|e| e.name == "cdk").unwrap();
assert_eq!((cdk.kind, cdk.target_kind, cdk.size), (EntryKind::Symlink, Some(EntryKind::Dir), 0));
assert_eq!(names(&list(&r.join("node_modules/@angular/cdk"))), ["a11y", "package.json"]);
assert_eq!(names(&list(&r.join("node_modules/@angular/cdk/a11y"))), ["index.d.ts"]);
assert_eq!(names(&list(&r.join("node_modules/@scope/pkg"))), ["index.ts"]);
assert_eq!(names(&list(&r.join("node_modules/outside"))), ["readme.md"]);
assert!(names(&list(&r.join("node_modules/cycle/cycle/@angular"))).contains(&"cdk".to_string()));
assert!(read_dir(&r.join("node_modules/dangling"), &[]).is_err());
if symlinked {
assert_eq!(names(&list(&r.join("node_modules/sym"))), ["index.ts"]);
}
// Dangling: not a folder for the UI (it treats a size-0 link as one).
let dangling = get("dangling");
assert_eq!((dangling.kind, dangling.target_kind), (EntryKind::Symlink, None));
assert_ne!(dangling.size, 0, "a dangling junction must not look like a folder");
}