Skip to content

Commit 7c2060c

Browse files
jlenon7ruyadorno
authored andcommitted
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: #46826 (comment) PR-URL: #48434 Reviewed-By: Jacob Smith <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent 1287d5b commit 7c2060c

File tree

4 files changed

+37
-22
lines changed

4 files changed

+37
-22
lines changed

‎lib/internal/modules/esm/resolve.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const experimentalNetworkImports =
3737
getOptionValue('--experimental-network-imports');
3838
consttypeFlag=getOptionValue('--input-type');
3939
const{URL, pathToFileURL, fileURLToPath, isURL }=require('internal/url');
40+
const{ getCWDURL }=require('internal/util');
4041
const{canParse: URLCanParse}=internalBinding('url');
4142
const{legacyMainResolve: FSLegacyMainResolve}=internalBinding('fs');
4243
const{
@@ -1017,7 +1018,7 @@ function defaultResolve(specifier, context ={}){
10171018

10181019
constisMain=parentURL===undefined;
10191020
if(isMain){
1020-
parentURL=pathToFileURL(`${process.cwd()}/`).href;
1021+
parentURL=getCWDURL().href;
10211022

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

‎lib/internal/modules/esm/utils.js‎

Lines changed: 2 additions & 11 deletions
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,
@@ -112,15 +112,6 @@ function isLoaderWorker(){
112112
asyncfunctioninitializeHooks(){
113113
constcustomLoaderURLs=getOptionValue('--experimental-loader');
114114

115-
letcwd;
116-
try{
117-
// `process.cwd()` can fail if the parent directory is deleted while the process runs.
118-
cwd=process.cwd()+'/';
119-
}catch{
120-
cwd='/';
121-
}
122-
123-
124115
const{ Hooks }=require('internal/modules/esm/hooks');
125116
constesmLoader=require('internal/process/esm_loader').esmLoader;
126117

@@ -137,7 +128,7 @@ async function initializeHooks(){
137128
loadPreloadModules();
138129
initializeFrozenIntrinsics();
139130

140-
constparentURL=pathToFileURL(cwd).href;
131+
constparentURL=getCWDURL().href;
141132
for(leti=0;i<customLoaderURLs.length;i++){
142133
awaithooks.register(
143134
customLoaderURLs[i],

‎lib/internal/process/esm_loader.js‎

Lines changed: 2 additions & 10 deletions
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
letesmLoader;
1615

@@ -23,14 +22,7 @@ module.exports ={
2322
try{
2423
constuserImports=getOptionValue('--import');
2524
if(userImports.length>0){
26-
letcwd;
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-
constparentURL=pathToFileURL(cwd).href;
25+
constparentURL=getCWDURL().href;
3426
awaitSafePromiseAllReturnVoid(userImports,(specifier)=>esmLoader.import(
3527
specifier,
3628
parentURL,

‎lib/internal/util.js‎

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

361+
letcachedURL;
362+
letcachedCWD;
363+
364+
/**
365+
* Get the current working directory while accounting for the possibility that it has been deleted.
366+
* `process.cwd()` can fail if the parent directory is deleted while the process runs.
367+
* @returns{URL} The current working directory or the volume root if it cannot be determined.
368+
*/
369+
functiongetCWDURL(){
370+
const{ sep }=require('path');
371+
const{ pathToFileURL }=require('internal/url');
372+
373+
letcwd;
374+
375+
try{
376+
// The implementation of `process.cwd()` already uses proper cache when it can.
377+
// It's a relatively cheap call performance-wise for the most common use case.
378+
cwd=process.cwd();
379+
}catch{
380+
cachedURL??=pathToFileURL(sep);
381+
}
382+
383+
if(cwd!=null&&cwd!==cachedCWD){
384+
cachedURL=pathToFileURL(cwd+sep);
385+
cachedCWD=cwd;
386+
}
387+
388+
returncachedURL;
389+
}
390+
361391
functiongetSystemErrorName(err){
362392
constentry=uvErrmapGet(err);
363393
returnentry ? entry[0] : `Unknown system error ${err}`;
@@ -850,6 +880,7 @@ module.exports ={
850880
filterDuplicateStrings,
851881
filterOwnProperties,
852882
getConstructorOf,
883+
getCWDURL,
853884
getInternalGlobal,
854885
getSystemErrorMap,
855886
getSystemErrorName,

0 commit comments

Comments
(0)