GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — util: add `getCwdSafe` internal util fn · nodejs/node@a67fa2a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal
    • modules/esm
      • resolve.js
      • utils.js
    • process
      • esm_loader.js
    • util.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/resolve.js
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const experimentalNetworkImports =
3838
getOptionValue('--experimental-network-imports');
3939
const inputTypeFlag = getOptionValue('--input-type');
4040
const { URL, pathToFileURL, fileURLToPath, isURL, toPathIfFileURL } = require('internal/url');
41+
const { getCWDURL } = require('internal/util');
4142
const { canParse: URLCanParse } = internalBinding('url');
4243
const {
4344
ERR_INPUT_TYPE_NOT_ALLOWED,
@@ -1175,7 +1176,7 @@ function defaultResolve(specifier, context = {}) {
11751176

11761177
const isMain = parentURL === undefined;
11771178
if (isMain) {
1178-
parentURL = pathToFileURL(`${process.cwd()}/`).href;
1179+
parentURL = getCWDURL().href;
11791180

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

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

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/utils.js
+2-11Lines 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,
@@ -146,15 +146,6 @@ function isLoaderWorker() {
146146
async function initializeHooks() {
147147
const customLoaderURLs = getOptionValue('--experimental-loader');
148148

149-
let cwd;
150-
try {
151-
// `process.cwd()` can fail if the parent directory is deleted while the process runs.
152-
cwd = process.cwd() + '/';
153-
} catch {
154-
cwd = '/';
155-
}
156-
157-
158149
const { Hooks } = require('internal/modules/esm/hooks');
159150
const esmLoader = require('internal/process/esm_loader').esmLoader;
160151

@@ -171,7 +162,7 @@ async function initializeHooks() {
171162
loadPreloadModules();
172163
initializeFrozenIntrinsics();
173164

174-
const parentURL = pathToFileURL(cwd).href;
165+
const parentURL = getCWDURL().href;
175166
for (let i = 0; i < customLoaderURLs.length; i++) {
176167
await hooks.register(
177168
customLoaderURLs[i],
Collapse file

‎lib/internal/process/esm_loader.js‎

Copy file name to clipboardExpand all lines: lib/internal/process/esm_loader.js
+2-10Lines 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
let esmLoader;
1615

@@ -23,14 +22,7 @@ module.exports = {
2322
try {
2423
const userImports = getOptionValue('--import');
2524
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;
25+
const parentURL = getCWDURL().href;
3426
await SafePromiseAllReturnVoid(userImports, (specifier) => esmLoader.import(
3527
specifier,
3628
parentURL,
Collapse file

‎lib/internal/util.js‎

Copy file name to clipboardExpand all lines: lib/internal/util.js
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,36 @@ function getConstructorOf(obj) {
357357
return null;
358358
}
359359

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

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.