Unverified Commit dae80fb7 authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

Turbopack: Simplify the return type of `FileSystemPath::try_join` (#86523)

This is probably left over from when more of `FileSystemPath` was using `turbo_task::function`s...

We can't ever possibly return an error, so the wrapping `Result` isn't needed.
parent 52ea3cba
...@@ -1438,30 +1438,27 @@ impl FileSystemPath { ...@@ -1438,30 +1438,27 @@ impl FileSystemPath {
)) ))
} }
/// Similar to [FileSystemPath::join], but returns an Option that will be /// Similar to [FileSystemPath::join], but returns an [`Option`] that will be [`None`] when the
/// None when the joined path would leave the filesystem root. /// joined path would leave the filesystem root.
#[allow(clippy::needless_borrow)] // for windows build #[allow(clippy::needless_borrow)] // for windows build
pub fn try_join(&self, path: &str) -> Result<Option<FileSystemPath>> { pub fn try_join(&self, path: &str) -> Option<FileSystemPath> {
// TODO(PACK-3279): Remove this once we do not produce invalid paths at the first place. // TODO(PACK-3279): Remove this once we do not produce invalid paths at the first place.
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
let path = path.replace('\\', "/"); let path = path.replace('\\', "/");
if let Some(path) = join_path(&self.path, &path) { join_path(&self.path, &path).map(|p| Self::new_normalized(self.fs, RcStr::from(p)))
Ok(Some(Self::new_normalized(self.fs, path.into())))
} else {
Ok(None)
}
} }
/// Similar to [FileSystemPath::join], but returns an Option that will be /// Similar to [FileSystemPath::try_join], but returns [`None`] when the new path would leave
/// None when the joined path would leave the current path. /// the current path (not just the filesystem root). This is useful for preventing access
pub fn try_join_inside(&self, path: &str) -> Result<Option<FileSystemPath>> { /// outside of a directory.
if let Some(path) = join_path(&self.path, path) pub fn try_join_inside(&self, path: &str) -> Option<FileSystemPath> {
&& path.starts_with(&*self.path) if let Some(p) = join_path(&self.path, path)
&& p.starts_with(&*self.path)
{ {
return Ok(Some(Self::new_normalized(self.fs, path.into()))); return Some(Self::new_normalized(self.fs, RcStr::from(p)));
} }
Ok(None) None
} }
/// DETERMINISM: Result is in random order. Either sort result or do not depend /// DETERMINISM: Result is in random order. Either sort result or do not depend
......
...@@ -1561,9 +1561,9 @@ pub async fn read_matches( ...@@ -1561,9 +1561,9 @@ pub async fn read_matches(
if last_segment.is_empty() { if last_segment.is_empty() {
// This means we don't have a last segment, so we just have a directory // This means we don't have a last segment, so we just have a directory
let joined = if force_in_lookup_dir { let joined = if force_in_lookup_dir {
lookup_dir.try_join_inside(parent_path)? lookup_dir.try_join_inside(parent_path)
} else { } else {
lookup_dir.try_join(parent_path)? lookup_dir.try_join(parent_path)
}; };
let Some(fs_path) = joined else { let Some(fs_path) = joined else {
continue; continue;
...@@ -1579,9 +1579,9 @@ pub async fn read_matches( ...@@ -1579,9 +1579,9 @@ pub async fn read_matches(
Entry::Occupied(e) => Some(e.into_mut()), Entry::Occupied(e) => Some(e.into_mut()),
Entry::Vacant(e) => { Entry::Vacant(e) => {
let path_option = if force_in_lookup_dir { let path_option = if force_in_lookup_dir {
lookup_dir.try_join_inside(parent_path)? lookup_dir.try_join_inside(parent_path)
} else { } else {
lookup_dir.try_join(parent_path)? lookup_dir.try_join(parent_path)
}; };
if let Some(path) = path_option { if let Some(path) = path_option {
Some(e.insert((path.raw_read_dir().await?, path))) Some(e.insert((path.raw_read_dir().await?, path)))
...@@ -1635,9 +1635,9 @@ pub async fn read_matches( ...@@ -1635,9 +1635,9 @@ pub async fn read_matches(
let subpath = &str[..=str.rfind('/').unwrap()]; let subpath = &str[..=str.rfind('/').unwrap()];
if handled.insert(subpath) { if handled.insert(subpath) {
let joined = if force_in_lookup_dir { let joined = if force_in_lookup_dir {
lookup_dir.try_join_inside(subpath)? lookup_dir.try_join_inside(subpath)
} else { } else {
lookup_dir.try_join(subpath)? lookup_dir.try_join(subpath)
}; };
let Some(fs_path) = joined else { let Some(fs_path) = joined else {
continue; continue;
......
...@@ -377,7 +377,7 @@ impl SourceMap { ...@@ -377,7 +377,7 @@ impl SourceMap {
origin: FileSystemPath, origin: FileSystemPath,
) -> Result<(BytesStr, BytesStr)> { ) -> Result<(BytesStr, BytesStr)> {
Ok( Ok(
if let Some(path) = origin.parent().try_join(&source_request)? { if let Some(path) = origin.parent().try_join(&source_request) {
let path_str = path.value_to_string().await?; let path_str = path.value_to_string().await?;
let source = format!("{SOURCE_URL_PROTOCOL}///{path_str}"); let source = format!("{SOURCE_URL_PROTOCOL}///{path_str}");
let source_content = if let Some(source_content) = source_content { let source_content = if let Some(source_content) = source_content {
......
...@@ -128,9 +128,9 @@ pub async fn resolve_source_map_sources( ...@@ -128,9 +128,9 @@ pub async fn resolve_source_map_sources(
} else { } else {
// assume it's a relative URL, and just remove any percent encoding from path // assume it's a relative URL, and just remove any percent encoding from path
// segments. Our internal path format is POSIX-like, without percent encoding. // segments. Our internal path format is POSIX-like, without percent encoding.
origin.parent().try_join( origin
&urlencoding::decode(source_url).unwrap_or(Cow::Borrowed(source_url)), .parent()
)? .try_join(&urlencoding::decode(source_url).unwrap_or(Cow::Borrowed(source_url)))
}; };
if let Some(fs_path) = fs_path { if let Some(fs_path) = fs_path {
......
...@@ -78,7 +78,7 @@ impl ModuleReference for TsReferencePathAssetReference { ...@@ -78,7 +78,7 @@ impl ModuleReference for TsReferencePathAssetReference {
.origin_path() .origin_path()
.await? .await?
.parent() .parent()
.try_join(&self.path)? .try_join(&self.path)
{ {
let module = self let module = self
.origin .origin
......
...@@ -255,7 +255,7 @@ async fn try_join_base_url( ...@@ -255,7 +255,7 @@ async fn try_join_base_url(
base_url: RcStr, base_url: RcStr,
) -> Result<Vc<FileSystemPathOption>> { ) -> Result<Vc<FileSystemPathOption>> {
Ok(Vc::cell( Ok(Vc::cell(
source.ident().path().await?.parent().try_join(&base_url)?, source.ident().path().await?.parent().try_join(&base_url),
)) ))
} }
...@@ -294,7 +294,7 @@ pub async fn tsconfig_resolve_options( ...@@ -294,7 +294,7 @@ pub async fn tsconfig_resolve_options(
{ {
let mut context_dir = source.ident().path().await?.parent(); let mut context_dir = source.ident().path().await?.parent();
if let Some(base_url) = json["compilerOptions"]["baseUrl"].as_str() if let Some(base_url) = json["compilerOptions"]["baseUrl"].as_str()
&& let Some(new_context) = context_dir.try_join(base_url)? && let Some(new_context) = context_dir.try_join(base_url)
{ {
context_dir = new_context; context_dir = new_context;
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment