Skip to content

Commit

Permalink
module: fix check for package.json at volume root
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
DerekNonGeneric authored and guybedford committed Sep 28, 2020
1 parent 7e20df2 commit d6af3ba
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -1854,11 +1854,11 @@ _conditions_)

> 1. Let _scopeURL_ be _url_.
> 1. While _scopeURL_ is not the file system root,
> 1. Set _scopeURL_ to the parent URL of _scopeURL_.
> 1. If _scopeURL_ ends in a _"node_modules"_ path segment, return **null**.
> 1. Let _pjson_ be the result of **READ_PACKAGE_JSON**(_scopeURL_).
> 1. If _pjson_ is not **null**, then
> 1. Return _pjson_.
> 1. Set _scopeURL_ to the parent URL of _scopeURL_.
> 1. Return **null**.

**READ_PACKAGE_JSON**(_packageURL_)
Expand Down
21 changes: 12 additions & 9 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ const {
RegExpPrototypeTest,
SafeMap,
SafeSet,
StringPrototypeEndsWith,
StringPrototypeIndexOf,
StringPrototypeLastIndexOf,
StringPrototypeMatch,
StringPrototypeSlice,
StringPrototypeStartsWith,
Expand All @@ -58,6 +61,7 @@ const assert = require('internal/assert');
const fs = require('fs');
const internalFS = require('internal/fs/utils');
const path = require('path');
const { sep } = path;
const { internalModuleStat } = internalBinding('fs');
const packageJsonReader = require('internal/modules/package_json_reader');
const { safeGetenv } = internalBinding('credentials');
Expand Down Expand Up @@ -274,20 +278,19 @@ function readPackage(requestPath) {
}

function readPackageScope(checkPath) {
const rootSeparatorIndex = checkPath.indexOf(path.sep);
const rootSeparatorIndex = StringPrototypeIndexOf(checkPath, sep);
let separatorIndex;
while (
(separatorIndex = checkPath.lastIndexOf(path.sep)) > rootSeparatorIndex
) {
checkPath = checkPath.slice(0, separatorIndex);
if (checkPath.endsWith(path.sep + 'node_modules'))
do {
separatorIndex = StringPrototypeLastIndexOf(checkPath, sep);
checkPath = StringPrototypeSlice(checkPath, 0, separatorIndex);
if (StringPrototypeEndsWith(checkPath, sep + 'node_modules'))
return false;
const pjson = readPackage(checkPath);
const pjson = readPackage(checkPath + sep);
if (pjson) return {
data: pjson,
path: checkPath,
data: pjson
};
}
} while (separatorIndex > rootSeparatorIndex);
return false;
}

Expand Down

0 comments on commit d6af3ba

Please sign in to comment.