Skip to content

Commit 0347574

Browse files
Derek Lewisnodejs-github-bot
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 Reviewed-By: Jan Krems <[email protected]> Reviewed-By: Mary Marchini <[email protected]>
1 parent 5f78dea commit 0347574

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
@@ -1864,11 +1864,11 @@ _conditions_)
18641864

18651865
> 1. Let _scopeURL_ be _url_.
18661866
> 1. While _scopeURL_ is not the file system root,
1867+
> 1. Set _scopeURL_ to the parent URL of _scopeURL_.
18671868
> 1. If _scopeURL_ ends in a _"node_modules"_ path segment, return **null**.
18681869
> 1. Let _pjson_ be the result of **READ_PACKAGE_JSON**(_scopeURL_).
18691870
> 1. If _pjson_ is not **null**, then
18701871
> 1. Return _pjson_.
1871-
> 1. Set _scopeURL_ to the parent URL of _scopeURL_.
18721872
> 1. Return **null**.
18731873

18741874
**READ_PACKAGE_JSON**(_packageURL_)

lib/internal/modules/cjs/loader.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ const {
4646
SafeMap,
4747
SafeWeakMap,
4848
String,
49+
StringPrototypeEndsWith,
50+
StringPrototypeIndexOf,
51+
StringPrototypeLastIndexOf,
4952
StringPrototypeMatch,
5053
StringPrototypeSlice,
5154
StringPrototypeStartsWith,
@@ -64,6 +67,7 @@ const assert = require('internal/assert');
6467
const fs = require('fs');
6568
const internalFS = require('internal/fs/utils');
6669
const path = require('path');
70+
const { sep } = path;
6771
const { emitWarningSync } = require('internal/process/warning');
6872
const { internalModuleStat } = internalBinding('fs');
6973
const packageJsonReader = require('internal/modules/package_json_reader');
@@ -296,20 +300,19 @@ function readPackage(requestPath) {
296300
}
297301

298302
function readPackageScope(checkPath) {
299-
const rootSeparatorIndex = checkPath.indexOf(path.sep);
303+
const rootSeparatorIndex = StringPrototypeIndexOf(checkPath, sep);
300304
let separatorIndex;
301-
while (
302-
(separatorIndex = checkPath.lastIndexOf(path.sep)) > rootSeparatorIndex
303-
) {
304-
checkPath = checkPath.slice(0, separatorIndex);
305-
if (checkPath.endsWith(path.sep + 'node_modules'))
305+
do {
306+
separatorIndex = StringPrototypeLastIndexOf(checkPath, sep);
307+
checkPath = StringPrototypeSlice(checkPath, 0, separatorIndex);
308+
if (StringPrototypeEndsWith(checkPath, sep + 'node_modules'))
306309
return false;
307-
const pjson = readPackage(checkPath);
310+
const pjson = readPackage(checkPath + sep);
308311
if (pjson) return {
312+
data: pjson,
309313
path: checkPath,
310-
data: pjson
311314
};
312-
}
315+
} while (separatorIndex > rootSeparatorIndex);
313316
return false;
314317
}
315318

0 commit comments

Comments
 (0)