module: disable package self resolution when name contains a colon#37290
module: disable package self resolution when name contains a colon#37290aduh95 wants to merge 3 commits intonodejs:masterfrom
Conversation
There is a discrepancy between package self resolution in CJS and ESM contexts. This commit fixes this by forbidding self-resolution of packages whose names contain a colon. q
8aa43f4 to
34dcb4a
Compare
|
Does this PR affect any other CJS resolution for oaths with a colon, or only the new self-resolution feature? |
Only CJS self-resolve, if the specifier contains a if (selfResolved) {
if (StringPrototypeIncludes(request, ':')) {
throw new ERR_INVALID_MODULE_SPECIFIER(
request, 'is not a valid package name', parentPath);
} |
|
It seems really strange that npm's requirements aren't the whole of the ecosystem, and any linked dependency (including in a workspace/monorepo) might never be published, and might use a colon, and might be relying on this feature. |
That's a good point, and I guess it's difficult to measure…
On that point, on Windows, I believe it currently works only from within, but not from outside. |
ljharb
left a comment
There was a problem hiding this comment.
it seems like we're faced with either an inconsistency between windows and non-windows, or an inconsistency between general CJS requireability, and CJS self-resolution. Personally the former seems more palatable. What do others think?
|
|
||
| Self-referencing is available only if `package.json` has [`"exports"`][], and | ||
| Self-referencing is available only if `package.json` has a [`"name"`][] that | ||
| does not contain the character `:`, and a [`"exports"`][] field, and |
There was a problem hiding this comment.
| does not contain the character `:`, and a [`"exports"`][] field, and | |
| does not contain the character `:`, and an [`"exports"`][] field, and |
|
The ESM loader currently includes the following validation:
It might be nice to treat |
|
This issue/PR was marked as stalled, it will be automatically closed in 30 days. If it should remain open, please leave a comment explaining why it should remain open. |
|
because using a bot to close issues is hostile |
|
Closing as stalled. |
There is a discrepancy between package self resolution in CJS and ESM contexts. This commit fixes this by forbidding self-resolution of packages whose names contain a colon.
Currently, ESM understands specifier with a column as a URL scheme (E.G.:
file:packagereferencespackageunder the schemefile:), while CJS interpret the specifier as a path, so the colon is just another valid character (I.E.:file:packagereferences a package namedfile:package).I think this should be considered as a bug fix and backported as far as we can, this is especially important considering it affects stuff like
require('node:fs')which should take a different meaning soon (see #37246).Note that while this is technically a breaking change, I'm confident it should not affect the ecosystem as npm already requires a name that doesn't contain colons (https://docs.npmjs.com/creating-a-package-json-file#required-name-and-version-fields), plus package self-resolution usually affects dev-env only.
/cc @nodejs/modules