Repository: awslabs/llrt
File: llrt_core/src/module_loader/resolver.rs (Rust)

Code before:
    match find_the_closest_package_scope(dir) {
        // 2. If no scope was found, return.
        None => {
            return Ok(None);
        },
        Some(path) => {
            package_json_file = fs::read(path.as_ref()).or_throw(ctx)?;
            package_json = simd_json::to_borrowed_value(&mut package_json_file).or_throw(ctx)?;
            // 3. If the SCOPE/package.json "exports" is null or undefined, return.
            if !is_exports_field_exists(&package_json) {
                return Ok(None);
            }
            // 4. If the SCOPE/package.json "name" is not the first segment of X, return.
            if let Some(name) = get_string_field(&package_json, "name") {
                if name != scope {
                    return Ok(None);
                }
            }
        },
    };
    // 5. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(SCOPE),
    //    "." + X.slice("name".length), `package.json` "exports", ["node", "require"])
    //    <a href="esm.md#resolver-algorithm-specification">defined in the ESM resolver</a>.
    // 6. RESOLVE_ESM_MATCH(MATCH)
    if let Ok((path, _)) = package_exports_resolve(&package_json, &name, is_esm) {
        trace!("|  load_package_self(2.c): {}", path);
        return Ok(Some(path.into()));
    }

    Ok(None)
}

fn get_name_and_scope(x: &str) -> (Cow<'_, str>, &str) {
    if let Some((s, n)) = x.split_once('/') {
        (Cow::Owned(["./", n].concat()), s)
    } else {
        (Cow::Borrowed("."), x)
    }
}

// Implementation equivalent to PACKAGE_EXPORTS_RESOLVE including RESOLVE_ESM_MATCH
fn package_exports_resolve<'a>(
    package_json: &'a BorrowedValue<'a>,
    modules_name: &str,
    is_esm: bool,
) -> Result<(&'a str, bool)> {
    let ident = if is_esm { "import" } else { "require" };

    if let BorrowedValue::Object(map) = package_json {
        let is_cjs =
            !matches!(map.get("type"), Some(BorrowedValue::String(ref _type)) if _type == "module");

Review comment (suggestion):
We don't yet know if we need the same implementation here. Even if we did, the `path` is still relative, so adding a prefix at this point would be a mistake.

Code after:
    let package_json: BorrowedValue;
    match find_the_closest_package_scope(dir) {
        // 2. If no scope was found, return.
        None => {
            return Ok(None);
        },
        Some(path) => {
            package_json_file = fs::read(path.as_ref()).or_throw(ctx)?;
            package_json = simd_json::to_borrowed_value(&mut package_json_file).or_throw(ctx)?;
            // 3. If the SCOPE/package.json "exports" is null or undefined, return.
            if !is_exports_field_exists(&package_json) {
                return Ok(None);
            }
            // 4. If the SCOPE/package.json "name" is not the first segment of X, return.
            if let Some(name) = get_string_field(&package_json, "name") {
                if name != scope {
                    return Ok(None);
                }
            }
        },
    };
    // 5. let MATCH = PACKAGE_EXPORTS_RESOLVE(pathToFileURL(SCOPE),
    //    "." + X.slice("name".length), `package.json` "exports", ["node", "require"])
    //    <a href="esm.md#resolver-algorithm-specification">defined in the ESM resolver</a>.
    // 6. RESOLVE_ESM_MATCH(MATCH)
    if let Ok((path, _)) = package_exports_resolve(&package_json, &name, is_esm) {
        trace!("|  load_package_self(2.c): {}", path);
        return Ok(Some(path.into()));
    }

    Ok(None)
}

fn get_name_and_scope(x: &str) -> (Cow<'_, str>, &str) {
    if let Some((s, n)) = x.split_once('/') {
        (Cow::Owned(["./", n].concat()), s)
    } else {
        (Cow::Borrowed("."), x)
    }
}

// Implementation equivalent to PACKAGE_EXPORTS_RESOLVE including RESOLVE_ESM_MATCH
fn package_exports_resolve<'a>(
    package_json: &'a BorrowedValue<'a>,
    modules_name: &str,
    is_esm: bool,
) -> Result<(&'a str, bool)> {
    let ident = if is_esm { "import" } else { "require" };

    if let BorrowedValue::Object(map) = package_json {
        let is_cjs =