Skip to content

Commit fb85279

Browse files
authored
esm: do not interpret "main" as a URL
As documented, its value is a path. PR-URL: #55003 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent fbc6fcb commit fb85279

File tree

3 files changed

+26
-46
lines changed

3 files changed

+26
-46
lines changed

lib/internal/modules/esm/resolve.js

+13-14
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const {
2222
StringPrototypeStartsWith,
2323
encodeURIComponent,
2424
} = primordials;
25+
const assert = require('internal/assert');
2526
const internalFS = require('internal/fs/utils');
2627
const { BuiltinModule } = require('internal/bootstrap/realm');
2728
const { realpathSync } = require('fs');
@@ -117,18 +118,17 @@ function emitInvalidSegmentDeprecation(target, request, match, pjsonUrl, interna
117118
* Emits a deprecation warning if the given URL is a module and
118119
* the package.json file does not define a "main" or "exports" field.
119120
* @param {URL} url - The URL of the module being resolved.
120-
* @param {URL} packageJSONUrl - The URL of the package.json file for the module.
121+
* @param {string} path - The path of the module being resolved.
122+
* @param {string} pkgPath - The path of the parent dir of the package.json file for the module.
121123
* @param {string | URL} [base] - The base URL for the module being resolved.
122124
* @param {string} [main] - The "main" field from the package.json file.
123125
*/
124-
function emitLegacyIndexDeprecation(url, packageJSONUrl, base, main) {
126+
function emitLegacyIndexDeprecation(url, path, pkgPath, base, main) {
125127
if (process.noDeprecation) {
126128
return;
127129
}
128130
const format = defaultGetFormatWithoutErrors(url);
129131
if (format !== 'module') { return; }
130-
const path = fileURLToPath(url);
131-
const pkgPath = fileURLToPath(new URL('.', packageJSONUrl));
132132
const basePath = fileURLToPath(base);
133133
if (!main) {
134134
process.emitWarning(
@@ -196,20 +196,19 @@ const legacyMainResolveExtensionsIndexes = {
196196
* @returns {URL}
197197
*/
198198
function legacyMainResolve(packageJSONUrl, packageConfig, base) {
199-
const packageJsonUrlString = packageJSONUrl.href;
200-
201-
if (typeof packageJsonUrlString !== 'string') {
202-
throw new ERR_INVALID_ARG_TYPE('packageJSONUrl', ['URL'], packageJSONUrl);
203-
}
199+
assert(isURL(packageJSONUrl));
200+
const pkgPath = fileURLToPath(new URL('.', packageJSONUrl));
204201

205202
const baseStringified = isURL(base) ? base.href : base;
206203

207-
const resolvedOption = FSLegacyMainResolve(packageJsonUrlString, packageConfig.main, baseStringified);
204+
const resolvedOption = FSLegacyMainResolve(pkgPath, packageConfig.main, baseStringified);
208205

209-
const baseUrl = resolvedOption <= legacyMainResolveExtensionsIndexes.kResolvedByMainIndexNode ? `./${packageConfig.main}` : '';
210-
const resolvedUrl = new URL(baseUrl + legacyMainResolveExtensions[resolvedOption], packageJSONUrl);
206+
const maybeMain = resolvedOption <= legacyMainResolveExtensionsIndexes.kResolvedByMainIndexNode ?
207+
packageConfig.main || './' : '';
208+
const resolvedPath = resolve(pkgPath, maybeMain + legacyMainResolveExtensions[resolvedOption]);
209+
const resolvedUrl = pathToFileURL(resolvedPath);
211210

212-
emitLegacyIndexDeprecation(resolvedUrl, packageJSONUrl, base, packageConfig.main);
211+
emitLegacyIndexDeprecation(resolvedUrl, resolvedPath, pkgPath, base, packageConfig.main);
213212

214213
return resolvedUrl;
215214
}
@@ -790,8 +789,8 @@ function packageResolve(specifier, base, conditions) {
790789
// ResolveSelf
791790
const packageConfig = packageJsonReader.getPackageScopeConfig(base);
792791
if (packageConfig.exists) {
793-
const packageJSONUrl = pathToFileURL(packageConfig.pjsonPath);
794792
if (packageConfig.exports != null && packageConfig.name === packageName) {
793+
const packageJSONUrl = pathToFileURL(packageConfig.pjsonPath);
795794
return packageExportsResolve(
796795
packageJSONUrl, packageSubpath, packageConfig, base, conditions);
797796
}

src/node_file.cc

+4-31
Original file line numberDiff line numberDiff line change
@@ -3331,37 +3331,18 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
33313331
Environment* env = Environment::GetCurrent(args);
33323332
auto isolate = env->isolate();
33333333

3334-
Utf8Value utf8_package_json_url(isolate, args[0]);
3335-
auto package_json_url =
3336-
ada::parse<ada::url_aggregator>(utf8_package_json_url.ToStringView());
3337-
3338-
if (!package_json_url) {
3339-
THROW_ERR_INVALID_URL(isolate, "Invalid URL");
3340-
return;
3341-
}
3334+
auto utf8_package_path = Utf8Value(isolate, args[0]).ToString();
33423335

33433336
std::string package_initial_file = "";
33443337

3345-
ada::result<ada::url_aggregator> file_path_url;
33463338
std::optional<std::string> initial_file_path;
33473339
std::string file_path;
33483340

33493341
if (args.Length() >= 2 && args[1]->IsString()) {
33503342
auto package_config_main = Utf8Value(isolate, args[1]).ToString();
33513343

3352-
file_path_url = ada::parse<ada::url_aggregator>(
3353-
std::string("./") + package_config_main, &package_json_url.value());
3354-
3355-
if (!file_path_url) {
3356-
THROW_ERR_INVALID_URL(isolate, "Invalid URL");
3357-
return;
3358-
}
3359-
3360-
initial_file_path = node::url::FileURLToPath(env, *file_path_url);
3361-
if (!initial_file_path.has_value()) {
3362-
return;
3363-
}
3364-
3344+
initial_file_path =
3345+
PathResolve(env, {utf8_package_path, package_config_main});
33653346
FromNamespacedPath(&initial_file_path.value());
33663347

33673348
package_initial_file = *initial_file_path;
@@ -3392,15 +3373,7 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
33923373
}
33933374
}
33943375

3395-
file_path_url =
3396-
ada::parse<ada::url_aggregator>("./index", &package_json_url.value());
3397-
3398-
if (!file_path_url) {
3399-
THROW_ERR_INVALID_URL(isolate, "Invalid URL");
3400-
return;
3401-
}
3402-
3403-
initial_file_path = node::url::FileURLToPath(env, *file_path_url);
3376+
initial_file_path = PathResolve(env, {utf8_package_path, "./index"});
34043377
if (!initial_file_path.has_value()) {
34053378
return;
34063379
}

test/es-module/test-cjs-legacyMainResolve.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ describe('legacyMainResolve', () => {
8282
{},
8383
''
8484
),
85-
{ message: /instance of URL/, code: 'ERR_INVALID_ARG_TYPE' },
85+
{ code: 'ERR_INTERNAL_ASSERTION' },
8686
);
8787
});
8888

@@ -166,4 +166,12 @@ describe('legacyMainResolve', () => {
166166
{ message: /"base" argument must be/, code: 'ERR_INVALID_ARG_TYPE' },
167167
);
168168
});
169+
170+
it('should interpret main as a path, not a URL', () => {
171+
const packageJsonUrl = fixtures.fileURL('/es-modules/legacy-main-resolver/package.json');
172+
assert.deepStrictEqual(
173+
legacyMainResolve(packageJsonUrl, { main: '../folder%25with percentage#/' }, packageJsonUrl),
174+
fixtures.fileURL('/es-modules/folder%25with percentage#/index.js'),
175+
);
176+
});
169177
});

0 commit comments

Comments
 (0)