Skip to content

Commit b151075

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 39d8514 commit b151075

File tree

7 files changed

+28
-23
lines changed

7 files changed

+28
-23
lines changed

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');
@@ -1004,7 +1003,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
10041003
}
10051004

10061005
if (request[0] === '#' && (parent?.filename || parent?.id === '<repl>')) {
1007-
const parentPath = parent?.filename ?? getCwdSafe();
1006+
const parentPath = parent?.filename ?? process.cwd() + path.sep;
10081007
const pkg = readPackageScope(parentPath) || { __proto__: null };
10091008
if (pkg.data?.imports != null) {
10101009
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 {
@@ -1018,7 +1018,7 @@ function defaultResolve(specifier, context = {}) {
10181018

10191019
const isMain = parentURL === undefined;
10201020
if (isMain) {
1021-
parentURL = pathToFileURL(getCwdSafe()).href;
1021+
parentURL = getCWDURL().href;
10221022

10231023
// This is the initial entry point to the program, and --input-type has
10241024
// 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
@@ -16,8 +16,7 @@ const {
1616
loadPreloadModules,
1717
initializeFrozenIntrinsics,
1818
} = require('internal/process/pre_execution');
19-
const { pathToFileURL } = require('internal/url');
20-
const { getCwdSafe } = require('internal/util');
19+
const { getCWDURL } = require('internal/util');
2120
const {
2221
setImportModuleDynamicallyCallback,
2322
setInitializeImportMetaObjectCallback,
@@ -129,7 +128,7 @@ async function initializeHooks() {
129128
loadPreloadModules();
130129
initializeFrozenIntrinsics();
131130

132-
const parentURL = pathToFileURL(getCwdSafe()).href;
131+
const parentURL = getCWDURL().href;
133132
for (let i = 0; i < customLoaderURLs.length; i++) {
134133
await hooks.register(
135134
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) {
@@ -868,7 +876,7 @@ module.exports = {
868876
filterDuplicateStrings,
869877
filterOwnProperties,
870878
getConstructorOf,
871-
getCwdSafe,
879+
getCWDURL,
872880
getInternalGlobal,
873881
getSystemErrorMap,
874882
getSystemErrorName,

test/fixtures/errors/force_colors.snapshot

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ throw new Error('Should include grayed stack trace')
44

55
Error: Should include grayed stack trace
66
at Object.<anonymous> (/test*force_colors.js:1:7)
7-
 at Module._compile (node:internal*modules*cjs*loader:1242:14)
8-
 at Module._extensions..js (node:internal*modules*cjs*loader:1296:10)
9-
 at Module.load (node:internal*modules*cjs*loader:1092:32)
10-
 at Module._load (node:internal*modules*cjs*loader:939:12)
7+
 at Module._compile (node:internal*modules*cjs*loader:1241:14)
8+
 at Module._extensions..js (node:internal*modules*cjs*loader:1295:10)
9+
 at Module.load (node:internal*modules*cjs*loader:1091:32)
10+
 at Module._load (node:internal*modules*cjs*loader:938:12)
1111
 at Function.executeUserEntryPoint [as runMain] (node:internal*modules*run_main:83:12)
1212
 at node:internal*main*run_main_module:23:47
1313

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Error: an exception.
22
at Object.<anonymous> (*typescript-sourcemapping_url_string.ts:3:7)
3-
at Module._compile (node:internal*modules*cjs*loader:1242:14)
3+
at Module._compile (node:internal*modules*cjs*loader:1241:14)

0 commit comments

Comments
 (0)