Skip to content

Commit e1ca87b

Browse files
elrrrrrrrclaude
andcommitted
refactor: simplify cross-drive cache dir detection
Use cwd.ancestors().last() to get drive root instead of manual component extraction. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 7af378c commit e1ca87b

2 files changed

Lines changed: 19 additions & 13 deletions

File tree

crates/pm/src/util/platform_const.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//! Platform-specific constants for cross-platform compatibility.
22
3+
use std::ffi::OsString;
4+
use std::path::Path;
5+
36
/// PATH environment variable separator.
47
/// Windows uses `;`, Unix uses `:`.
58
pub const PATH_SEPARATOR: &str = if cfg!(windows) { ";" } else { ":" };
@@ -12,3 +15,11 @@ pub const GLOBAL_NODE_MODULES: &str = if cfg!(windows) {
1215
} else {
1316
"lib/node_modules"
1417
};
18+
19+
/// Extract the drive root component (e.g. `C:`) from a path, uppercased for comparison.
20+
/// Returns `None` on Unix or if the path has no components.
21+
pub fn drive_root(p: &Path) -> Option<OsString> {
22+
p.components()
23+
.next()
24+
.map(|c| c.as_os_str().to_ascii_uppercase())
25+
}

crates/pm/src/util/user_config.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use std::collections::HashSet;
2+
use std::env;
23
use std::path::{Path, PathBuf};
34
use std::sync::{LazyLock, OnceLock};
45

6+
use super::platform_const::drive_root;
7+
58
use anyhow::Result;
69
use dashmap::DashMap;
710
use utoo_ruborist::builder::PeerDeps;
@@ -142,20 +145,12 @@ pub async fn set_cache_dir(cache_dir: Option<String>) {
142145
pub fn get_cache_dir() -> PathBuf {
143146
let cache = PathBuf::from(CACHE_DIR.get_sync());
144147

145-
// On Windows, hardlinks cannot cross drive boundaries.
146-
// If cache (e.g. C:\.cache\nm) is on a different drive than cwd (e.g. D:\project),
147-
// use a cache dir on the project's drive instead.
148+
// On Windows, hardlinks cannot cross drive boundaries (e.g. cache on C:, project on D:).
149+
// Fall back to a cache dir on the project's drive.
148150
#[cfg(windows)]
149-
if let Ok(cwd) = std::env::current_dir() {
150-
fn drive_prefix(p: &Path) -> Option<std::ffi::OsString> {
151-
p.components()
152-
.next()
153-
.map(|c| c.as_os_str().to_ascii_uppercase())
154-
}
155-
if drive_prefix(&cache) != drive_prefix(&cwd) {
156-
if let Some(drive) = cwd.components().next() {
157-
return PathBuf::from(drive.as_os_str()).join(".cache").join("nm");
158-
}
151+
if let Ok(cwd) = env::current_dir() {
152+
if drive_root(&cache) != drive_root(&cwd) {
153+
return cwd.ancestors().last().unwrap_or(&cwd).join(".cache/nm");
159154
}
160155
}
161156

0 commit comments

Comments
 (0)