Unverified Commit c8cf55f4 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: fix invalid NFT entry with file behind symlink (#83013)

parent c240df08
...@@ -231,6 +231,38 @@ impl Asset for NftJsonAsset { ...@@ -231,6 +231,38 @@ impl Asset for NftJsonAsset {
continue; continue;
} }
#[cfg(debug_assertions)]
{
// Verify that we there are no entries where a file is created inside of a symlink,
// as this can result in invalid ZIP files and deployment failures.
// For example
// node_modules/.pnpm/node_modules/@libsql/client/package.json
// where
// node_modules/.pnpm/node_modules/@libsql/client is a symlink
let mut current_path = referenced_chunk_path.parent();
loop {
use turbo_tasks_fs::FileSystemEntryType;
if current_path.is_root() {
break;
}
if matches!(
&*current_path.get_type().await?,
FileSystemEntryType::Symlink
) {
bail!(
"Encountered file inside of symlink in NFT list: {} is a symlink, but \
{} was created inside of it",
current_path.value_to_string().await?,
referenced_chunk_path.value_to_string().await?
);
}
current_path = current_path.parent();
}
}
let Some(specifier) = get_output_specifier( let Some(specifier) = get_output_specifier(
&referenced_chunk_path, &referenced_chunk_path,
&ident_folder, &ident_folder,
...@@ -241,6 +273,7 @@ impl Asset for NftJsonAsset { ...@@ -241,6 +273,7 @@ impl Asset for NftJsonAsset {
else { else {
continue; continue;
}; };
result.insert(specifier); result.insert(specifier);
} }
......
...@@ -2133,58 +2133,70 @@ async fn resolve_into_folder( ...@@ -2133,58 +2133,70 @@ async fn resolve_into_folder(
package_path: FileSystemPath, package_path: FileSystemPath,
options: Vc<ResolveOptions>, options: Vc<ResolveOptions>,
) -> Result<Vc<ResolveResult>> { ) -> Result<Vc<ResolveResult>> {
let package_json_path = package_path.join("package.json")?;
let options_value = options.await?; let options_value = options.await?;
for resolve_into_package in options_value.into_package.iter() { let mut affecting_sources = vec![];
match resolve_into_package { if let Some(package_json_path) =
ResolveIntoPackage::MainField { field: name } => { exists(&package_path.join("package.json")?, &mut affecting_sources).await?
if let Some(package_json) = {
&*read_package_json(Vc::upcast(FileSource::new(package_json_path.clone()))) for resolve_into_package in options_value.into_package.iter() {
.await? match resolve_into_package {
&& let Some(field_value) = package_json[name.as_str()].as_str() ResolveIntoPackage::MainField { field: name } => {
{ if let Some(package_json) =
let normalized_request = RcStr::from(normalize_request(field_value)); &*read_package_json(Vc::upcast(FileSource::new(package_json_path.clone())))
if normalized_request.is_empty() .await?
|| &*normalized_request == "." && let Some(field_value) = package_json[name.as_str()].as_str()
|| &*normalized_request == "./"
{ {
continue; let normalized_request = RcStr::from(normalize_request(field_value));
} if normalized_request.is_empty()
let request = Request::parse_string(normalized_request); || &*normalized_request == "."
|| &*normalized_request == "./"
// main field will always resolve not fully specified {
let options = if options_value.fully_specified { continue;
options.with_fully_specified(false).resolve().await? }
} else { let request = Request::parse_string(normalized_request);
options
// main field will always resolve not fully specified
let options = if options_value.fully_specified {
options.with_fully_specified(false).resolve().await?
} else {
options
};
let result =
&*resolve_internal_inline(package_path.clone(), request, options)
.await?
.await?;
// we are not that strict when a main field fails to resolve
// we continue to try other alternatives
if !result.is_unresolvable_ref() {
let mut result: ResolveResultBuilder =
result.with_request_ref(rcstr!(".")).into();
result.affecting_sources.push(ResolvedVc::upcast(
FileSource::new(package_json_path).to_resolved().await?,
));
result.affecting_sources.extend(affecting_sources);
return Ok(ResolveResult::from(result).cell());
}
}; };
let result = &*resolve_internal_inline(package_path.clone(), request, options) }
.await? ResolveIntoPackage::ExportsField { .. } => {}
.await?;
// we are not that strict when a main field fails to resolve
// we continue to try other alternatives
if !result.is_unresolvable_ref() {
let mut result: ResolveResultBuilder =
result.with_request_ref(rcstr!(".")).into();
result.affecting_sources.push(ResolvedVc::upcast(
FileSource::new(package_json_path).to_resolved().await?,
));
return Ok(ResolveResult::from(result).cell());
}
};
} }
ResolveIntoPackage::ExportsField { .. } => {}
} }
} }
if options_value.fully_specified { if options_value.fully_specified {
return Ok(*ResolveResult::unresolvable()); return Ok(*ResolveResult::unresolvable_with_affecting_sources(
affecting_sources,
));
} }
// fall back to dir/index.[js,ts,...] // fall back to dir/index.[js,ts,...]
let pattern = match &options_value.default_files[..] { let pattern = match &options_value.default_files[..] {
[] => return Ok(*ResolveResult::unresolvable()), [] => {
return Ok(*ResolveResult::unresolvable_with_affecting_sources(
affecting_sources,
));
}
[file] => Pattern::Constant(format!("./{file}").into()), [file] => Pattern::Constant(format!("./{file}").into()),
files => Pattern::Alternatives( files => Pattern::Alternatives(
files files
...@@ -2199,7 +2211,8 @@ async fn resolve_into_folder( ...@@ -2199,7 +2211,8 @@ async fn resolve_into_folder(
Ok( Ok(
resolve_internal_inline(package_path.clone(), request, options) resolve_internal_inline(package_path.clone(), request, options)
.await? .await?
.with_request(rcstr!(".")), .with_request(rcstr!("."))
.with_affecting_sources(ResolvedVc::deref_vec(affecting_sources)),
) )
} }
......
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