File tree Expand file tree Collapse file tree 3 files changed +23
-19
lines changed Expand file tree Collapse file tree 3 files changed +23
-19
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,7 @@ const {
22
22
initializeFrozenIntrinsics,
23
23
} = require ( 'internal/process/pre_execution' ) ;
24
24
const { pathToFileURL } = require ( 'internal/url' ) ;
25
+ const { getCwdSafe } = require ( 'internal/util' ) ;
25
26
const {
26
27
setImportModuleDynamicallyCallback,
27
28
setInitializeImportMetaObjectCallback,
@@ -195,15 +196,6 @@ function isLoaderWorker() {
195
196
async function initializeHooks ( ) {
196
197
const customLoaderURLs = getOptionValue ( '--experimental-loader' ) ;
197
198
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
-
207
199
const { Hooks } = require ( 'internal/modules/esm/hooks' ) ;
208
200
const esmLoader = require ( 'internal/process/esm_loader' ) . esmLoader ;
209
201
@@ -220,7 +212,7 @@ async function initializeHooks() {
220
212
loadPreloadModules ( ) ;
221
213
initializeFrozenIntrinsics ( ) ;
222
214
223
- const parentURL = pathToFileURL ( cwd ) . href ;
215
+ const parentURL = pathToFileURL ( getCwdSafe ( ) ) . href ;
224
216
for ( let i = 0 ; i < customLoaderURLs . length ; i ++ ) {
225
217
await hooks . register (
226
218
customLoaderURLs [ i ] ,
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ const {
10
10
hasUncaughtExceptionCaptureCallback,
11
11
} = require ( 'internal/process/execution' ) ;
12
12
const { pathToFileURL } = require ( 'internal/url' ) ;
13
- const { kEmptyObject } = require ( 'internal/util' ) ;
13
+ const { kEmptyObject, getCwdSafe } = require ( 'internal/util' ) ;
14
14
15
15
let esmLoader ;
16
16
@@ -23,14 +23,7 @@ module.exports = {
23
23
try {
24
24
const userImports = getOptionValue ( '--import' ) ;
25
25
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 ;
34
27
await SafePromiseAllReturnVoid ( userImports , ( specifier ) => esmLoader . import (
35
28
specifier ,
36
29
parentURL ,
Original file line number Diff line number Diff line change @@ -358,6 +358,24 @@ function getConstructorOf(obj) {
358
358
return null ;
359
359
}
360
360
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
+
361
379
function getSystemErrorName ( err ) {
362
380
const entry = uvErrmapGet ( err ) ;
363
381
return entry ? entry [ 0 ] : `Unknown system error ${ err } ` ;
@@ -853,6 +871,7 @@ module.exports = {
853
871
filterDuplicateStrings,
854
872
filterOwnProperties,
855
873
getConstructorOf,
874
+ getCwdSafe,
856
875
getInternalGlobal,
857
876
getSystemErrorMap,
858
877
getSystemErrorName,
You can’t perform that action at this time.
0 commit comments