Skip to content

Commit 88d4686

Browse files
Derek LewisMylesBorins
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 ab7d2ac commit 88d4686

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
@@ -45,6 +45,9 @@ const {
4545
RegExpPrototypeTest,
4646
SafeMap,
4747
String,
48+
StringPrototypeEndsWith,
49+
StringPrototypeIndexOf,
50+
StringPrototypeLastIndexOf,
4851
StringPrototypeMatch,
4952
StringPrototypeSlice,
5053
StringPrototypeStartsWith,
@@ -62,6 +65,7 @@ const assert = require('internal/assert');
6265
const fs = require('fs');
6366
const internalFS = require('internal/fs/utils');
6467
const path = require('path');
68+
const { sep } = path;
6569
const { emitWarningSync } = require('internal/process/warning');
6670
const { internalModuleStat } = internalBinding('fs');
6771
const packageJsonReader = require('internal/modules/package_json_reader');
@@ -282,20 +286,19 @@ function readPackage(requestPath) {
282286
}
283287

284288
function readPackageScope(checkPath) {
285-
const rootSeparatorIndex = checkPath.indexOf(path.sep);
289+
const rootSeparatorIndex = StringPrototypeIndexOf(checkPath, sep);
286290
let separatorIndex;
287-
while (
288-
(separatorIndex = checkPath.lastIndexOf(path.sep)) > rootSeparatorIndex
289-
) {
290-
checkPath = checkPath.slice(0, separatorIndex);
291-
if (checkPath.endsWith(path.sep + 'node_modules'))
291+
do {
292+
separatorIndex = StringPrototypeLastIndexOf(checkPath, sep);
293+
checkPath = StringPrototypeSlice(checkPath, 0, separatorIndex);
294+
if (StringPrototypeEndsWith(checkPath, sep + 'node_modules'))
292295
return false;
293-
const pjson = readPackage(checkPath);
296+
const pjson = readPackage(checkPath + sep);
294297
if (pjson) return {
298+
data: pjson,
295299
path: checkPath,
296-
data: pjson
297300
};
298-
}
301+
} while (separatorIndex > rootSeparatorIndex);
299302
return false;
300303
}
301304

0 commit comments

Comments
 (0)