Skip to content

Commit

Permalink
util: add getCwdSafe internal util fn
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
jlenon7 committed Sep 7, 2023
1 parent 0bce573 commit dfc01ee
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
12 changes: 2 additions & 10 deletions lib/internal/modules/esm/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {
initializeFrozenIntrinsics,
} = require('internal/process/pre_execution');
const { pathToFileURL } = require('internal/url');
const { getCwdSafe } = require('internal/util');
const {
setImportModuleDynamicallyCallback,
setInitializeImportMetaObjectCallback,
Expand Down Expand Up @@ -112,15 +113,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;

Expand All @@ -137,7 +129,7 @@ async function initializeHooks() {
loadPreloadModules();
initializeFrozenIntrinsics();

const parentURL = pathToFileURL(cwd).href;
const parentURL = pathToFileURL(getCwdSafe()).href;
for (let i = 0; i < customLoaderURLs.length; i++) {
await hooks.register(
customLoaderURLs[i],
Expand Down
11 changes: 2 additions & 9 deletions lib/internal/process/esm_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const {
hasUncaughtExceptionCaptureCallback,
} = require('internal/process/execution');
const { pathToFileURL } = require('internal/url');
const { kEmptyObject } = require('internal/util');
const { kEmptyObject, getCwdSafe } = require('internal/util');

let esmLoader;

Expand All @@ -23,14 +23,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 = pathToFileURL(getCwdSafe()).href;
await SafePromiseAllReturnVoid(userImports, (specifier) => esmLoader.import(
specifier,
parentURL,
Expand Down
19 changes: 19 additions & 0 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,24 @@ function getConstructorOf(obj) {
return null;
}

/**
* 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.
*/
function getCwdSafe() {
const { sep } = require('path');
let cwd = '';

try {
cwd = process.cwd();
} catch {
/**/
}

return cwd + sep;
}

function getSystemErrorName(err) {
const entry = uvErrmapGet(err);
return entry ? entry[0] : `Unknown system error ${err}`;
Expand Down Expand Up @@ -850,6 +868,7 @@ module.exports = {
filterDuplicateStrings,
filterOwnProperties,
getConstructorOf,
getCwdSafe,
getInternalGlobal,
getSystemErrorMap,
getSystemErrorName,
Expand Down

0 comments on commit dfc01ee

Please sign in to comment.