Unverified Commit 1f6ea09f authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

[backport] Turbopack: Improve handling of symlink resolution errors (#83805)

Co-authored-by: 's avatarLuke Sandberg <lukesandberg@users.noreply.github.com>
parent c7d18554
......@@ -103,11 +103,11 @@ pub async fn find_pages_structure(
next_router_root: FileSystemPath,
page_extensions: Vc<Vec<RcStr>>,
) -> Result<Vc<PagesStructure>> {
let pages_root = project_root.join("pages")?.realpath().owned().await?;
let pages_root = project_root.join("pages")?.realpath().await?;
let pages_root = if *pages_root.get_type().await? == FileSystemEntryType::Directory {
Some(pages_root)
} else {
let src_pages_root = project_root.join("src/pages")?.realpath().owned().await?;
let src_pages_root = project_root.join("src/pages")?.realpath().await?;
if *src_pages_root.get_type().await? == FileSystemEntryType::Directory {
Some(src_pages_root)
} else {
......
This diff is collapsed.
......@@ -99,7 +99,7 @@ async fn read_glob_internal(
}
}
}
DirectoryEntry::Other(_) | DirectoryEntry::Error => continue,
DirectoryEntry::Other(_) | DirectoryEntry::Error(_) => continue,
}
}
}
......@@ -197,18 +197,23 @@ async fn track_glob_internal(
reads.push(fs.read(path.clone()))
}
}
DirectoryEntry::Symlink(symlink_path) => unreachable!(
"resolve_symlink_safely() should have resolved all symlinks, but found \
unresolved symlink at path: '{}'. Found path: '{}'. Please report this \
as a bug.",
entry_path, symlink_path
DirectoryEntry::Symlink(symlink_path) => bail!(
"resolve_symlink_safely() should have resolved all symlinks or returned \
an error, but found unresolved symlink at path: '{}'. Found path: '{}'. \
Please report this as a bug.",
entry_path,
symlink_path
),
DirectoryEntry::Other(path) => {
if glob_value.matches(&entry_path) {
types.push(path.get_type())
}
}
DirectoryEntry::Error => {}
// The most likely case of this is actually a sylink resolution error, it is
// fine to ignore since the mere act of attempting to resolve it has triggered
// the ncecessary dependencies. If this file is actually a dependency we should
// get an error in the actual webpack loader when it reads it.
DirectoryEntry::Error(_) => {}
}
}
}
......@@ -517,8 +522,6 @@ pub mod tests {
));
let path: RcStr = scratch.path().to_str().unwrap().into();
tt.run_once(async {
use turbo_rcstr::rcstr;
let fs = Vc::upcast::<Box<dyn FileSystem>>(DiskFileSystem::new(rcstr!("temp"), path));
let err = fs
.root()
......@@ -551,6 +554,102 @@ pub mod tests {
.unwrap();
}
// Reproduces an issue where a dead symlink would cause a panic when tracking/reading a glob
#[cfg(unix)]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn dead_symlinks() {
crate::register();
let scratch = tempfile::tempdir().unwrap();
{
use std::os::unix::fs::symlink;
// Create a simple directory with 1 file and a symlink pointing at a non-existent file
let path = scratch.path();
let sub = &path.join("sub");
create_dir(sub).unwrap();
let foo = sub.join("foo.js");
File::create_new(&foo).unwrap().write_all(b"foo").unwrap();
// put a link in sub that points to a sibling file that doesn't exist
symlink(sub.join("doesntexist.js"), sub.join("dead_link.js")).unwrap();
}
let tt = turbo_tasks::TurboTasks::new(TurboTasksBackend::new(
BackendOptions::default(),
noop_backing_storage(),
));
let path: RcStr = scratch.path().to_str().unwrap().into();
tt.run_once(async {
let fs = Vc::upcast::<Box<dyn FileSystem>>(DiskFileSystem::new(rcstr!("temp"), path));
fs.root()
.await?
.track_glob(Glob::new(rcstr!("sub/*.js")), false)
.await
})
.await
.unwrap();
let path: RcStr = scratch.path().to_str().unwrap().into();
tt.run_once(async {
let fs = Vc::upcast::<Box<dyn FileSystem>>(DiskFileSystem::new(rcstr!("temp"), path));
let root = fs.root().owned().await?;
let read_dir = root.read_glob(Glob::new(rcstr!("sub/*.js"))).await?;
assert_eq!(read_dir.results.len(), 0);
assert_eq!(read_dir.inner.len(), 1);
let inner_sub = &*read_dir.inner.get("sub").unwrap().await?;
assert_eq!(inner_sub.inner.len(), 0);
assert_eq!(
inner_sub.results,
HashMap::from_iter([
(
"foo.js".into(),
DirectoryEntry::File(root.join("sub/foo.js")?),
),
// read_glob doesn't resolve symlinks and thus doesn't detect that it is dead
(
"dead_link.js".into(),
DirectoryEntry::Symlink(root.join("sub/dead_link.js")?),
)
])
);
anyhow::Ok(())
})
.await
.unwrap();
}
// Reproduces an issue where a dead symlink would cause a panic when tracking/reading a glob
#[cfg(unix)]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn symlink_escapes_fs_root() {
crate::register();
let scratch = tempfile::tempdir().unwrap();
{
use std::os::unix::fs::symlink;
// Create a simple directory with 1 file and a symlink pointing at a non-existent file
let path = scratch.path();
let sub = &path.join("sub");
create_dir(sub).unwrap();
let foo = scratch.path().join("foo.js");
File::create_new(&foo).unwrap().write_all(b"foo").unwrap();
// put a link in sub that points to a parent file
symlink(foo, sub.join("escape.js")).unwrap();
}
let tt = turbo_tasks::TurboTasks::new(TurboTasksBackend::new(
BackendOptions::default(),
noop_backing_storage(),
));
let root: RcStr = scratch.path().join("sub").to_str().unwrap().into();
tt.run_once(async {
let fs = Vc::upcast::<Box<dyn FileSystem>>(DiskFileSystem::new(rcstr!("temp"), root));
fs.root()
.await?
.track_glob(Glob::new(rcstr!("*.js")), false)
.await
})
.await
.unwrap();
}
#[cfg(unix)]
#[tokio::test]
async fn read_glob_symlinks_loop() {
......
......@@ -15,7 +15,7 @@ use turbo_tasks::{
FxIndexMap, FxIndexSet, NonLocalValue, ReadRef, ResolvedVc, SliceMap, TaskInput,
TryJoinIterExt, ValueToString, Vc, trace::TraceRawVcs,
};
use turbo_tasks_fs::{FileSystemEntryType, FileSystemPath, RealPathResult};
use turbo_tasks_fs::{FileSystemEntryType, FileSystemPath};
use turbo_unix_path::normalize_request;
use self::{
......@@ -1170,20 +1170,23 @@ async fn realpath(
fs_path: &FileSystemPath,
refs: &mut Vec<ResolvedVc<Box<dyn Source>>>,
) -> Result<FileSystemPath> {
let result = fs_path.realpath_with_links().owned().await?;
let result = fs_path.realpath_with_links().await?;
refs.extend(
result
.symlinks
.into_iter()
.iter()
.map(|path| async move {
Ok(ResolvedVc::upcast(
FileSource::new(path).to_resolved().await?,
FileSource::new(path.clone()).to_resolved().await?,
))
})
.try_join()
.await?,
);
Ok(result.path)
match &result.path_or_error {
Ok(path) => Ok(path.clone()),
Err(e) => bail!(e.as_error_message(fs_path, &result)),
}
}
#[turbo_tasks::value(shared)]
......@@ -1514,11 +1517,16 @@ pub async fn resolve_raw(
force_in_lookup_dir: bool,
) -> Result<Vc<ResolveResult>> {
async fn to_result(request: RcStr, path: FileSystemPath) -> Result<Vc<ResolveResult>> {
let RealPathResult { path, symlinks } = &*path.realpath_with_links().await?;
let result = &*path.realpath_with_links().await?;
let path = match &result.path_or_error {
Ok(path) => path,
Err(e) => bail!(e.as_error_message(&path, result)),
};
Ok(*ResolveResult::source_with_affecting_sources(
RequestKey::new(request),
ResolvedVc::upcast(FileSource::new(path.clone()).to_resolved().await?),
symlinks
result
.symlinks
.iter()
.map(|symlink| async move {
anyhow::Ok(ResolvedVc::upcast(
......@@ -2870,7 +2878,11 @@ async fn resolved(
query: RcStr,
fragment: RcStr,
) -> Result<Vc<ResolveResult>> {
let RealPathResult { path, symlinks } = &*fs_path.realpath_with_links().await?;
let result = &*fs_path.realpath_with_links().await?;
let path = match &result.path_or_error {
Ok(path) => path,
Err(e) => bail!(e.as_error_message(&fs_path, result)),
};
let path_ref = path.clone();
// Check alias field for path aliases first
......@@ -2914,7 +2926,8 @@ async fn resolved(
.to_resolved()
.await?,
),
symlinks
result
.symlinks
.iter()
.map(|symlink| async move {
anyhow::Ok(ResolvedVc::upcast(
......
......@@ -1806,7 +1806,6 @@ pub async fn read_matches(
prefix.truncate(len)
}
RawDirectoryEntry::Other => {}
RawDirectoryEntry::Error => {}
}
}
}
......
......@@ -142,7 +142,7 @@ impl Introspectable for StaticAssetsContentSource {
.to_resolved()
.await?,
),
DirectoryEntry::Other(_) | DirectoryEntry::Error => {
DirectoryEntry::Other(_) | DirectoryEntry::Error(_) => {
todo!("unsupported DirectoryContent variant: {entry:?}")
}
};
......
use anyhow::Result;
use anyhow::{Result, bail};
use tracing::Instrument;
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, ValueToString, Vc};
......@@ -124,10 +124,14 @@ async fn resolve_reference_from_dir(
FileSource::new(symlink.clone()).to_resolved().await?,
));
}
let path: FileSystemPath = match &realpath.path_or_error {
Ok(path) => path.clone(),
Err(e) => bail!(e.as_error_message(file, &realpath)),
};
results.push((
RequestKey::new(matched_path.clone()),
ResolvedVc::upcast(
RawModule::new(Vc::upcast(FileSource::new(realpath.path.clone())))
RawModule::new(Vc::upcast(FileSource::new(path)))
.to_resolved()
.await?,
),
......
use std::sync::LazyLock;
use anyhow::Result;
use anyhow::{Result, bail};
use regex::Regex;
use serde::{Deserialize, Serialize};
use turbo_rcstr::{RcStr, rcstr};
......@@ -185,7 +185,10 @@ pub async fn resolve_node_pre_gyp_files(
}
sources.insert(
format!("deps/lib/{key}").into(),
Vc::upcast(FileSource::new(realpath_with_links.path.clone())),
Vc::upcast(FileSource::new(match &realpath_with_links.path_or_error {
Ok(path) => path.clone(),
Err(e) => bail!(e.as_error_message(dylib, &realpath_with_links)),
})),
);
}
_ => {}
......
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