Skip to content

Commit 17aa7a7

Browse files
committed
util: add getCwdSafe internal util fn
This function was first implemented in nodejs#46826, but at some point of the PR implementation this fn was no longer related to the PR. Refs: nodejs#46826 (comment)
1 parent b64f620 commit 17aa7a7

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed

lib/internal/modules/esm/utils.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const {
2222
initializeFrozenIntrinsics,
2323
} = require('internal/process/pre_execution');
2424
const { pathToFileURL } = require('internal/url');
25+
const { getCwdSafe } = require('internal/util');
2526
const {
2627
setImportModuleDynamicallyCallback,
2728
setInitializeImportMetaObjectCallback,
@@ -195,15 +196,6 @@ function isLoaderWorker() {
195196
async function initializeHooks() {
196197
const customLoaderURLs = getOptionValue('--experimental-loader');
197198

198-
let cwd;
199-
try {
200-
// `process.cwd()` can fail if the parent directory is deleted while the process runs.
201-
cwd = process.cwd() + '/';
202-
} catch {
203-
cwd = '/';
204-
}
205-
206-
207199
const { Hooks } = require('internal/modules/esm/hooks');
208200
const esmLoader = require('internal/process/esm_loader').esmLoader;
209201

@@ -220,7 +212,7 @@ async function initializeHooks() {
220212
loadPreloadModules();
221213
initializeFrozenIntrinsics();
222214

223-
const parentURL = pathToFileURL(cwd).href;
215+
const parentURL = pathToFileURL(getCwdSafe()).href;
224216
for (let i = 0; i < customLoaderURLs.length; i++) {
225217
await hooks.register(
226218
customLoaderURLs[i],

lib/internal/process/esm_loader.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const {
1010
hasUncaughtExceptionCaptureCallback,
1111
} = require('internal/process/execution');
1212
const { pathToFileURL } = require('internal/url');
13-
const { kEmptyObject } = require('internal/util');
13+
const { kEmptyObject, getCwdSafe } = require('internal/util');
1414

1515
let esmLoader;
1616

@@ -23,14 +23,7 @@ module.exports = {
2323
try {
2424
const userImports = getOptionValue('--import');
2525
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;
26+
const parentURL = pathToFileURL(getCwdSafe()).href;
3427
await SafePromiseAllReturnVoid(userImports, (specifier) => esmLoader.import(
3528
specifier,
3629
parentURL,

lib/internal/util.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,24 @@ function getConstructorOf(obj) {
358358
return null;
359359
}
360360

361+
/**
362+
* Get the current working directory while accounting for the possibility that it has been deleted.
363+
* `process.cwd()` can fail if the parent directory is deleted while the process runs.
364+
* @returns {string} The current working directory or the volume root if it cannot be determined.
365+
*/
366+
function getCwdSafe() {
367+
const { sep } = require('path');
368+
let cwd = '';
369+
370+
try {
371+
cwd = process.cwd();
372+
} catch {
373+
/**/
374+
}
375+
376+
return cwd + sep;
377+
}
378+
361379
function getSystemErrorName(err) {
362380
const entry = uvErrmapGet(err);
363381
return entry ? entry[0] : `Unknown system error ${err}`;
@@ -853,6 +871,7 @@ module.exports = {
853871
filterDuplicateStrings,
854872
filterOwnProperties,
855873
getConstructorOf,
874+
getCwdSafe,
856875
getInternalGlobal,
857876
getSystemErrorMap,
858877
getSystemErrorName,

0 commit comments

Comments
 (0)