Skip to content

Commit

Permalink
esm: fix import.meta.resolve crash
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-ippolito committed Nov 8, 2024
1 parent 6af5c4e commit b8cb866
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
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
13 changes: 12 additions & 1 deletion test/es-module/test-esm-import-meta-resolve.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Flags: --experimental-import-meta-resolve
import { spawnPromisified } from '../common/index.mjs';
import { fileURL as fixturesFileURL } from '../common/fixtures.mjs';
import { fileURL as fixturesFileURL, path } from '../common/fixtures.mjs';
import assert from 'assert';
import { spawn } from 'child_process';
import { execPath } from 'process';
Expand Down Expand Up @@ -106,3 +106,14 @@ 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',
path('import-meta-resolve-loader.js'),
]);
assert.match(result.stderr, /ERR_INVALID_URL/);
assert.strictEqual(result.stdout, '');
assert.strictEqual(result.code, 1);
}
1 change: 1 addition & 0 deletions test/fixtures/import-meta-resolve-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import.meta.resolve('foo', 'http://example.com/bar.js');

0 comments on commit b8cb866

Please sign in to comment.