Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: RootsPlugin should fall through if it fails to resolve the roots #144

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}