Skip to content

Commit

Permalink
esm: fix import.meta.resolve crash
Browse files Browse the repository at this point in the history
PR-URL: #55777
Fixes: #55518
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
  • Loading branch information
marco-ippolito authored and aduh95 committed Nov 16, 2024
1 parent 9aeb671 commit 83ba688
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/node_modules.cc
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,10 @@ void BindingData::GetPackageScopeConfig(
}

auto file_url = url::FileURLToPath(realm->env(), *package_json_url);
CHECK(file_url);
if (!file_url) {
url::ThrowInvalidURL(realm->env(), resolved.ToStringView(), std::nullopt);
return;
}
error_context.specifier = resolved.ToString();
auto package_json = GetPackageJSON(realm, *file_url, &error_context);
if (package_json != nullptr) {
Expand Down
12 changes: 12 additions & 0 deletions test/es-module/test-esm-import-meta-resolve.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,15 @@ await assert.rejects(import('data:text/javascript,export default import.meta.res
stdout: 'http://example.com/\n',
});
}

{
const result = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-import-meta-resolve',
'--eval',
'import.meta.resolve("foo", "http://example.com/bar.js")',
]);
assert.match(result.stderr, /ERR_INVALID_URL/);
assert.strictEqual(result.stdout, '');
assert.strictEqual(result.code, 1);
}

0 comments on commit 83ba688

Please sign in to comment.