From e2b24f84c1c1de4e0884e7cf28a1593899b5f8da Mon Sep 17 00:00:00 2001 From: Sergey Chernyshev Date: Thu, 25 Apr 2024 03:59:34 +0200 Subject: [PATCH] util: add `getCwdSafe` internal util fn This function was first implemented in #46826, but at some point of the PR implementation this fn was no longer related to the PR. Refs: https://github.com/nodejs/node/pull/46826#discussion_r1225431178 PR-URL: https://github.com/nodejs/node/pull/48434 Backport-PR-URL: https://github.com/nodejs/node/pull/50669 Reviewed-By: Jacob Smith Reviewed-By: Yagiz Nizipli Reviewed-By: Antoine du Hamel --- .../lib/internal/modules/esm/resolve.js | 3 +- .../lib/internal/modules/esm/utils.js | 13 ++------ .../lib/internal/process/esm_loader.js | 12 ++----- graal-nodejs/lib/internal/util.js | 31 +++++++++++++++++++ 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/graal-nodejs/lib/internal/modules/esm/resolve.js b/graal-nodejs/lib/internal/modules/esm/resolve.js index 7451bc273d1..529110f92ee 100644 --- a/graal-nodejs/lib/internal/modules/esm/resolve.js +++ b/graal-nodejs/lib/internal/modules/esm/resolve.js @@ -38,6 +38,7 @@ const experimentalNetworkImports = getOptionValue('--experimental-network-imports'); const inputTypeFlag = getOptionValue('--input-type'); const { URL, pathToFileURL, fileURLToPath, isURL, toPathIfFileURL } = require('internal/url'); +const { getCWDURL } = require('internal/util'); const { canParse: URLCanParse } = internalBinding('url'); const { ERR_INPUT_TYPE_NOT_ALLOWED, @@ -1175,7 +1176,7 @@ function defaultResolve(specifier, context = {}) { const isMain = parentURL === undefined; if (isMain) { - parentURL = pathToFileURL(`${process.cwd()}/`).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/graal-nodejs/lib/internal/modules/esm/utils.js b/graal-nodejs/lib/internal/modules/esm/utils.js index a5391f9b9bf..414e593634e 100644 --- a/graal-nodejs/lib/internal/modules/esm/utils.js +++ b/graal-nodejs/lib/internal/modules/esm/utils.js @@ -16,7 +16,7 @@ const { loadPreloadModules, initializeFrozenIntrinsics, } = require('internal/process/pre_execution'); -const { pathToFileURL } = require('internal/url'); +const { getCWDURL } = require('internal/util'); const { setImportModuleDynamicallyCallback, setInitializeImportMetaObjectCallback, @@ -146,15 +146,6 @@ function isLoaderWorker() { async function initializeHooks() { const customLoaderURLs = getOptionValue('--experimental-loader'); - let cwd; - try { - // `process.cwd()` can fail if the parent directory is deleted while the process runs. - cwd = process.cwd() + '/'; - } catch { - cwd = '/'; - } - - const { Hooks } = require('internal/modules/esm/hooks'); const esmLoader = require('internal/process/esm_loader').esmLoader; @@ -171,7 +162,7 @@ async function initializeHooks() { loadPreloadModules(); initializeFrozenIntrinsics(); - const parentURL = pathToFileURL(cwd).href; + const parentURL = getCWDURL().href; for (let i = 0; i < customLoaderURLs.length; i++) { await hooks.register( customLoaderURLs[i], diff --git a/graal-nodejs/lib/internal/process/esm_loader.js b/graal-nodejs/lib/internal/process/esm_loader.js index e735101ab18..a3451ddab30 100644 --- a/graal-nodejs/lib/internal/process/esm_loader.js +++ b/graal-nodejs/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 } = require('internal/util'); +const { kEmptyObject, getCWDURL } = require('internal/util'); let esmLoader; @@ -23,14 +22,7 @@ module.exports = { try { const userImports = getOptionValue('--import'); if (userImports.length > 0) { - let cwd; - try { - // `process.cwd()` can fail if the parent directory is deleted while the process runs. - cwd = process.cwd() + '/'; - } catch { - cwd = '/'; - } - const parentURL = pathToFileURL(cwd).href; + const parentURL = getCWDURL().href; await SafePromiseAllReturnVoid(userImports, (specifier) => esmLoader.import( specifier, parentURL, diff --git a/graal-nodejs/lib/internal/util.js b/graal-nodejs/lib/internal/util.js index d9c292c99a6..aedfa2e9889 100644 --- a/graal-nodejs/lib/internal/util.js +++ b/graal-nodejs/lib/internal/util.js @@ -357,6 +357,36 @@ 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 {URL} The current working directory or the volume root if it cannot be determined. + */ +function getCWDURL() { + const { sep } = require('path'); + const { pathToFileURL } = require('internal/url'); + + let cwd; + + try { + // The implementation of `process.cwd()` already uses proper cache when it can. + // It's a relatively cheap call performance-wise for the most common use case. + cwd = process.cwd(); + } catch { + cachedURL ??= pathToFileURL(sep); + } + + if (cwd != null && cwd !== cachedCWD) { + cachedURL = pathToFileURL(cwd + sep); + cachedCWD = cwd; + } + + return cachedURL; +} + function getSystemErrorName(err) { const entry = uvErrmapGet(err); return entry ? entry[0] : `Unknown system error ${err}`; @@ -784,6 +814,7 @@ module.exports = { filterDuplicateStrings, filterOwnProperties, getConstructorOf, + getCWDURL, getInternalGlobal, getSystemErrorMap, getSystemErrorName,