parcel_resolver/
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
use bitflags::bitflags;
use dashmap::DashSet;
use rustc_hash::FxHasher;

use crate::{
  fs::FileKind,
  package_json::PackageJson,
  tsconfig::{TsConfig, TsConfigWrapper},
  FileSystem, ResolverError,
};
use std::{
  cell::UnsafeCell,
  ffi::OsStr,
  hash::{BuildHasherDefault, Hash, Hasher},
  ops::Deref,
  path::{is_separator, Component, Path, PathBuf},
  sync::{
    atomic::{AtomicU64, Ordering},
    Arc, OnceLock,
  },
};

/// Stores various cached info about file paths.
pub struct Cache {
  pub fs: Arc<dyn FileSystem>,
  paths: DashSet<PathEntry<'static>, BuildHasherDefault<IdentityHasher>>,
}

/// An entry in the path cache. Can also be borrowed for lookups without allocations.
enum PathEntry<'a> {
  Owned(Arc<PathInfo>),
  Borrowed { hash: u64, path: &'a Path },
}

impl<'a> Hash for PathEntry<'a> {
  fn hash<H: Hasher>(&self, state: &mut H) {
    match self {
      PathEntry::Owned(info) => {
        info.hash.hash(state);
      }
      PathEntry::Borrowed { hash, .. } => {
        hash.hash(state);
      }
    }
  }
}

impl<'a> PartialEq for PathEntry<'a> {
  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<'a> Eq for PathEntry<'a> {}

#[cfg(not(target_arch = "wasm32"))]
impl Default for Cache {
  fn default() -> Self {
    Cache::new(Arc::new(crate::fs::OsFileSystem))
  }
}

impl Cache {
  /// Creates an empty cache with the given file system.
  pub fn new(fs: Arc<dyn FileSystem>) -> Cache {
    Cache {
      fs,
      paths: DashSet::default(),
    }
  }

  /// Returns cached info for a pre-normalized path.
  pub fn get<P: AsRef<Path>>(&self, path: P) -> CachedPath {
    self.get_path(path.as_ref())
  }

  /// Normalizes the given path and returns its cached info.
  pub fn get_normalized<P: AsRef<Path>>(&self, path: P) -> CachedPath {
    self.get_path(&normalize_path(path.as_ref()))
  }

  fn get_path(&self, path: &Path) -> CachedPath {
    let mut hasher = FxHasher::default();
    path.as_os_str().hash(&mut hasher);
    let hash = 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 CachedPath(Arc::clone(entry));
      }
    }

    // If that wasn't found, we need to create a new entry.
    let parent = path
      .parent()
      .map(|p| CachedPath(Arc::clone(&self.get(p).0)));
    let mut flags = parent.as_ref().map_or(PathFlags::empty(), |p| {
      p.0.flags & PathFlags::IN_NODE_MODULES
    });
    if matches!(path.file_name(), Some(f) if f == "node_modules") {
      flags |= PathFlags::IS_NODE_MODULES | PathFlags::IN_NODE_MODULES;
    }

    let info = Arc::new(PathInfo {
      hash,
      path: path.to_path_buf(),
      parent,
      flags,
      kind: OnceLock::new(),
      canonical: OnceLock::new(),
      canonicalizing: AtomicU64::new(0),
      package_json: OnceLock::new(),
      tsconfig: OnceLock::new(),
    });

    self.paths.insert(PathEntry::Owned(Arc::clone(&info)));
    CachedPath(info)
  }
}

pub(crate) mod private {
  use super::*;

  #[allow(clippy::large_enum_variant)]
  /// Special Cow implementation for a Cache that doesn't require Clone.
  pub enum CacheCow<'a> {
    Borrowed(&'a Cache),
    Owned(Cache),
  }

  impl<'a> Deref for CacheCow<'a> {
    type Target = Cache;

    fn deref(&self) -> &Self::Target {
      match self {
        CacheCow::Borrowed(c) => c,
        CacheCow::Owned(c) => c,
      }
    }
  }

  impl<'a> From<Cache> for CacheCow<'a> {
    fn from(value: Cache) -> Self {
      CacheCow::Owned(value)
    }
  }

  impl<'a> From<&'a Cache> for CacheCow<'a> {
    fn from(value: &'a Cache) -> Self {
      CacheCow::Borrowed(value)
    }
  }
}

