Skip to content

Commit d6af3ba

Browse files
DerekNonGenericguybedford
authored andcommitted
module: fix check for package.json at volume root
This patch converts the "read package scope" algorithm's while loop into a do-while loop enabling items at the filesystem root dir to be considered within the scope of a sibling package.json also at the filesystem root dir. Fixes: nodejs#33438 Co-authored-by: Guy Bedford <[email protected]> PR-URL: nodejs#34595 Reviewed-By: Jan Krems <[email protected]> Reviewed-By: Mary Marchini <[email protected]>
1 parent 7e20df2 commit d6af3ba

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

doc/api/esm.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1854,11 +1854,11 @@ _conditions_)
18541854

18551855
> 1. Let _scopeURL_ be _url_.
18561856
> 1. While _scopeURL_ is not the file system root,
1857+
> 1. Set _scopeURL_ to the parent URL of _scopeURL_.
18571858
> 1. If _scopeURL_ ends in a _"node_modules"_ path segment, return **null**.
18581859
> 1. Let _pjson_ be the result of **READ_PACKAGE_JSON**(_scopeURL_).
18591860
> 1. If _pjson_ is not **null**, then
18601861
> 1. Return _pjson_.
1861-
> 1. Set _scopeURL_ to the parent URL of _scopeURL_.
18621862
> 1. Return **null**.
18631863

18641864
**READ_PACKAGE_JSON**(_packageURL_)

lib/internal/modules/cjs/loader.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ const {
4141
RegExpPrototypeTest,
4242
SafeMap,
4343
SafeSet,
44+
StringPrototypeEndsWith,
45+
StringPrototypeIndexOf,
46+
StringPrototypeLastIndexOf,
4447
StringPrototypeMatch,
4548
StringPrototypeSlice,
4649
StringPrototypeStartsWith,
@@ -58,6 +61,7 @@ const assert = require('internal/assert');
5861
const fs = require('fs');
5962
const internalFS = require('internal/fs/utils');
6063
const path = require('path');
64+
const { sep } = path;
6165
const { internalModuleStat } = internalBinding('fs');
6266
const packageJsonReader = require('internal/modules/package_json_reader');
6367
const { safeGetenv } = internalBinding('credentials');
@@ -274,20 +278,19 @@ function readPackage(requestPath) {
274278
}
275279

276280
function readPackageScope(checkPath) {
277-
const rootSeparatorIndex = checkPath.indexOf(path.sep);
281+
const rootSeparatorIndex = StringPrototypeIndexOf(checkPath, sep);
278282
let separatorIndex;
279-
while (
280-
(separatorIndex = checkPath.lastIndexOf(path.sep)) > rootSeparatorIndex
281-
) {
282-
checkPath = checkPath.slice(0, separatorIndex);
283-
if (checkPath.endsWith(path.sep + 'node_modules'))
283+
do {
284+
separatorIndex = StringPrototypeLastIndexOf(checkPath, sep);
285+
checkPath = StringPrototypeSlice(checkPath, 0, separatorIndex);
286+
if (StringPrototypeEndsWith(checkPath, sep + 'node_modules'))
284287
return false;
285-
const pjson = readPackage(checkPath);
288+
const pjson = readPackage(checkPath + sep);
286289
if (pjson) return {
290+
data: pjson,
287291
path: checkPath,
288-
data: pjson
289292
};
290-
}
293+
} while (separatorIndex > rootSeparatorIndex);
291294
return false;
292295
}
293296

0 commit comments

Comments
 (0)