diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 5271c286a5c79f..19a7d7e671f5ab 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'); @@ -1004,7 +1003,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 7aec2c2a54dff1..5f352a14e90c1f 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 { @@ -1018,7 +1018,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 f13f0f488912ee..64bc21a47c7845 100644 --- a/lib/internal/modules/esm/utils.js +++ b/lib/internal/modules/esm/utils.js @@ -16,8 +16,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, @@ -129,7 +128,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 12627e7885517a..207f6beb150ba0 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) { @@ -868,7 +876,7 @@ module.exports = { filterDuplicateStrings, filterOwnProperties, getConstructorOf, - getCwdSafe, + getCWDURL, getInternalGlobal, getSystemErrorMap, getSystemErrorName, diff --git a/test/fixtures/errors/force_colors.snapshot b/test/fixtures/errors/force_colors.snapshot index 2af6dc7fd7a478..be1d45d0d8e8ba 100644 --- a/test/fixtures/errors/force_colors.snapshot +++ b/test/fixtures/errors/force_colors.snapshot @@ -4,10 +4,10 @@ throw new Error('Should include grayed stack trace') Error: Should include grayed stack trace at Object. (/test*force_colors.js:1:7) - at Module._compile (node:internal*modules*cjs*loader:1242:14) - at Module._extensions..js (node:internal*modules*cjs*loader:1296:10) - at Module.load (node:internal*modules*cjs*loader:1092:32) - at Module._load (node:internal*modules*cjs*loader:939:12) + at Module._compile (node:internal*modules*cjs*loader:1241:14) + at Module._extensions..js (node:internal*modules*cjs*loader:1295:10) + at Module.load (node:internal*modules*cjs*loader:1091:32) + at Module._load (node:internal*modules*cjs*loader:938:12)  at Function.executeUserEntryPoint [as runMain] (node:internal*modules*run_main:83:12)  at node:internal*main*run_main_module:23:47 diff --git a/test/fixtures/source-map/output/source_map_sourcemapping_url_string.snapshot b/test/fixtures/source-map/output/source_map_sourcemapping_url_string.snapshot index 3d4a248fb72eda..2c1e11eeb9eab1 100644 --- a/test/fixtures/source-map/output/source_map_sourcemapping_url_string.snapshot +++ b/test/fixtures/source-map/output/source_map_sourcemapping_url_string.snapshot @@ -1,3 +1,3 @@ Error: an exception. at Object. (*typescript-sourcemapping_url_string.ts:3:7) - at Module._compile (node:internal*modules*cjs*loader:1242:14) + at Module._compile (node:internal*modules*cjs*loader:1241:14)