Repository: vercel/next.js
File: turbopack/crates/turbopack-core/src/resolve/mod.rs (Rust)

Code before:

    // Directory matches must be resolved AFTER file matches
    for m in matches.iter() {
        if let PatternMatch::Directory(matched_pattern, path) = m {
            results.push(
                resolve_into_folder(path.clone(), options).with_request(matched_pattern.clone()),
            );
        }
    }

    Ok(merge_results(results))
}

#[tracing::instrument(level = Level::TRACE, skip_all)]
async fn apply_in_package(
    lookup_path: FileSystemPath,
    options: Vc<ResolveOptions>,
    options_value: &ResolveOptions,
    get_request: impl Fn(&FileSystemPath) -> Option<RcStr>,
    query: RcStr,
    fragment: RcStr,
) -> Result<Option<Vc<ResolveResult>>> {
    // Early return if no in_package rules
    if options_value.in_package.is_empty() {
        return Ok(None);
    }

    // Check alias field for module aliases first
    for in_package in options_value.in_package.iter() {
        // resolve_module_request is called when importing a node
        // module, not a PackageInternal one, so the imports field
        // doesn't apply.
        let ResolveInPackage::AliasField(field) = in_package else {
            continue;
        };

        let FindContextFileResult::Found(package_json_path, refs) = &*find_context_file(
            lookup_path.clone(),
            package_json().resolve().await?,
            options_value.collect_affecting_sources,
        )
        .await?
        else {
            continue;
        };

        let read =
            read_package_json(Vc::upcast(FileSource::new(package_json_path.clone()))).await?;
        let Some(package_json) = &*read else {
            continue;
        };

Review comment (suggestion):
I'm surprised that
```
if vec.is_empty(){ return None; }
for i in vec { ... }
return None
```
is really helping? This should make no difference

Code after:
    Ok(merge_results(results))
}

#[tracing::instrument(level = Level::TRACE, skip_all)]
async fn apply_in_package(
    lookup_path: FileSystemPath,
    options: Vc<ResolveOptions>,
    options_value: &ResolveOptions,
    get_request: impl Fn(&FileSystemPath) -> Option<RcStr>,
    query: RcStr,
    fragment: RcStr,
) -> Result<Option<Vc<ResolveResult>>> {
    // Check alias field for module aliases first
    for in_package in options_value.in_package.iter() {
        // resolve_module_request is called when importing a node
        // module, not a PackageInternal one, so the imports field
        // doesn't apply.
        let ResolveInPackage::AliasField(field) = in_package else {
            continue;
        };

        let FindContextFileResult::Found(package_json_path, refs) = &*find_context_file(
            lookup_path.clone(),
            package_json().resolve().await?,
            options_value.collect_affecting_sources,
        )
        .await?
        else {
            continue;
        };

        let read =
            read_package_json(Vc::upcast(FileSource::new(package_json_path.clone()))).await?;
        let Some(package_json) = &*read else {
            continue;
        };

        let Some(field_value) = package_json[field.as_str()].as_object() else {
            continue;
        };

        let package_path = package_json_path.parent();

        let Some(request) = get_request(&package_path) else {
            continue;
        };

        let value = if let Some(value) = field_value.get(&*request) {
            value
        } else if let Some(request) = request.strip_prefix("./") {
            let Some(value) = field_value.get(request) else {