Skip to content

Commit 740c958

Browse files
Derek Lewiscodebytere
Derek Lewis
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: #33438 Co-authored-by: Guy Bedford <[email protected]> PR-URL: #34595 Backport-PR-URL: #35385 Reviewed-By: Jan Krems <[email protected]> Reviewed-By: Mary Marchini <[email protected]>
1 parent cecc193 commit 740c958

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');
@@ -276,20 +280,19 @@ function readPackage(requestPath) {
276280
}
277281

278282
function readPackageScope(checkPath) {
279-
const rootSeparatorIndex = checkPath.indexOf(path.sep);
283+
const rootSeparatorIndex = StringPrototypeIndexOf(checkPath, sep);
280284
let separatorIndex;
281-
while (
282-
(separatorIndex = checkPath.lastIndexOf(path.sep)) > rootSeparatorIndex
283-
) {
284-
checkPath = checkPath.slice(0, separatorIndex);
285-
if (checkPath.endsWith(path.sep + 'node_modules'))
285+
do {
286+
separatorIndex = StringPrototypeLastIndexOf(checkPath, sep);
287+
checkPath = StringPrototypeSlice(checkPath, 0, separatorIndex);
288+
if (StringPrototypeEndsWith(checkPath, sep + 'node_modules'))
286289
return false;
287-
const pjson = readPackage(checkPath);
290+
const pjson = readPackage(checkPath + sep);
288291
if (pjson) return {
292+
data: pjson,
289293
path: checkPath,
290-
data: pjson
291294
};
292-
}
295+
} while (separatorIndex > rootSeparatorIndex);
293296
return false;
294297
}
295298

0 commit comments

Comments
 (0)