bitflags! {
  struct PathFlags: u8 {
    /// Whether this path is inside a node_modules directory.
    const IN_NODE_MODULES = 1 << 0;
    /// Whether this path is a node_modules directory.
    const IS_NODE_MODULES = 1 << 1;
  }
}

/// Cached info about a file path.
struct PathInfo {
  hash: u64,
  path: PathBuf,
  flags: PathFlags,
  parent: Option<CachedPath>,
  kind: OnceLock<FileKind>,
  canonical: OnceLock<Result<CachedPath, ResolverError>>,
  canonicalizing: AtomicU64,
  package_json: OnceLock<Arc<Result<PackageJson, ResolverError>>>,
  tsconfig: OnceLock<Arc<Result<TsConfigWrapper, ResolverError>>>,
}

#[derive(Clone)]
pub struct CachedPath(Arc<PathInfo>);

impl CachedPath {
  /// Returns a std Path.
  pub fn as_path(&self) -> &Path {
    self.0.path.as_path()
  }

  /// Returns the parent path.
  pub fn parent(&self) -> Option<&CachedPath> {
    self.0.parent.as_ref()
  }

  fn kind(&self, fs: &dyn FileSystem) -> FileKind {
    *self.0.kind.get_or_init(|| fs.kind(self.as_path()))
  }

  /// Returns whether the path is a file.
  pub fn is_file(&self, fs: &dyn FileSystem) -> bool {
    self.kind(fs).contains(FileKind::IS_FILE)
  }

  /// Returns whether the path is a directory.
  pub fn is_dir(&self, fs: &dyn FileSystem) -> bool {
    self.kind(fs).contains(FileKind::IS_DIR)
  }

  /// Returns whether the path is a node_modules directory.
  pub fn is_node_modules(&self) -> bool {
    self.0.flags.contains(PathFlags::IS_NODE_MODULES)
  }

  /// Returns whether the path is inside a node_modules directory.
  pub fn in_node_modules(&self) -> bool {
    self.0.flags.contains(PathFlags::IN_NODE_MODULES)
  }

  /// Returns the canonical path, resolving all symbolic links.
  pub fn canonicalize(&self, cache: &Cache) -> Result<CachedPath, ResolverError> {
    // 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(std::io::Error::new(std::io::ErrorKind::NotFound, "Circular symlink").into());
    }

