Skip to content

Commit 269f624

Browse files
committed
util: move from getCwdSafe to getCWDURL
Implement a function that can handle a second cachedCWD when Node.js process doesn't owns it own state.
1 parent ad89e01 commit 269f624

5 files changed

Lines changed: 23 additions & 18 deletions

File tree

lib/internal/modules/cjs/loader.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ const {
8383
emitExperimentalWarning,
8484
kEmptyObject,
8585
setOwnProperty,
86-
getCwdSafe,
8786
getLazy,
8887
} = require('internal/util');
8988
const { internalCompileFunction } = require('internal/vm');
@@ -1153,7 +1152,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
11531152
}
11541153

11551154
if (request[0] === '#' && (parent?.filename || parent?.id === '<repl>')) {
1156-
const parentPath = parent?.filename ?? getCwdSafe();
1155+
const parentPath = parent?.filename ?? process.cwd() + path.sep;
11571156
const pkg = readPackageScope(parentPath) || { __proto__: null };
11581157
if (pkg.data?.imports != null) {
11591158
try {

lib/internal/modules/esm/resolve.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const experimentalNetworkImports =
3737
getOptionValue('--experimental-network-imports');
3838
const typeFlag = getOptionValue('--input-type');
3939
const { URL, pathToFileURL, fileURLToPath, isURL } = require('internal/url');
40-
const { getCwdSafe } = require('internal/util');
40+
const { getCWDURL } = require('internal/util');
4141
const { canParse: URLCanParse } = internalBinding('url');
4242
const { legacyMainResolve: FSLegacyMainResolve } = internalBinding('fs');
4343
const {
@@ -1096,7 +1096,7 @@ function defaultResolve(specifier, context = {}) {
10961096

10971097
const isMain = parentURL === undefined;
10981098
if (isMain) {
1099-
parentURL = pathToFileURL(getCwdSafe()).href;
1099+
parentURL = getCWDURL().href;
11001100

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

lib/internal/modules/esm/utils.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ const {
2121
loadPreloadModules,
2222
initializeFrozenIntrinsics,
2323
} = require('internal/process/pre_execution');
24-
const { pathToFileURL } = require('internal/url');
25-
const { getCwdSafe } = require('internal/util');
24+
const { getCWDURL } = require('internal/util');
2625
const {
2726
setImportModuleDynamicallyCallback,
2827
setInitializeImportMetaObjectCallback,
@@ -212,7 +211,7 @@ async function initializeHooks() {
212211
loadPreloadModules();
213212
initializeFrozenIntrinsics();
214213

215-
const parentURL = pathToFileURL(getCwdSafe()).href;
214+
const parentURL = getCWDURL().href;
216215
for (let i = 0; i < customLoaderURLs.length; i++) {
217216
await hooks.register(
218217
customLoaderURLs[i],

lib/internal/process/esm_loader.js

Lines changed: 2 additions & 3 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, getCwdSafe } = require('internal/util');
12+
const { kEmptyObject, getCWDURL } = require('internal/util');
1413

1514
let esmLoader;
1615

@@ -23,7 +22,7 @@ module.exports = {
2322
try {
2423
const userImports = getOptionValue('--import');
2524
if (userImports.length > 0) {
26-
const parentURL = pathToFileURL(getCwdSafe()).href;
25+
const parentURL = getCWDURL().href;
2726
await SafePromiseAllReturnVoid(userImports, (specifier) => esmLoader.import(
2827
specifier,
2928
parentURL,

lib/internal/util.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -358,22 +358,30 @@ function getConstructorOf(obj) {
358358
return null;
359359
}
360360

361+
let cachedURL;
362+
let cachedCWD;
363+
361364
/**
362365
* Get the current working directory while accounting for the possibility that it has been deleted.
363366
* `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.
367+
* @returns {URL} The current working directory or the volume root if it cannot be determined.
365368
*/
366-
function getCwdSafe() {
369+
function getCWDURL() {
367370
const { sep } = require('path');
368-
let cwd = '';
371+
const { pathToFileURL } = require('url');
372+
373+
let cwd;
369374

370375
try {
371-
cwd = process.cwd();
372-
} catch {
373-
/**/
376+
cwd = process.cwd() + sep;
377+
} catch {}
378+
379+
if (cwd != null && cwd !== cachedCWD) {
380+
cachedURL = pathToFileURL(cwd);
381+
cachedCWD = cwd;
374382
}
375383

376-
return cwd + sep;
384+
return cachedURL;
377385
}
378386

379387
function getSystemErrorName(err) {
@@ -871,7 +879,7 @@ module.exports = {
871879
filterDuplicateStrings,
872880
filterOwnProperties,
873881
getConstructorOf,
874-
getCwdSafe,
882+
getCWDURL,
875883
getInternalGlobal,
876884
getSystemErrorMap,
877885
getSystemErrorName,

0 commit comments

Comments
 (0)