Skip to content

Commit b8cb866

Browse files
esm: fix import.meta.resolve crash
1 parent 6af5c4e commit b8cb866

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

src/node_modules.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,10 @@ void BindingData::GetPackageScopeConfig(
412412
}
413413

414414
auto file_url = url::FileURLToPath(realm->env(), *package_json_url);
415-
CHECK(file_url);
415+
if (!file_url) {
416+
url::ThrowInvalidURL(realm->env(), resolved.ToStringView(), std::nullopt);
417+
return;
418+
}
416419
error_context.specifier = resolved.ToString();
417420
auto package_json = GetPackageJSON(realm, *file_url, &error_context);
418421
if (package_json != nullptr) {

test/es-module/test-esm-import-meta-resolve.mjs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Flags: --experimental-import-meta-resolve
22
import { spawnPromisified } from '../common/index.mjs';
3-
import { fileURL as fixturesFileURL } from '../common/fixtures.mjs';
3+
import { fileURL as fixturesFileURL, path } from '../common/fixtures.mjs';
44
import assert from 'assert';
55
import { spawn } from 'child_process';
66
import { execPath } from 'process';
@@ -106,3 +106,14 @@ await assert.rejects(import('data:text/javascript,export default import.meta.res
106106
stdout: 'http://example.com/\n',
107107
});
108108
}
109+
110+
{
111+
const result = await spawnPromisified(execPath, [
112+
'--no-warnings',
113+
'--experimental-import-meta-resolve',
114+
path('import-meta-resolve-loader.js'),
115+
]);
116+
assert.match(result.stderr, /ERR_INVALID_URL/);
117+
assert.strictEqual(result.stdout, '');
118+
assert.strictEqual(result.code, 1);
119+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import.meta.resolve('foo', 'http://example.com/bar.js');

0 commit comments

Comments
 (0)