GeezerVison is showing github.com — util: add `getCwdSafe` internal util fn · nodejs/node@a67fa2a
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/resolve.js +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -38,6 +38,7 @@ const experimentalNetworkImports = | 38 | 38 | getOptionValue('--experimental-network-imports'); | 39 | 39 | const inputTypeFlag = getOptionValue('--input-type'); | 40 | 40 | const { URL, pathToFileURL, fileURLToPath, isURL, toPathIfFileURL } = require('internal/url'); | | 41 | +const { getCWDURL } = require('internal/util'); | 41 | 42 | const { canParse: URLCanParse } = internalBinding('url'); | 42 | 43 | const { | 43 | 44 | ERR_INPUT_TYPE_NOT_ALLOWED, | @@ -1175,7 +1176,7 @@ function defaultResolve(specifier, context = {}) { | 1175 | 1176 |
| 1176 | 1177 | const isMain = parentURL === undefined; | 1177 | 1178 | if (isMain) { | 1178 | | - parentURL = pathToFileURL(`${process.cwd()}/`).href; | | 1179 | + parentURL = getCWDURL().href; | 1179 | 1180 |
| 1180 | 1181 | // This is the initial entry point to the program, and --input-type has | 1181 | 1182 | // been passed as an option; but --input-type can only be used with | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/modules/esm/utils.js +2-11Lines changed: 2 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,7 +16,7 @@ const { | 16 | 16 | loadPreloadModules, | 17 | 17 | initializeFrozenIntrinsics, | 18 | 18 | } = require('internal/process/pre_execution'); | 19 | | -const { pathToFileURL } = require('internal/url'); | | 19 | +const { getCWDURL } = require('internal/util'); | 20 | 20 | const { | 21 | 21 | setImportModuleDynamicallyCallback, | 22 | 22 | setInitializeImportMetaObjectCallback, | @@ -146,15 +146,6 @@ function isLoaderWorker() { | 146 | 146 | async function initializeHooks() { | 147 | 147 | const customLoaderURLs = getOptionValue('--experimental-loader'); | 148 | 148 |
| 149 | | - let cwd; | 150 | | - try { | 151 | | - // `process.cwd()` can fail if the parent directory is deleted while the process runs. | 152 | | - cwd = process.cwd() + '/'; | 153 | | - } catch { | 154 | | - cwd = '/'; | 155 | | - } | 156 | | - | 157 | | - | 158 | 149 | const { Hooks } = require('internal/modules/esm/hooks'); | 159 | 150 | const esmLoader = require('internal/process/esm_loader').esmLoader; | 160 | 151 |
| @@ -171,7 +162,7 @@ async function initializeHooks() { | 171 | 162 | loadPreloadModules(); | 172 | 163 | initializeFrozenIntrinsics(); | 173 | 164 |
| 174 | | - const parentURL = pathToFileURL(cwd).href; | | 165 | + const parentURL = getCWDURL().href; | 175 | 166 | for (let i = 0; i < customLoaderURLs.length; i++) { | 176 | 167 | await hooks.register( | 177 | 168 | customLoaderURLs[i], | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/process/esm_loader.js +2-10Lines changed: 2 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -9,8 +9,7 @@ const { getOptionValue } = require('internal/options'); | 9 | 9 | const { | 10 | 10 | hasUncaughtExceptionCaptureCallback, | 11 | 11 | } = require('internal/process/execution'); | 12 | | -const { pathToFileURL } = require('internal/url'); | 13 | | -const { kEmptyObject } = require('internal/util'); | | 12 | +const { kEmptyObject, getCWDURL } = require('internal/util'); | 14 | 13 |
| 15 | 14 | let esmLoader; | 16 | 15 |
| @@ -23,14 +22,7 @@ module.exports = { | 23 | 22 | try { | 24 | 23 | const userImports = getOptionValue('--import'); | 25 | 24 | if (userImports.length > 0) { | 26 | | - let cwd; | 27 | | - try { | 28 | | - // `process.cwd()` can fail if the parent directory is deleted while the process runs. | 29 | | - cwd = process.cwd() + '/'; | 30 | | - } catch { | 31 | | - cwd = '/'; | 32 | | - } | 33 | | - const parentURL = pathToFileURL(cwd).href; | | 25 | + const parentURL = getCWDURL().href; | 34 | 26 | await SafePromiseAllReturnVoid(userImports, (specifier) => esmLoader.import( | 35 | 27 | specifier, | 36 | 28 | parentURL, | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/util.js +31Lines changed: 31 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -357,6 +357,36 @@ function getConstructorOf(obj) { | 357 | 357 | return null; | 358 | 358 | } | 359 | 359 |
| | 360 | +let cachedURL; | | 361 | +let cachedCWD; | | 362 | + | | 363 | +/** | | 364 | + * Get the current working directory while accounting for the possibility that it has been deleted. | | 365 | + * `process.cwd()` can fail if the parent directory is deleted while the process runs. | | 366 | + * @returns {URL} The current working directory or the volume root if it cannot be determined. | | 367 | + */ | | 368 | +function getCWDURL() { | | 369 | + const { sep } = require('path'); | | 370 | + const { pathToFileURL } = require('internal/url'); | | 371 | + | | 372 | + let cwd; | | 373 | + | | 374 | + try { | | 375 | + // The implementation of `process.cwd()` already uses proper cache when it can. | | 376 | + // It's a relatively cheap call performance-wise for the most common use case. | | 377 | + cwd = process.cwd(); | | 378 | + } catch { | | 379 | + cachedURL ??= pathToFileURL(sep); | | 380 | + } | | 381 | + | | 382 | + if (cwd != null && cwd !== cachedCWD) { | | 383 | + cachedURL = pathToFileURL(cwd + sep); | | 384 | + cachedCWD = cwd; | | 385 | + } | | 386 | + | | 387 | + return cachedURL; | | 388 | +} | | 389 | + | 360 | 390 | function getSystemErrorName(err) { | 361 | 391 | const entry = uvErrmapGet(err); | 362 | 392 | return entry ? entry[0] : `Unknown system error ${err}`; | @@ -784,6 +814,7 @@ module.exports = { | 784 | 814 | filterDuplicateStrings, | 785 | 815 | filterOwnProperties, | 786 | 816 | getConstructorOf, | | 817 | + getCWDURL, | 787 | 818 | getInternalGlobal, | 788 | 819 | getSystemErrorMap, | 789 | 820 | getSystemErrorName, | |
|