From 740c95819f0468345eebc44792c29c6e82b9820e Mon Sep 17 00:00:00 2001 From: Derek Lewis Date: Sun, 2 Aug 2020 17:27:51 -0400 Subject: [PATCH] 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: https://github.com/nodejs/node/issues/33438 Co-authored-by: Guy Bedford PR-URL: https://github.com/nodejs/node/pull/34595 Backport-PR-URL: https://github.com/nodejs/node/pull/35385 Reviewed-By: Jan Krems Reviewed-By: Mary Marchini --- doc/api/esm.md | 2 +- lib/internal/modules/cjs/loader.js | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/doc/api/esm.md b/doc/api/esm.md index cefa06d66d6f93..341bc4c3cd64f4 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -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_) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index e890bee787a1af..280c42b5098d80 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -41,6 +41,9 @@ const { RegExpPrototypeTest, SafeMap, SafeSet, + StringPrototypeEndsWith, + StringPrototypeIndexOf, + StringPrototypeLastIndexOf, StringPrototypeMatch, StringPrototypeSlice, StringPrototypeStartsWith, @@ -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'); @@ -276,20 +280,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; }