    self
      .0
      .canonical
      .get_or_init(|| {
        self.0.canonicalizing.store(tid, Ordering::Release);

        let res = self
          .parent()
          .map(|parent| {
            parent.canonicalize(cache).and_then(|parent_canonical| {
              let path = parent_canonical.join(
                self
                  .as_path()
                  .strip_prefix(parent.as_path())
                  .map_err(|_| ResolverError::UnknownError)?,
                cache,
              );

              if self.kind(&*cache.fs).contains(FileKind::IS_SYMLINK) {
                let link = cache.fs.read_link(path.as_path())?;
                if link.is_absolute() {
                  return cache.get(&normalize_path(&link)).canonicalize(cache);
                } else {
                  return path.resolve(&link, cache).canonicalize(cache);
                }
              }

              Ok(path)
            })
          })
          .unwrap_or_else(|| Ok(self.clone()));

        self.0.canonicalizing.store(0, Ordering::Release);
        res
      })
      .clone()
  }

  /// Returns an iterator over all ancestor paths.
  pub fn ancestors<'a>(&'a self) -> impl Iterator<Item = &'a CachedPath> {
    std::iter::successors(Some(self), |p| p.parent())
  }

  /// Returns the file name of this path (the final path component).
  pub fn file_name(&self) -> Option<&OsStr> {
    self.as_path().file_name()
  }

  /// Returns the file extension of this path.
  pub fn extension(&self) -> Option<&OsStr> {
    self.as_path().extension()
  }

  /// Returns a new path with the given path segment appended to this path.
  pub fn join<P: AsRef<OsStr>>(&self, segment: P, cache: &Cache) -> CachedPath {
    SCRATCH_PATH.with(|path| {
      let path = unsafe { &mut *path.get() };
      path.clear();
      path.as_mut_os_string().push(self.as_path().as_os_str());
      push_normalized(path, segment.as_ref());
      cache.get(path)
    })
  }

  /// Returns a new path with the given node_modules directory appended to this path.
  pub fn join_module(&self, module: &str, cache: &Cache) -> CachedPath {
    SCRATCH_PATH.with(|path| {
      let path = unsafe { &mut *path.get() };
      path.clear();
      path.as_mut_os_string().push(self.as_path().as_os_str());
      path.push("node_modules");
      push_normalized(path, module);
      cache.get(path)
    })
  }

  /// Returns a new path with the given node_modules directory and package subpath appended to this path.
  pub fn join_package(&self, module: &str, subpath: &str, cache: &Cache) -> CachedPath {
    SCRATCH_PATH.with(|path| {
      let path = unsafe { &mut *path.get() };
      path.clear();
      path.as_mut_os_string().push(self.as_path().as_os_str());
      push_normalized(path, module);
      push_normalized(path, subpath);
      cache.get(path)
    })
  }

  /// Returns a new path by resolving the given subpath (including "." and ".." components) with this path.
  pub fn resolve(&self, subpath: &Path, cache: &Cache) -> CachedPath {
    SCRATCH_PATH.with(|path| {
      let path = unsafe { &mut *path.get() };
      path.clear();
      if let Some(parent) = self.0.parent.as_ref() {
        path.as_mut_os_string().push(parent.0.path.as_os_str());
      }

      for component in subpath.components() {
        match component {
          Component::Prefix(..) | Component::RootDir => unreachable!(),
          Component::CurDir => {}
          Component::ParentDir => {
            path.pop();
          }
          Component::Normal(c) => {
            path.push(c);
          }
        }
      }

      cache.get(path)
    })
  }

  /// Returns a new path by appending the given file extension (without leading ".") with this path.
  pub fn add_extension(&self, ext: &str, cache: &Cache) -> CachedPath {
    SCRATCH_PATH.with(|path| {
      let path = unsafe { &mut *path.get() };
      path.clear();
      let s = path.as_mut_os_string();
      s.push(self.as_path().as_os_str());
      s.push(".");
      s.push(ext);
      cache.get(path)
    })
  }

  /// Returns the parsed package.json at this path.
  pub fn package_json(&self, cache: &Cache) -> Arc<Result<PackageJson, ResolverError>> {
    self
      .0
      .package_json
      .get_or_init(|| Arc::new(PackageJson::read(self, cache)))
      .clone()
  }

  /// Returns the parsed tsconfig.json at this path.
  pub fn tsconfig<F: FnOnce(&mut TsConfigWrapper) -> Result<(), ResolverError>>(
    &self,
    cache: &Cache,
    process: F,
  ) -> Arc<Result<TsConfigWrapper, ResolverError>> {
    self
      .0
      .tsconfig
      .get_or_init(|| Arc::new(TsConfig::read(self, process, cache)))
      .clone()
  }
}

static THREAD_COUNT: AtomicU64 = AtomicU64::new(1);

// Per-thread pre-allocated path that is used to perform operations on paths more quickly.
thread_local! {
  pub static SCRATCH_PATH: UnsafeCell<PathBuf> = UnsafeCell::new(PathBuf::with_capacity(256));
  pub static THREAD_ID: u64 = THREAD_COUNT.fetch_add(1, Ordering::SeqCst);
}

#[cfg(windows)]
#[inline]
fn push_normalized<S: AsRef<OsStr>>(path: &mut PathBuf, s: S) {
  // PathBuf::push does not normalize separators, so on Windows, push each part separately.
  // Note that this does not use Path::components because that also strips the trailing separator.
  let bytes = s.as_ref().as_encoded_bytes();
  for part in bytes.split(|b| *b == b'/') {
    path.push(unsafe { OsStr::from_encoded_bytes_unchecked(part) });
  }
}

#[cfg(not(windows))]
#[inline]
fn push_normalized<S: AsRef<OsStr>>(path: &mut PathBuf, s: S) {
  path.push(s.as_ref());
}

impl Hash for CachedPath {
  fn hash<H: Hasher>(&self, state: &mut H) {
    self.0.hash.hash(state);
  }
}

impl PartialEq for CachedPath {
  fn eq(&self, other: &Self) -> bool {
    // Cached paths always point to unique values, so we only need to compare the pointers.
    std::ptr::eq(Arc::as_ptr(&self.0), Arc::as_ptr(&other.0))
  }
}

impl Eq for CachedPath {}

impl std::fmt::Debug for CachedPath {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    self.0.path.fmt(f)
  }
}

/// A hasher that just passes through a value that is already a hash.
#[derive(Default)]
pub struct IdentityHasher {
  hash: u64,
}

impl Hasher for IdentityHasher {
  fn write(&mut self, bytes: &[u8]) {
    if bytes.len() == 8 {
      self.hash = u64::from_ne_bytes([
        bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
      ])
    } else {
      unreachable!()
    }
  }

  fn finish(&self) -> u64 {
    self.hash
  }
}

pub fn normalize_path(path: &Path) -> PathBuf {
  // Normalize path components to resolve ".." and "." segments.
  // https://github.com/rust-lang/cargo/blob/fede83ccf973457de319ba6fa0e36ead454d2e20/src/cargo/util/paths.rs#L61
  let mut components = path.components().peekable();
  let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
    components.next();
    PathBuf::from(c.as_os_str())
  } else {
    PathBuf::new()
  };

  for component in components {
    match component {
      Component::Prefix(..) => unreachable!(),
      Component::RootDir => {
        ret.push(component.as_os_str());
      }
      Component::CurDir => {}
      Component::ParentDir => {
        ret.pop();
      }
      Component::Normal(c) => {
        ret.push(c);
      }
    }
  }

  // If the path ends with a separator, add an additional empty component.
  if matches!(path.as_os_str().as_encoded_bytes().last(), Some(b) if is_separator(*b as char)) {
    ret.push("");
  }

  ret
}

#[cfg(test)]
mod test {
  use crate::OsFileSystem;

  use super::*;
  use assert_fs::prelude::*;

  #[test]
  fn test_canonicalize() -> Result<(), Box<dyn std::error::Error>> {
    #[cfg(windows)]
    if !is_elevated::is_elevated() {
      println!("skipping symlink tests due to missing permissions");
      return Ok(());
    }

    let dir = assert_fs::TempDir::new()?;
    dir.child("foo/bar.js").write_str("")?;
    dir.child("root.js").write_str("")?;

    dir
      .child("symlink")
      .symlink_to_file(Path::new("foo").join("bar.js"))?;
    dir
      .child("foo/symlink")
      .symlink_to_file(Path::new("..").join("root.js"))?;
    dir
      .child("absolute")
      .symlink_to_file(dir.child("root.js").path())?;
    dir
      .child("recursive")
      .symlink_to_file(Path::new("foo").join("symlink"))?;
    dir.child("cycle").symlink_to_file("cycle1")?;
    dir.child("cycle1").symlink_to_file("cycle")?;
    dir
      .child("absolute_cycle")
      .symlink_to_file(dir.child("absolute_cycle1").path())?;
    dir
      .child("absolute_cycle1")
      .symlink_to_file(dir.child("absolute_cycle").path())?;
    dir.child("a/b/c").create_dir_all()?;
    dir.child("a/b/e").symlink_to_file("..")?;
    dir.child("a/d").symlink_to_file("..")?;
    dir.child("a/b/c/x.txt").write_str("")?;
    dir
      .child("a/link")
      .symlink_to_file(dir.child("a/b").path())?;

    let fs = OsFileSystem::default();
    let cache = Cache::new(Arc::new(fs));

    assert_eq!(
      cache
        .get(dir.child("symlink").path())
        .canonicalize(&cache)?,
      cache
        .get(dir.child("foo/bar.js").path())
        .canonicalize(&cache)?
    );
    assert_eq!(
      cache
        .get(dir.child("foo/symlink").path())
        .canonicalize(&cache)?,
      cache
        .get(dir.child("root.js").path())
        .canonicalize(&cache)?
    );
    assert_eq!(
      cache
        .get(dir.child("absolute").path())
        .canonicalize(&cache)?,
      cache
        .get(dir.child("root.js").path())
        .canonicalize(&cache)?
    );
    assert_eq!(
      cache
        .get(dir.child("recursive").path())
        .canonicalize(&cache)?,
      cache
        .get(dir.child("root.js").path())
        .canonicalize(&cache)?
    );
    assert!(cache
      .get(dir.child("cycle").path())
      .canonicalize(&cache)
      .is_err());
    assert!(cache
      .get(dir.child("absolute_cycle").path())
      .canonicalize(&cache)
      .is_err());
    assert_eq!(
      cache
        .get(dir.child("a/b/e/d/a/b/e/d/a").path())
        .canonicalize(&cache)?,
      cache.get(dir.child("a").path()).canonicalize(&cache)?
    );
    assert_eq!(
      cache
        .get(dir.child("a/link/c/x.txt").path())
        .canonicalize(&cache)?,
      cache
        .get(dir.child("a/b/c/x.txt").path())
        .canonicalize(&cache)?
    );

    Ok(())
  }
}