Unverified Commit ec7b33e3 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

[Turbopack] fix require.resolve self hanging (#69793)

### What?

evaluation require.resolve tries to resolve and process a module during
module analytics. Since analytics is part of processing a module this
can create a call cycle when `require.resolve("./self")` is used. This
happens in some npm packages and caused Turbopack to hange.

This PR fixes that problem by only resolving the module and not
processing it when using `require.resolve`.

In addition to that it adds a panic when a call cycle occurs. The next
time we mess it up and create a call cycle this will crash Turbopack
instead of hanging, which makes it much easier to find the problem.

fixes PACK-3227
parent a9bfc646
...@@ -189,6 +189,13 @@ impl<C: AggregationContext> PreparedInternalOperation<C> for PreparedNotifyNewFo ...@@ -189,6 +189,13 @@ impl<C: AggregationContext> PreparedInternalOperation<C> for PreparedNotifyNewFo
let follower = ctx.node(&follower_id); let follower = ctx.node(&follower_id);
let follower_aggregation_number = follower.aggregation_number(); let follower_aggregation_number = follower.aggregation_number();
if follower_aggregation_number == upper_aggregation_number { if follower_aggregation_number == upper_aggregation_number {
if upper_id == follower_id {
panic!(
"Cycle in call graph (A function calls itself \
recursively with the same arguments. This will never \
finish and would hang indefinitely.)"
);
}
increase_aggregation_number_internal( increase_aggregation_number_internal(
ctx, ctx,
balance_queue, balance_queue,
......
...@@ -2752,10 +2752,6 @@ async fn resolve_package_internal_with_imports_field( ...@@ -2752,10 +2752,6 @@ async fn resolve_package_internal_with_imports_field(
.await .await
} }
async fn is_unresolveable(result: Vc<ModuleResolveResult>) -> Result<bool> {
Ok(*result.resolve().await?.is_unresolveable().await?)
}
pub async fn handle_resolve_error( pub async fn handle_resolve_error(
result: Vc<ModuleResolveResult>, result: Vc<ModuleResolveResult>,
reference_type: Value<ReferenceType>, reference_type: Value<ReferenceType>,
...@@ -2765,25 +2761,90 @@ pub async fn handle_resolve_error( ...@@ -2765,25 +2761,90 @@ pub async fn handle_resolve_error(
severity: Vc<IssueSeverity>, severity: Vc<IssueSeverity>,
source: Option<Vc<IssueSource>>, source: Option<Vc<IssueSource>>,
) -> Result<Vc<ModuleResolveResult>> { ) -> Result<Vc<ModuleResolveResult>> {
async fn is_unresolveable(result: Vc<ModuleResolveResult>) -> Result<bool> {
Ok(*result.resolve().await?.is_unresolveable().await?)
}
Ok(match is_unresolveable(result).await { Ok(match is_unresolveable(result).await {
Ok(unresolveable) => { Ok(unresolveable) => {
if unresolveable { if unresolveable {
ResolvingIssue { emit_unresolveable_issue(
severity, severity,
file_path: origin_path, origin_path,
request_type: format!("{} request", reference_type.into_value()), reference_type,
request, request,
resolve_options, resolve_options,
error_message: None,
source, source,
);
} }
.cell()
.emit(); result
}
Err(err) => {
emit_resolve_error_issue(
severity,
origin_path,
reference_type,
request,
resolve_options,
err,
source,
);
ModuleResolveResult::unresolveable().cell()
}
})
}
pub async fn handle_resolve_source_error(
result: Vc<ResolveResult>,
reference_type: Value<ReferenceType>,
origin_path: Vc<FileSystemPath>,
request: Vc<Request>,
resolve_options: Vc<ResolveOptions>,
severity: Vc<IssueSeverity>,
source: Option<Vc<IssueSource>>,
) -> Result<Vc<ResolveResult>> {
async fn is_unresolveable(result: Vc<ResolveResult>) -> Result<bool> {
Ok(*result.resolve().await?.is_unresolveable().await?)
}
Ok(match is_unresolveable(result).await {
Ok(unresolveable) => {
if unresolveable {
emit_unresolveable_issue(
severity,
origin_path,
reference_type,
request,
resolve_options,
source,
);
} }
result result
} }
Err(err) => { Err(err) => {
emit_resolve_error_issue(
severity,
origin_path,
reference_type,
request,
resolve_options,
err,
source,
);
ResolveResult::unresolveable().cell()
}
})
}
fn emit_resolve_error_issue(
severity: Vc<IssueSeverity>,
origin_path: Vc<FileSystemPath>,
reference_type: Value<ReferenceType>,
request: Vc<Request>,
resolve_options: Vc<ResolveOptions>,
err: anyhow::Error,
source: Option<Vc<IssueSource>>,
) {
ResolvingIssue { ResolvingIssue {
severity, severity,
file_path: origin_path, file_path: origin_path,
...@@ -2795,9 +2856,27 @@ pub async fn handle_resolve_error( ...@@ -2795,9 +2856,27 @@ pub async fn handle_resolve_error(
} }
.cell() .cell()
.emit(); .emit();
ModuleResolveResult::unresolveable().cell() }
fn emit_unresolveable_issue(
severity: Vc<IssueSeverity>,
origin_path: Vc<FileSystemPath>,
reference_type: Value<ReferenceType>,
request: Vc<Request>,
resolve_options: Vc<ResolveOptions>,
source: Option<Vc<IssueSource>>,
) {
ResolvingIssue {
severity,
file_path: origin_path,
request_type: format!("{} request", reference_type.into_value()),
request,
resolve_options,
error_message: None,
source,
} }
}) .cell()
.emit();
} }
// TODO this should become a TaskInput instead of a Vc // TODO this should become a TaskInput instead of a Vc
......
...@@ -72,7 +72,7 @@ use turbopack_core::{ ...@@ -72,7 +72,7 @@ use turbopack_core::{
source_map::{GenerateSourceMap, OptionSourceMap, SourceMap}, source_map::{GenerateSourceMap, OptionSourceMap, SourceMap},
}; };
use turbopack_resolve::{ use turbopack_resolve::{
ecmascript::{apply_cjs_specific_options, cjs_resolve}, ecmascript::{apply_cjs_specific_options, cjs_resolve_source},
typescript::tsconfig, typescript::tsconfig,
}; };
use turbopack_swc_utils::emitter::IssueEmitter; use turbopack_swc_utils::emitter::IssueEmitter;
...@@ -2383,14 +2383,14 @@ async fn require_resolve_visitor( ...@@ -2383,14 +2383,14 @@ async fn require_resolve_visitor(
Ok(if args.len() == 1 { Ok(if args.len() == 1 {
let pat = js_value_to_pattern(&args[0]); let pat = js_value_to_pattern(&args[0]);
let request = Request::parse(Value::new(pat.clone())); let request = Request::parse(Value::new(pat.clone()));
let resolved = cjs_resolve(origin, request, None, IssueSeverity::Warning.cell()) let resolved = cjs_resolve_source(origin, request, None, IssueSeverity::Warning.cell())
.resolve() .resolve()
.await?; .await?;
let mut values = resolved let mut values = resolved
.primary_modules() .primary_sources()
.await? .await?
.iter() .iter()
.map(|&module| async move { require_resolve(module.ident().path()).await }) .map(|&source| async move { require_resolve(source.ident().path()).await })
.try_join() .try_join()
.await?; .await?;
......
...@@ -4,14 +4,14 @@ use turbopack_core::{ ...@@ -4,14 +4,14 @@ use turbopack_core::{
issue::{IssueSeverity, IssueSource}, issue::{IssueSeverity, IssueSource},
reference_type::{CommonJsReferenceSubType, EcmaScriptModulesReferenceSubType, ReferenceType}, reference_type::{CommonJsReferenceSubType, EcmaScriptModulesReferenceSubType, ReferenceType},
resolve::{ resolve::{
handle_resolve_error, handle_resolve_error, handle_resolve_source_error,
options::{ options::{
ConditionValue, ResolutionConditions, ResolveInPackage, ResolveIntoPackage, ConditionValue, ResolutionConditions, ResolveInPackage, ResolveIntoPackage,
ResolveOptions, ResolveOptions,
}, },
origin::{ResolveOrigin, ResolveOriginExt}, origin::{ResolveOrigin, ResolveOriginExt},
parse::Request, parse::Request,
ModuleResolveResult, resolve, ModuleResolveResult, ResolveResult,
}, },
}; };
/// Retrieves the [ResolutionConditions] of both the "into" package (allowing a /// Retrieves the [ResolutionConditions] of both the "into" package (allowing a
...@@ -103,6 +103,37 @@ pub async fn cjs_resolve( ...@@ -103,6 +103,37 @@ pub async fn cjs_resolve(
specific_resolve(origin, request, options, ty, issue_severity, issue_source).await specific_resolve(origin, request, options, ty, issue_severity, issue_source).await
} }
#[turbo_tasks::function]
pub async fn cjs_resolve_source(
origin: Vc<Box<dyn ResolveOrigin>>,
request: Vc<Request>,
issue_source: Option<Vc<IssueSource>>,
issue_severity: Vc<IssueSeverity>,
) -> Result<Vc<ResolveResult>> {
// TODO pass CommonJsReferenceSubType
let ty = Value::new(ReferenceType::CommonJs(CommonJsReferenceSubType::Undefined));
let options = apply_cjs_specific_options(origin.resolve_options(ty.clone()))
.resolve()
.await?;
let result = resolve(
origin.origin_path().parent().resolve().await?,
ty.clone(),
request,
options,
);
handle_resolve_source_error(
result,
ty,
origin.origin_path(),
request,
options,
issue_severity,
issue_source,
)
.await
}
async fn specific_resolve( async fn specific_resolve(
origin: Vc<Box<dyn ResolveOrigin>>, origin: Vc<Box<dyn ResolveOrigin>>,
request: Vc<Request>, request: Vc<Request>,
......
...@@ -9,3 +9,15 @@ it('should support require.resolve with extensions', () => { ...@@ -9,3 +9,15 @@ it('should support require.resolve with extensions', () => {
'[project]/turbopack/crates/turbopack-tests/tests/execution/turbopack/resolving/require-resolve/input/resolved.js [test] (ecmascript)' '[project]/turbopack/crates/turbopack-tests/tests/execution/turbopack/resolving/require-resolve/input/resolved.js [test] (ecmascript)'
) )
}) })
it('should support require.resolve on the current module', () => {
expect(require.resolve('./index.js')).toBe(
'[project]/turbopack/crates/turbopack-tests/tests/execution/turbopack/resolving/require-resolve/input/index.js [test] (ecmascript)'
)
// Check if static evaluation is not hanging
if (require.resolve('./index.js') == "") {
throw new Error("no no");
}
})
export {};
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