-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcache.rs
487 lines (444 loc) · 16.9 KB
/
cache.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
use std::{
borrow::Cow,
cell::RefCell,
convert::AsRef,
hash::{BuildHasherDefault, Hash, Hasher},
io,
ops::Deref,
path::{Component, Path, PathBuf},
sync::{
atomic::{AtomicU64, Ordering},
Arc,
},
};
use cfg_if::cfg_if;
use dashmap::{DashMap, DashSet};
use once_cell::sync::OnceCell as OnceLock;
use rustc_hash::FxHasher;
use crate::{
context::ResolveContext as Ctx, package_json::PackageJson, path::PathUtil, FileMetadata,
FileSystem, ResolveError, ResolveOptions, TsConfig,
};
static THREAD_COUNT: AtomicU64 = AtomicU64::new(1);
thread_local! {
/// Per-thread pre-allocated path that is used to perform operations on paths more quickly.
/// Learned from parcel <https://github.com/parcel-bundler/parcel/blob/a53f8f3ba1025c7ea8653e9719e0a61ef9717079/crates/parcel-resolver/src/cache.rs#L394>
pub static SCRATCH_PATH: RefCell<PathBuf> = RefCell::new(PathBuf::with_capacity(256));
pub static THREAD_ID: u64 = THREAD_COUNT.fetch_add(1, Ordering::SeqCst);
}
#[derive(Default)]
pub struct Cache<Fs> {
pub(crate) fs: Fs,
paths: DashSet<PathEntry<'static>, BuildHasherDefault<IdentityHasher>>,
tsconfigs: DashMap<PathBuf, Arc<TsConfig>, BuildHasherDefault<FxHasher>>,
}
/// An entry in the path cache. Can also be borrowed for lookups without allocations.
enum PathEntry<'a> {
Owned(CachedPath),
Borrowed { hash: u64, path: &'a Path },
}
impl Hash for PathEntry<'_> {
fn hash<H: Hasher>(&self, state: &mut H) {
match self {
PathEntry::Owned(entry) => {
entry.hash.hash(state);
}
PathEntry::Borrowed { hash, .. } => {
hash.hash(state);
}
}
}
}
impl PartialEq for PathEntry<'_> {
fn eq(&self, other: &Self) -> bool {
let self_path = match self {
PathEntry::Owned(info) => &info.path,
PathEntry::Borrowed { path, .. } => *path,
};
let other_path = match other {
PathEntry::Owned(info) => &info.path,
PathEntry::Borrowed { path, .. } => *path,
};
self_path.as_os_str() == other_path.as_os_str()
}
}
impl Eq for PathEntry<'_> {}
impl<Fs: FileSystem> Cache<Fs> {
pub fn new(fs: Fs) -> Self {
Self { fs, paths: DashSet::default(), tsconfigs: DashMap::default() }
}
pub fn clear(&self) {
self.paths.clear();
self.tsconfigs.clear();
}
#[allow(clippy::cast_possible_truncation)]
pub fn value(&self, path: &Path) -> CachedPath {
// `Path::hash` is slow: https://doc.rust-lang.org/std/path/struct.Path.html#impl-Hash-for-Path
// `path.as_os_str()` hash is not stable because we may joined a path like `foo/bar` and `foo\\bar` on windows.
let hash = {
let mut hasher = FxHasher::default();
path.as_os_str().hash(&mut hasher);
hasher.finish()
};
let key = PathEntry::Borrowed { hash, path };
// A DashMap is just an array of RwLock<HashSet>, sharded by hash to reduce lock contention.
// This uses the low level raw API to avoid cloning the value when using the `entry` method.
// First, find which shard the value is in, and check to see if we already have a value in the map.
let shard = self.paths.determine_shard(hash as usize);
{
// Scope the read lock.
let map = self.paths.shards()[shard].read();
if let Some((PathEntry::Owned(entry), _)) = map.get(hash, |v| v.0 == key) {
return entry.clone();
}
}
let parent = path.parent().map(|p| self.value(p));
let cached_path = CachedPath(Arc::new(CachedPathImpl::new(
hash,
path.to_path_buf().into_boxed_path(),
parent,
)));
self.paths.insert(PathEntry::Owned(cached_path.clone()));
cached_path
}
pub fn tsconfig<F: FnOnce(&mut TsConfig) -> Result<(), ResolveError>>(
&self,
root: bool,
path: &Path,
callback: F, // callback for modifying tsconfig with `extends`
) -> Result<Arc<TsConfig>, ResolveError> {
if let Some(tsconfig_ref) = self.tsconfigs.get(path) {
return Ok(Arc::clone(tsconfig_ref.value()));
}
let meta = self.fs.metadata(path).ok();
let tsconfig_path = if meta.is_some_and(|m| m.is_file) {
Cow::Borrowed(path)
} else if meta.is_some_and(|m| m.is_dir) {
Cow::Owned(path.join("tsconfig.json"))
} else {
let mut os_string = path.to_path_buf().into_os_string();
os_string.push(".json");
Cow::Owned(PathBuf::from(os_string))
};
let mut tsconfig_string = self
.fs
.read_to_string(&tsconfig_path)
.map_err(|_| ResolveError::TsconfigNotFound(path.to_path_buf()))?;
let mut tsconfig =
TsConfig::parse(root, &tsconfig_path, &mut tsconfig_string).map_err(|error| {
ResolveError::from_serde_json_error(tsconfig_path.to_path_buf(), &error)
})?;
callback(&mut tsconfig)?;
let tsconfig = Arc::new(tsconfig.build());
self.tsconfigs.insert(path.to_path_buf(), Arc::clone(&tsconfig));
Ok(tsconfig)
}
}
#[derive(Clone)]
pub struct CachedPath(Arc<CachedPathImpl>);
pub struct CachedPathImpl {
hash: u64,
path: Box<Path>,
parent: Option<CachedPath>,
meta: OnceLock<Option<FileMetadata>>,
canonicalized: OnceLock<Result<CachedPath, ResolveError>>,
canonicalizing: AtomicU64,
node_modules: OnceLock<Option<CachedPath>>,
package_json: OnceLock<Option<(CachedPath, Arc<PackageJson>)>>,
}
impl CachedPathImpl {
const fn new(hash: u64, path: Box<Path>, parent: Option<CachedPath>) -> Self {
Self {
hash,
path,
parent,
meta: OnceLock::new(),
canonicalized: OnceLock::new(),
canonicalizing: AtomicU64::new(0),
node_modules: OnceLock::new(),
package_json: OnceLock::new(),
}
}
}
impl Deref for CachedPath {
type Target = CachedPathImpl;
fn deref(&self) -> &Self::Target {
self.0.as_ref()
}
}
impl CachedPath {
pub fn path(&self) -> &Path {
&self.0.path
}
pub fn to_path_buf(&self) -> PathBuf {
self.path.to_path_buf()
}
pub fn parent(&self) -> Option<&Self> {
self.0.parent.as_ref()
}
fn meta<Fs: FileSystem>(&self, fs: &Fs) -> Option<FileMetadata> {
*self.meta.get_or_init(|| fs.metadata(&self.path).ok())
}
pub fn is_file<Fs: FileSystem>(&self, fs: &Fs, ctx: &mut Ctx) -> bool {
if let Some(meta) = self.meta(fs) {
ctx.add_file_dependency(self.path());
meta.is_file
} else {
ctx.add_missing_dependency(self.path());
false
}
}
pub fn is_dir<Fs: FileSystem>(&self, fs: &Fs, ctx: &mut Ctx) -> bool {
self.meta(fs).map_or_else(
|| {
ctx.add_missing_dependency(self.path());
false
},
|meta| meta.is_dir,
)
}
pub fn canonicalize<Fs: FileSystem>(&self, cache: &Cache<Fs>) -> Result<PathBuf, ResolveError> {
let cached_path = self.canocalize_impl(cache)?;
let path = cached_path.to_path_buf();
cfg_if! {
if #[cfg(windows)] {
let path = crate::FileSystemOs::strip_windows_prefix(path);
}
}
Ok(path)
}
/// Returns the canonical path, resolving all symbolic links.
///
/// <https://github.com/parcel-bundler/parcel/blob/4d27ec8b8bd1792f536811fef86e74a31fa0e704/crates/parcel-resolver/src/cache.rs#L232>
fn canocalize_impl<Fs: FileSystem>(&self, cache: &Cache<Fs>) -> Result<Self, ResolveError> {
// Check if this thread is already canonicalizing. If so, we have found a circular symlink.
// If a different thread is canonicalizing, OnceLock will queue this thread to wait for the result.
let tid = THREAD_ID.with(|t| *t);
if self.0.canonicalizing.load(Ordering::Acquire) == tid {
return Err(io::Error::new(io::ErrorKind::NotFound, "Circular symlink").into());
}
self.0
.canonicalized
.get_or_init(|| {
self.0.canonicalizing.store(tid, Ordering::Release);
let res = self.parent().map_or_else(
|| Ok(self.normalize_root(cache)),
|parent| {
parent.canocalize_impl(cache).and_then(|parent_canonical| {
let path = parent_canonical.normalize_with(
self.path().strip_prefix(parent.path()).unwrap(),
cache,
);
if cache.fs.symlink_metadata(self.path()).is_ok_and(|m| m.is_symlink) {
let link = cache.fs.read_link(path.path())?;
if link.is_absolute() {
return cache.value(&link.normalize()).canocalize_impl(cache);
} else if let Some(dir) = path.parent() {
// Symlink is relative `../../foo.js`, use the path directory
// to resolve this symlink.
return dir.normalize_with(&link, cache).canocalize_impl(cache);
}
debug_assert!(
false,
"Failed to get path parent for {:?}.",
path.path()
);
}
Ok(path)
})
},
);
self.0.canonicalizing.store(0, Ordering::Release);
res
})
.clone()
}
pub fn module_directory<Fs: FileSystem>(
&self,
module_name: &str,
cache: &Cache<Fs>,
ctx: &mut Ctx,
) -> Option<Self> {
let cached_path = cache.value(&self.path.join(module_name));
cached_path.is_dir(&cache.fs, ctx).then_some(cached_path)
}
pub fn cached_node_modules<Fs: FileSystem>(
&self,
cache: &Cache<Fs>,
ctx: &mut Ctx,
) -> Option<Self> {
self.node_modules.get_or_init(|| self.module_directory("node_modules", cache, ctx)).clone()
}
/// Get package.json of the given path.
///
/// # Errors
///
/// * [ResolveError::JSON]
pub fn package_json<Fs: FileSystem>(
&self,
options: &ResolveOptions,
cache: &Cache<Fs>,
ctx: &mut Ctx,
) -> Result<Option<(Self, Arc<PackageJson>)>, ResolveError> {
// Change to `std::sync::OnceLock::get_or_try_init` when it is stable.
let result = self
.package_json
.get_or_try_init(|| {
let package_json_path = self.path.join("package.json");
let Ok(package_json_string) = cache.fs.read_to_string(&package_json_path) else {
return Ok(None);
};
let real_path = if options.symlinks {
self.canonicalize(cache)?.join("package.json")
} else {
package_json_path.clone()
};
PackageJson::parse(package_json_path.clone(), real_path, &package_json_string)
.map(|package_json| Some((self.clone(), (Arc::new(package_json)))))
.map_err(|error| ResolveError::from_serde_json_error(package_json_path, &error))
})
.cloned();
// https://github.com/webpack/enhanced-resolve/blob/58464fc7cb56673c9aa849e68e6300239601e615/lib/DescriptionFileUtils.js#L68-L82
match &result {
Ok(Some((_, package_json))) => {
ctx.add_file_dependency(&package_json.path);
}
Ok(None) => {
// Avoid an allocation by making this lazy
if let Some(deps) = &mut ctx.missing_dependencies {
deps.push(self.path.join("package.json"));
}
}
Err(_) => {
if let Some(deps) = &mut ctx.file_dependencies {
deps.push(self.path.join("package.json"));
}
}
}
result
}
/// Find package.json of a path by traversing parent directories.
///
/// # Errors
///
/// * [ResolveError::JSON]
pub fn find_package_json<Fs: FileSystem>(
&self,
options: &ResolveOptions,
cache: &Cache<Fs>,
ctx: &mut Ctx,
) -> Result<Option<(Self, Arc<PackageJson>)>, ResolveError> {
let mut cache_value = self;
// Go up directories when the querying path is not a directory
while !cache_value.is_dir(&cache.fs, ctx) {
if let Some(cv) = &cache_value.parent {
cache_value = cv;
} else {
break;
}
}
let mut cache_value = Some(cache_value);
while let Some(cv) = cache_value {
if let Some(package_json) = cv.package_json(options, cache, ctx)? {
return Ok(Some(package_json));
}
cache_value = cv.parent.as_ref();
}
Ok(None)
}
pub fn add_extension<Fs: FileSystem>(&self, ext: &str, cache: &Cache<Fs>) -> Self {
SCRATCH_PATH.with_borrow_mut(|path| {
path.clear();
let s = path.as_mut_os_string();
s.push(self.path.as_os_str());
s.push(ext);
cache.value(path)
})
}
pub fn replace_extension<Fs: FileSystem>(&self, ext: &str, cache: &Cache<Fs>) -> Self {
SCRATCH_PATH.with_borrow_mut(|path| {
path.clear();
let s = path.as_mut_os_string();
let self_len = self.path.as_os_str().len();
let self_bytes = self.path.as_os_str().as_encoded_bytes();
let slice_to_copy = self.path.extension().map_or(self_bytes, |previous_extension| {
&self_bytes[..self_len - previous_extension.len() - 1]
});
// SAFETY: ???
s.push(unsafe { std::ffi::OsStr::from_encoded_bytes_unchecked(slice_to_copy) });
s.push(ext);
cache.value(path)
})
}
/// Returns a new path by resolving the given subpath (including "." and ".." components) with this path.
pub fn normalize_with<P, Fs>(&self, subpath: P, cache: &Cache<Fs>) -> Self
where
P: AsRef<Path>,
Fs: FileSystem,
{
let subpath = subpath.as_ref();
let mut components = subpath.components();
let Some(head) = components.next() else { return cache.value(subpath) };
if matches!(head, Component::Prefix(..) | Component::RootDir) {
return cache.value(subpath);
}
SCRATCH_PATH.with_borrow_mut(|path| {
path.clear();
path.push(&self.path);
for component in std::iter::once(head).chain(components) {
match component {
Component::CurDir => {}
Component::ParentDir => {
path.pop();
}
Component::Normal(c) => {
cfg_if! {
if #[cfg(target_family = "wasm")] {
// Need to trim the extra \0 introduces by https://github.com/nodejs/uvwasi/issues/262
path.push(c.to_string_lossy().trim_end_matches('\0'));
} else {
path.push(c);
}
}
}
Component::Prefix(..) | Component::RootDir => {
unreachable!("Path {:?} Subpath {:?}", self.path, subpath)
}
}
}
cache.value(path)
})
}
#[inline]
#[cfg(windows)]
pub fn normalize_root<Fs: FileSystem>(&self, cache: &Cache<Fs>) -> Self {
if self.path().as_os_str().as_encoded_bytes().last() == Some(&b'/') {
let mut path_string = self.path.to_string_lossy().into_owned();
path_string.pop();
path_string.push('\\');
cache.value(&PathBuf::from(path_string))
} else {
self.clone()
}
}
#[inline]
#[cfg(not(windows))]
pub fn normalize_root<Fs: FileSystem>(&self, _cache: &Cache<Fs>) -> Self {
self.clone()
}
}
/// Since the cache key is memoized, use an identity hasher
/// to avoid double cache.
#[derive(Default)]
struct IdentityHasher(u64);
impl Hasher for IdentityHasher {
fn write(&mut self, _: &[u8]) {
unreachable!("Invalid use of IdentityHasher")
}
fn write_u64(&mut self, n: u64) {
self.0 = n;
}
fn finish(&self) -> u64 {
self.0
}
}