Unverified Commit a6906597 authored by Benjamin Woodruff ‮'s avatar Benjamin Woodruff Committed by GitHub

Turbopack: Ignore realpath errors during module resolution, matching node's behavior (#99951)

Addresses agent feedback:
https://vercel.slack.com/archives/C0ANB536V62/p1791479564863939

Certain setups could cause us to report a Turbopack internal error due
to this bail.

In testing, it appears that node treats realpath errors (e.g. due to a
symlink cycle) the same as not-found. So while these errors could be
useful, we shouldn't emit them.

I discussed this with @lukesandberg, and because the resolution
algorithm is so hot, emitting issues here (e.g. warnings) could be
extremely noisy. So it seems better not to do that.

If somebody's `node_modules` is severely f*ked up,
`MissingNextFolderIssue` should already provide a sufficiently good
error message:
https://github.com/vercel/next.js/blob/3fd5b1c0b23546e1256f37ee7d705e255a4938e2/crates/next-core/src/next_import_map.rs#L1320-L1323
parent 3fd5b1c0
...@@ -20,7 +20,7 @@ use turbo_tasks::{ ...@@ -20,7 +20,7 @@ use turbo_tasks::{
FxIndexMap, JoinIterExt, NonLocalValue, ReadRef, ResolvedVc, TryFlatJoinIterExt, FxIndexMap, JoinIterExt, NonLocalValue, ReadRef, ResolvedVc, TryFlatJoinIterExt,
TryJoinIterExt, ValueToString, ValueToStringRef, Vc, TryJoinIterExt, ValueToString, ValueToStringRef, Vc,
}; };
use turbo_tasks_fs::{FileSystemEntryType, FileSystemPath, RealPathErrorType}; use turbo_tasks_fs::{FileSystemEntryType, FileSystemPath};
use turbo_unix_path::normalize_request; use turbo_unix_path::normalize_request;
use crate::{ use crate::{
...@@ -1218,8 +1218,9 @@ async fn realpath_if_exists( ...@@ -1218,8 +1218,9 @@ async fn realpath_if_exists(
} }
match &result.path_result { match &result.path_result {
Ok(path) => Ok(Some(path.clone())), Ok(path) => Ok(Some(path.clone())),
Err(error) if matches!(error.kind(), RealPathErrorType::NotFound) => Ok(None), // Treat paths whose symlinks cannot be resolved as missing candidates, matching Node.js
Err(error) => bail!(error.clone()), // module resolution so lookup can continue in ancestor directories.
Err(_) => Ok(None),
} }
} }
...@@ -3629,19 +3630,21 @@ mod tests { ...@@ -3629,19 +3630,21 @@ mod tests {
#[cfg(unix)] #[cfg(unix)]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_missing_paths_through_symlinks_do_not_error() { async fn test_symlink_resolution_failures_are_missing_candidates() {
use std::os::unix::fs::symlink; use std::os::unix::fs::symlink;
#[turbo_tasks::value] #[turbo_tasks::value]
struct MissingPathsResult { struct MissingPathsResult {
missing_file: bool, missing_file: bool,
dangling_package: bool, dangling: bool,
self_cycle: bool,
} }
let scratch = tempfile::tempdir().unwrap(); let scratch = tempfile::tempdir().unwrap();
create_dir_all(scratch.path().join("package")).unwrap(); create_dir_all(scratch.path().join("package")).unwrap();
symlink("package", scratch.path().join("linked-package")).unwrap(); symlink("package", scratch.path().join("linked-package")).unwrap();
symlink("missing-package", scratch.path().join("dangling-package")).unwrap(); symlink("missing", scratch.path().join("dangling")).unwrap();
symlink("self-cycle", scratch.path().join("self-cycle")).unwrap();
let path = RcStr::from(scratch.path().to_str().unwrap()); let path = RcStr::from(scratch.path().to_str().unwrap());
let tt = turbo_tasks::TurboTasks::new(TurboTasksBackend::new( let tt = turbo_tasks::TurboTasks::new(TurboTasksBackend::new(
...@@ -3655,12 +3658,16 @@ mod tests { ...@@ -3655,12 +3658,16 @@ mod tests {
) -> Result<Vc<MissingPathsResult>> { ) -> Result<Vc<MissingPathsResult>> {
let fs = DiskFileSystem::new(rcstr!("temp"), Vc::cell(path)); let fs = DiskFileSystem::new(rcstr!("temp"), Vc::cell(path));
let root = fs.root().owned().await?; let root = fs.root().owned().await?;
let missing_file = root.join("linked-package/package.json")?;
let dangling_package = root.join("dangling-package")?;
Ok(MissingPathsResult { Ok(MissingPathsResult {
missing_file: realpath_if_exists(&missing_file, None).await?.is_none(), missing_file: realpath_if_exists(&root.join("linked-package/package.json")?, None)
dangling_package: realpath_if_exists(&dangling_package, None).await?.is_none(), .await?
.is_none(),
dangling: realpath_if_exists(&root.join("dangling")?, None)
.await?
.is_none(),
self_cycle: realpath_if_exists(&root.join("self-cycle")?, None)
.await?
.is_none(),
} }
.cell()) .cell())
} }
...@@ -3670,7 +3677,8 @@ mod tests { ...@@ -3670,7 +3677,8 @@ mod tests {
.read_strongly_consistent() .read_strongly_consistent()
.await?; .await?;
assert!(missing.missing_file); assert!(missing.missing_file);
assert!(missing.dangling_package); assert!(missing.dangling);
assert!(missing.self_cycle);
anyhow::Ok(()) anyhow::Ok(())
}) })
......
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