Skip to content

Commit dae80fb

Browse files
authored
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.
1 parent 52ea3cb commit dae80fb

File tree

6 files changed

+25
-28
lines changed

6 files changed

+25
-28
lines changed

turbopack/crates/turbo-tasks-fs/src/lib.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,30 +1438,27 @@ impl FileSystemPath {
14381438
))
14391439
}
14401440

1441-
/// Similar to [FileSystemPath::join], but returns an Option that will be
1442-
/// None when the joined path would leave the filesystem root.
1441+
/// Similar to [FileSystemPath::join], but returns an [`Option`] that will be [`None`] when the
1442+
/// joined path would leave the filesystem root.
14431443
#[allow(clippy::needless_borrow)] // for windows build
1444-
pub fn try_join(&self, path: &str) -> Result<Option<FileSystemPath>> {
1444+
pub fn try_join(&self, path: &str) -> Option<FileSystemPath> {
14451445
// TODO(PACK-3279): Remove this once we do not produce invalid paths at the first place.
14461446
#[cfg(target_os = "windows")]
14471447
let path = path.replace('\\', "/");
14481448

1449-
if let Some(path) = join_path(&self.path, &path) {
1450-
Ok(Some(Self::new_normalized(self.fs, path.into())))
1451-
} else {
1452-
Ok(None)
1453-
}
1449+
join_path(&self.path, &path).map(|p| Self::new_normalized(self.fs, RcStr::from(p)))
14541450
}
14551451

1456-
/// Similar to [FileSystemPath::join], but returns an Option that will be
1457-
/// None when the joined path would leave the current path.
1458-
pub fn try_join_inside(&self, path: &str) -> Result<Option<FileSystemPath>> {
1459-
if let Some(path) = join_path(&self.path, path)
1460-
&& path.starts_with(&*self.path)
1452+
/// Similar to [FileSystemPath::try_join], but returns [`None`] when the new path would leave
1453+
/// the current path (not just the filesystem root). This is useful for preventing access
1454+
/// outside of a directory.
1455+
pub fn try_join_inside(&self, path: &str) -> Option<FileSystemPath> {
1456+
if let Some(p) = join_path(&self.path, path)
1457+
&& p.starts_with(&*self.path)
14611458
{
1462-
return Ok(Some(Self::new_normalized(self.fs, path.into())));
1459+
return Some(Self::new_normalized(self.fs, RcStr::from(p)));
14631460
}
1464-
Ok(None)
1461+
None
14651462
}
14661463

14671464
/// DETERMINISM: Result is in random order. Either sort result or do not depend

turbopack/crates/turbopack-core/src/resolve/pattern.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,9 +1561,9 @@ pub async fn read_matches(
15611561
if last_segment.is_empty() {
15621562
// This means we don't have a last segment, so we just have a directory
15631563
let joined = if force_in_lookup_dir {
1564-
lookup_dir.try_join_inside(parent_path)?
1564+
lookup_dir.try_join_inside(parent_path)
15651565
} else {
1566-
lookup_dir.try_join(parent_path)?
1566+
lookup_dir.try_join(parent_path)
15671567
};
15681568
let Some(fs_path) = joined else {
15691569
continue;
@@ -1579,9 +1579,9 @@ pub async fn read_matches(
15791579
Entry::Occupied(e) => Some(e.into_mut()),
15801580
Entry::Vacant(e) => {
15811581
let path_option = if force_in_lookup_dir {
1582-
lookup_dir.try_join_inside(parent_path)?
1582+
lookup_dir.try_join_inside(parent_path)
15831583
} else {
1584-
lookup_dir.try_join(parent_path)?
1584+
lookup_dir.try_join(parent_path)
15851585
};
15861586
if let Some(path) = path_option {
15871587
Some(e.insert((path.raw_read_dir().await?, path)))
@@ -1635,9 +1635,9 @@ pub async fn read_matches(
16351635
let subpath = &str[..=str.rfind('/').unwrap()];
16361636
if handled.insert(subpath) {
16371637
let joined = if force_in_lookup_dir {
1638-
lookup_dir.try_join_inside(subpath)?
1638+
lookup_dir.try_join_inside(subpath)
16391639
} else {
1640-
lookup_dir.try_join(subpath)?
1640+
lookup_dir.try_join(subpath)
16411641
};
16421642
let Some(fs_path) = joined else {
16431643
continue;

turbopack/crates/turbopack-core/src/source_map/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ impl SourceMap {
377377
origin: FileSystemPath,
378378
) -> Result<(BytesStr, BytesStr)> {
379379
Ok(
380-
if let Some(path) = origin.parent().try_join(&source_request)? {
380+
if let Some(path) = origin.parent().try_join(&source_request) {
381381
let path_str = path.value_to_string().await?;
382382
let source = format!("{SOURCE_URL_PROTOCOL}///{path_str}");
383383
let source_content = if let Some(source_content) = source_content {

turbopack/crates/turbopack-core/src/source_map/utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ pub async fn resolve_source_map_sources(
128128
} else {
129129
// assume it's a relative URL, and just remove any percent encoding from path
130130
// segments. Our internal path format is POSIX-like, without percent encoding.
131-
origin.parent().try_join(
132-
&urlencoding::decode(source_url).unwrap_or(Cow::Borrowed(source_url)),
133-
)?
131+
origin
132+
.parent()
133+
.try_join(&urlencoding::decode(source_url).unwrap_or(Cow::Borrowed(source_url)))
134134
};
135135

136136
if let Some(fs_path) = fs_path {

turbopack/crates/turbopack-ecmascript/src/references/typescript.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl ModuleReference for TsReferencePathAssetReference {
7878
.origin_path()
7979
.await?
8080
.parent()
81-
.try_join(&self.path)?
81+
.try_join(&self.path)
8282
{
8383
let module = self
8484
.origin

turbopack/crates/turbopack-resolve/src/typescript.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ async fn try_join_base_url(
255255
base_url: RcStr,
256256
) -> Result<Vc<FileSystemPathOption>> {
257257
Ok(Vc::cell(
258-
source.ident().path().await?.parent().try_join(&base_url)?,
258+
source.ident().path().await?.parent().try_join(&base_url),
259259
))
260260
}
261261

@@ -294,7 +294,7 @@ pub async fn tsconfig_resolve_options(
294294
{
295295
let mut context_dir = source.ident().path().await?.parent();
296296
if let Some(base_url) = json["compilerOptions"]["baseUrl"].as_str()
297-
&& let Some(new_context) = context_dir.try_join(base_url)?
297+
&& let Some(new_context) = context_dir.try_join(base_url)
298298
{
299299
context_dir = new_context;
300300
};

0 commit comments

Comments
 (0)