Skip to content

Commit

Permalink
fix: RootsPlugin should fall through if it fails to resolve the roots (
Browse files Browse the repository at this point in the history
…#144)

---------

Co-authored-by: Boshen <boshenc@gmail.com>
  • Loading branch information
ahabhgk and Boshen authored Apr 24, 2024
1 parent d345d3d commit 87dc7f0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
Empty file.
30 changes: 12 additions & 18 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,27 +333,21 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
return Ok(path);
}
}
if self.options.roots.is_empty() {
// 2. If X begins with '/'
// a. set Y to be the file system root
let path = self.cache.value(Path::new(specifier));
if let Some(path) = self.load_as_file_or_directory(&path, specifier, ctx)? {
// enhanced-resolve: RootsPlugin
for root in &self.options.roots {
let cached_path = self.cache.value(root);
let specifier = specifier.trim_start_matches(SLASH_START);
if let Ok(path) = self.require_relative(&cached_path, specifier, ctx) {
return Ok(path);
}
Err(ResolveError::NotFound(specifier.to_string()))
} else {
for root in &self.options.roots {
let cached_path = self.cache.value(root);
if let Ok(path) = self.require_relative(
&cached_path,
specifier.trim_start_matches(SLASH_START),
ctx,
) {
return Ok(path);
}
}
Err(ResolveError::NotFound(specifier.to_string()))
}
// 2. If X begins with '/'
// a. set Y to be the file system root
let path = self.cache.value(Path::new(specifier));
if let Some(path) = self.load_as_file_or_directory(&path, specifier, ctx)? {
return Ok(path);
}
Err(ResolveError::NotFound(specifier.to_string()))
}

// 3. If X begins with './' or '/' or '../'
Expand Down
9 changes: 9 additions & 0 deletions src/tests/roots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,12 @@ fn prefer_absolute() {
assert_eq!(resolved_path, Ok(expected), "{comment} {request}");
}
}

#[test]
fn roots_fall_through() {
let f = super::fixture();
let absolute_path = f.join("roots_fall_through/index.js");
let specifier = absolute_path.to_string_lossy();
let resolution = Resolver::new(ResolveOptions::default().with_root(&f)).resolve(&f, &specifier);
assert_eq!(resolution.map(|r| r.into_path_buf()), Ok(absolute_path));
}

0 comments on commit 87dc7f0

Please sign in to comment.