Skip to content

Commit

Permalink
util: move from getCwdSafe to getCWDURL
Browse files Browse the repository at this point in the history
Implement a function that can handle a second
cachedCWD when Node.js process doesn't owns it
own state.
  • Loading branch information
jlenon7 committed Sep 20, 2023
1 parent ad89e01 commit 269f624
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 18 deletions.
3 changes: 1 addition & 2 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ const {
emitExperimentalWarning,
kEmptyObject,
setOwnProperty,
getCwdSafe,
getLazy,
} = require('internal/util');
const { internalCompileFunction } = require('internal/vm');
Expand Down Expand Up @@ -1153,7 +1152,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
}

if (request[0] === '#' && (parent?.filename || parent?.id === '<repl>')) {
const parentPath = parent?.filename ?? getCwdSafe();
const parentPath = parent?.filename ?? process.cwd() + path.sep;
const pkg = readPackageScope(parentPath) || { __proto__: null };
if (pkg.data?.imports != null) {
try {
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const experimentalNetworkImports =
getOptionValue('--experimental-network-imports');
const typeFlag = getOptionValue('--input-type');
const { URL, pathToFileURL, fileURLToPath, isURL } = require('internal/url');
const { getCwdSafe } = require('internal/util');
const { getCWDURL } = require('internal/util');
const { canParse: URLCanParse } = internalBinding('url');
const { legacyMainResolve: FSLegacyMainResolve } = internalBinding('fs');
const {
Expand Down Expand Up @@ -1096,7 +1096,7 @@ function defaultResolve(specifier, context = {}) {

const isMain = parentURL === undefined;
if (isMain) {
parentURL = pathToFileURL(getCwdSafe()).href;
parentURL = getCWDURL().href;

// This is the initial entry point to the program, and --input-type has
// been passed as an option; but --input-type can only be used with
Expand Down
5 changes: 2 additions & 3 deletions lib/internal/modules/esm/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ const {
loadPreloadModules,
initializeFrozenIntrinsics,
} = require('internal/process/pre_execution');
const { pathToFileURL } = require('internal/url');
const { getCwdSafe } = require('internal/util');
const { getCWDURL } = require('internal/util');
const {
setImportModuleDynamicallyCallback,
setInitializeImportMetaObjectCallback,
Expand Down Expand Up @@ -212,7 +211,7 @@ async function initializeHooks() {
loadPreloadModules();
initializeFrozenIntrinsics();

const parentURL = pathToFileURL(getCwdSafe()).href;
const parentURL = getCWDURL().href;
for (let i = 0; i < customLoaderURLs.length; i++) {
await hooks.register(
customLoaderURLs[i],
Expand Down
5 changes: 2 additions & 3 deletions lib/internal/process/esm_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ const { getOptionValue } = require('internal/options');
const {
hasUncaughtExceptionCaptureCallback,
} = require('internal/process/execution');
const { pathToFileURL } = require('internal/url');
const { kEmptyObject, getCwdSafe } = require('internal/util');
const { kEmptyObject, getCWDURL } = require('internal/util');

let esmLoader;

Expand All @@ -23,7 +22,7 @@ module.exports = {
try {
const userImports = getOptionValue('--import');
if (userImports.length > 0) {
const parentURL = pathToFileURL(getCwdSafe()).href;
const parentURL = getCWDURL().href;
await SafePromiseAllReturnVoid(userImports, (specifier) => esmLoader.import(
specifier,
parentURL,
Expand Down
24 changes: 16 additions & 8 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,22 +358,30 @@ function getConstructorOf(obj) {
return null;
}

let cachedURL;
let cachedCWD;

/**
* Get the current working directory while accounting for the possibility that it has been deleted.
* `process.cwd()` can fail if the parent directory is deleted while the process runs.
* @returns {string} The current working directory or the volume root if it cannot be determined.
* @returns {URL} The current working directory or the volume root if it cannot be determined.
*/
function getCwdSafe() {
function getCWDURL() {
const { sep } = require('path');
let cwd = '';
const { pathToFileURL } = require('url');

let cwd;

try {
cwd = process.cwd();
} catch {
/**/
cwd = process.cwd() + sep;
} catch {}

if (cwd != null && cwd !== cachedCWD) {
cachedURL = pathToFileURL(cwd);
cachedCWD = cwd;
}

return cwd + sep;
return cachedURL;
}

function getSystemErrorName(err) {
Expand Down Expand Up @@ -871,7 +879,7 @@ module.exports = {
filterDuplicateStrings,
filterOwnProperties,
getConstructorOf,
getCwdSafe,
getCWDURL,
getInternalGlobal,
getSystemErrorMap,
getSystemErrorName,
Expand Down

0 comments on commit 269f624

Please sign in to comment.