Description
📗 API Reference Docs Problem
- Version: v16.11.1
- Platform: Darwin
- Subsystem: esm
Location
Resolver Algorithm Specification
Affected URL(s):
Description
In the resolver algorithm specification, READ_PACKAGE_SCOPE
returns a parsed JSON value but is then used as a URL. Surely this is a mistake. In detail:
-
The
READ_PACKAGE_JSON
function either returns null, throws an error, or returns a parsed JSON value:READ_PACKAGE_JSON(packageURL)
- Let pjsonURL be the resolution of "package.json" within packageURL.
- If the file at pjsonURL does not exist, then
- Return null.
- If the file at packageURL does not parse as valid JSON, then
- Throw an Invalid Package Configuration error.
- Return the parsed JSON source of the file at pjsonURL.
-
The
READ_PACKAGE_SCOPE
function either returnsnull
or the result of callingREAD_PACKAGE_JSON
, so it also returns a parsed JSON value (called pjson below):READ_PACKAGE_SCOPE(url)
- Let scopeURL be url.
- While scopeURL is not the file system root,
- Set scopeURL to the parent URL of scopeURL.
- If scopeURL ends in a "node_modules" path segment, return null.
- Let pjson be the result of READ_PACKAGE_JSON(scopeURL).
- If pjson is not null, then
- Return pjson.
- Return null.
-
The
ESM_FORMAT
function callsREAD_PACKAGE_SCOPE
and uses the result as a parsed JSON object (called pjson below):ESM_FORMAT(url)
- Assert: url corresponds to an existing file.
- Let pjson be the result of READ_PACKAGE_SCOPE(url).
- If url ends in ".mjs", then
- Return "module".
- If url ends in ".cjs", then
- Return "commonjs".
- If pjson?.type exists and is "module", then
- If url ends in ".js", then
- Return "module".
- Throw an Unsupported File Extension error.
- If url ends in ".js", then
- Otherwise,
- Throw an Unsupported File Extension error.
-
However, both
PACKAGE_SELF_RESOLVE
andPACKAGE_IMPORTS_RESOLVE
callREAD_PACKAGE_SCOPE
and use the result as a URL (called packageURL below). In particular, they pass the result toREAD_PACKAGE_JSON
again. I assumeREAD_PACKAGE_JSON
is not meant to be called on its own result:PACKAGE_SELF_RESOLVE(packageName, packageSubpath, parentURL)
- Let packageURL be the result of READ_PACKAGE_SCOPE(parentURL).
- If packageURL is null, then
- Return undefined.
- Let pjson be the result of READ_PACKAGE_JSON(packageURL).
- If pjson is null or if pjson.exports is null or
undefined, then- Return undefined.
- If pjson.name is equal to packageName, then
- Return the result of PACKAGE_EXPORTS_RESOLVE(packageURL,
packageSubpath, pjson.exports, defaultConditions).
- Return the result of PACKAGE_EXPORTS_RESOLVE(packageURL,
- Otherwise, return undefined.
PACKAGE_IMPORTS_RESOLVE(specifier, parentURL, conditions)
- Assert: specifier begins with "#".
- If specifier is exactly equal to "#" or starts with "#/", then
- Throw an Invalid Module Specifier error.
- Let packageURL be the result of READ_PACKAGE_SCOPE(parentURL).
- If packageURL is not null, then
- Let pjson be the result of READ_PACKAGE_JSON(packageURL).
- If pjson.imports is a non-null Object, then
- Let resolved be the result of
PACKAGE_IMPORTS_EXPORTS_RESOLVE(
specifier, pjson.imports, packageURL, true, conditions). - If resolved is not null or undefined, return resolved.
- Let resolved be the result of
- Throw a Package Import Not Defined error.
If this was type checked, this would be a type error. I'm not sure what the right fix is. Maybe READ_PACKAGE_SCOPE
should return a URL instead? Anyway I think the docs should be updated even if it's possible to figure out what they actually mean, because the mismatch is unnecessarily confusing.
- I would like to work on this issue and
submit a pull request.