diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 953dd8d755f980..db31feb93ee724 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -83,7 +83,6 @@ const { emitExperimentalWarning, kEmptyObject, setOwnProperty, - getCwdSafe, getLazy, } = require('internal/util'); const { internalCompileFunction } = require('internal/vm'); @@ -1153,7 +1152,7 @@ Module._resolveFilename = function(request, parent, isMain, options) { } if (request[0] === '#' && (parent?.filename || parent?.id === '')) { - const parentPath = parent?.filename ?? getCwdSafe(); + const parentPath = parent?.filename ?? process.cwd() + path.sep; const pkg = readPackageScope(parentPath) || { __proto__: null }; if (pkg.data?.imports != null) { try { diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index 2cd5e0b9d5037b..03725bd0b5dfbe 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -37,7 +37,7 @@ const experimentalNetworkImports = getOptionValue('--experimental-network-imports'); const typeFlag = getOptionValue('--input-type'); const { URL, pathToFileURL, fileURLToPath, isURL } = require('internal/url'); -const { getCwdSafe } = require('internal/util'); +const { getCWDURL } = require('internal/util'); const { canParse: URLCanParse } = internalBinding('url'); const { legacyMainResolve: FSLegacyMainResolve } = internalBinding('fs'); const { @@ -1096,7 +1096,7 @@ function defaultResolve(specifier, context = {}) { const isMain = parentURL === undefined; if (isMain) { - parentURL = pathToFileURL(getCwdSafe()).href; + parentURL = getCWDURL().href; // This is the initial entry point to the program, and --input-type has // been passed as an option; but --input-type can only be used with diff --git a/lib/internal/modules/esm/utils.js b/lib/internal/modules/esm/utils.js index 100daa2a09d754..af4e975566c83a 100644 --- a/lib/internal/modules/esm/utils.js +++ b/lib/internal/modules/esm/utils.js @@ -21,8 +21,7 @@ const { loadPreloadModules, initializeFrozenIntrinsics, } = require('internal/process/pre_execution'); -const { pathToFileURL } = require('internal/url'); -const { getCwdSafe } = require('internal/util'); +const { getCWDURL } = require('internal/util'); const { setImportModuleDynamicallyCallback, setInitializeImportMetaObjectCallback, @@ -212,7 +211,7 @@ async function initializeHooks() { loadPreloadModules(); initializeFrozenIntrinsics(); - const parentURL = pathToFileURL(getCwdSafe()).href; + const parentURL = getCWDURL().href; for (let i = 0; i < customLoaderURLs.length; i++) { await hooks.register( customLoaderURLs[i], diff --git a/lib/internal/process/esm_loader.js b/lib/internal/process/esm_loader.js index 9f80991610cd08..a3451ddab307f2 100644 --- a/lib/internal/process/esm_loader.js +++ b/lib/internal/process/esm_loader.js @@ -9,8 +9,7 @@ const { getOptionValue } = require('internal/options'); const { hasUncaughtExceptionCaptureCallback, } = require('internal/process/execution'); -const { pathToFileURL } = require('internal/url'); -const { kEmptyObject, getCwdSafe } = require('internal/util'); +const { kEmptyObject, getCWDURL } = require('internal/util'); let esmLoader; @@ -23,7 +22,7 @@ module.exports = { try { const userImports = getOptionValue('--import'); if (userImports.length > 0) { - const parentURL = pathToFileURL(getCwdSafe()).href; + const parentURL = getCWDURL().href; await SafePromiseAllReturnVoid(userImports, (specifier) => esmLoader.import( specifier, parentURL, diff --git a/lib/internal/util.js b/lib/internal/util.js index 75083d259a7e57..e6d925f909388a 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -358,22 +358,30 @@ function getConstructorOf(obj) { return null; } +let cachedURL; +let cachedCWD; + /** * Get the current working directory while accounting for the possibility that it has been deleted. * `process.cwd()` can fail if the parent directory is deleted while the process runs. - * @returns {string} The current working directory or the volume root if it cannot be determined. + * @returns {URL} The current working directory or the volume root if it cannot be determined. */ -function getCwdSafe() { +function getCWDURL() { const { sep } = require('path'); - let cwd = ''; + const { pathToFileURL } = require('url'); + + let cwd; try { - cwd = process.cwd(); - } catch { - /**/ + cwd = process.cwd() + sep; + } catch {} + + if (cwd != null && cwd !== cachedCWD) { + cachedURL = pathToFileURL(cwd); + cachedCWD = cwd; } - return cwd + sep; + return cachedURL; } function getSystemErrorName(err) { @@ -871,7 +879,7 @@ module.exports = { filterDuplicateStrings, filterOwnProperties, getConstructorOf, - getCwdSafe, + getCWDURL, getInternalGlobal, getSystemErrorMap, getSystemErrorName,