Skip to content

Commit e2b24f8

Browse files
committed
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: nodejs/node#46826 (comment) PR-URL: nodejs/node#48434 Backport-PR-URL: nodejs/node#50669 Reviewed-By: Jacob Smith <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent 54d64e0 commit e2b24f8

File tree

4 files changed

+37
-22
lines changed

4 files changed

+37
-22
lines changed

graal-nodejs/lib/internal/modules/esm/resolve.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const experimentalNetworkImports =
3838
getOptionValue('--experimental-network-imports');
3939
const inputTypeFlag = getOptionValue('--input-type');
4040
const { URL, pathToFileURL, fileURLToPath, isURL, toPathIfFileURL } = require('internal/url');
41+
const { getCWDURL } = require('internal/util');
4142
const { canParse: URLCanParse } = internalBinding('url');
4243
const {
4344
ERR_INPUT_TYPE_NOT_ALLOWED,
@@ -1175,7 +1176,7 @@ function defaultResolve(specifier, context = {}) {
11751176

11761177
const isMain = parentURL === undefined;
11771178
if (isMain) {
1178-
parentURL = pathToFileURL(`${process.cwd()}/`).href;
1179+
parentURL = getCWDURL().href;
11791180

11801181
// This is the initial entry point to the program, and --input-type has
11811182
// been passed as an option; but --input-type can only be used with

graal-nodejs/lib/internal/modules/esm/utils.js

+2-11
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const {
1616
loadPreloadModules,
1717
initializeFrozenIntrinsics,
1818
} = require('internal/process/pre_execution');
19-
const { pathToFileURL } = require('internal/url');
19+
const { getCWDURL } = require('internal/util');
2020
const {
2121
setImportModuleDynamicallyCallback,
2222
setInitializeImportMetaObjectCallback,
@@ -146,15 +146,6 @@ function isLoaderWorker() {
146146
async function initializeHooks() {
147147
const customLoaderURLs = getOptionValue('--experimental-loader');
148148

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-
158149
const { Hooks } = require('internal/modules/esm/hooks');
159150
const esmLoader = require('internal/process/esm_loader').esmLoader;
160151

@@ -171,7 +162,7 @@ async function initializeHooks() {
171162
loadPreloadModules();
172163
initializeFrozenIntrinsics();
173164

174-
const parentURL = pathToFileURL(cwd).href;
165+
const parentURL = getCWDURL().href;
175166
for (let i = 0; i < customLoaderURLs.length; i++) {
176167
await hooks.register(
177168
customLoaderURLs[i],

graal-nodejs/lib/internal/process/esm_loader.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ const { getOptionValue } = require('internal/options');
99
const {
1010
hasUncaughtExceptionCaptureCallback,
1111
} = require('internal/process/execution');
12-
const { pathToFileURL } = require('internal/url');
13-
const { kEmptyObject } = require('internal/util');
12+
const { kEmptyObject, getCWDURL } = require('internal/util');
1413

1514
let esmLoader;
1615

@@ -23,14 +22,7 @@ module.exports = {
2322
try {
2423
const userImports = getOptionValue('--import');
2524
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;
3426
await SafePromiseAllReturnVoid(userImports, (specifier) => esmLoader.import(
3527
specifier,
3628
parentURL,

graal-nodejs/lib/internal/util.js

+31
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,36 @@ function getConstructorOf(obj) {
357357
return null;
358358
}
359359

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+
360390
function getSystemErrorName(err) {
361391
const entry = uvErrmapGet(err);
362392
return entry ? entry[0] : `Unknown system error ${err}`;
@@ -784,6 +814,7 @@ module.exports = {
784814
filterDuplicateStrings,
785815
filterOwnProperties,
786816
getConstructorOf,
817+
getCWDURL,
787818
getInternalGlobal,
788819
getSystemErrorMap,
789820
getSystemErrorName,

0 commit comments

Comments
 (0)