diff --git a/lib/internal/bootstrap/switches/does_own_process_state.js b/lib/internal/bootstrap/switches/does_own_process_state.js index 370da66a825f65..0c1dd6eaa7bf4f 100644 --- a/lib/internal/bootstrap/switches/does_own_process_state.js +++ b/lib/internal/bootstrap/switches/does_own_process_state.js @@ -35,15 +35,15 @@ const { validateString, validateUint32, } = require('internal/validators'); +const { + codes: { + ERR_INVALID_ARG_TYPE, + ERR_SYSTEM_ERROR, + ERR_UNKNOWN_CREDENTIAL, + }, +} = require('internal/errors'); function wrapPosixCredentialSetters(credentials) { - const { - codes: { - ERR_INVALID_ARG_TYPE, - ERR_UNKNOWN_CREDENTIAL, - }, - } = require('internal/errors'); - const { initgroups: _initgroups, setgroups: _setgroups, @@ -138,7 +138,24 @@ function wrappedUmask(mask) { } function wrappedCwd() { - if (cachedCwd === '') - cachedCwd = rawMethods.cwd(); + if (cachedCwd === '') { + try { + cachedCwd = rawMethods.cwd(); + } catch (err) { + if (err.code === 'ENOENT') { + const context = { + code: 'ENOENT', + syscall: 'process.cwd', + message: + 'The current working directory does not exist. It may have been deleted or unmounted.', + }; + const wrapped = new ERR_SYSTEM_ERROR(context); + // Preserve the original error for debugging. + wrapped.cause = err; + throw wrapped; + } + throw err; + } + } return cachedCwd; }