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 39d8514 commit b151075
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 23 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 @@ -1004,7 +1003,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 @@ -1018,7 +1018,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 @@ -16,8 +16,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 @@ -129,7 +128,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');

Check failure on line 371 in lib/internal/util.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'url' module is restricted from being used. Require `internal/url` instead of `url`

let cwd;

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

Check failure on line 377 in lib/internal/util.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Empty block statement

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

return cwd + sep;
return cachedURL;
}

function getSystemErrorName(err) {
Expand Down Expand Up @@ -868,7 +876,7 @@ module.exports = {
filterDuplicateStrings,
filterOwnProperties,
getConstructorOf,
getCwdSafe,
getCWDURL,
getInternalGlobal,
getSystemErrorMap,
getSystemErrorName,
Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/errors/force_colors.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ throw new Error('Should include grayed stack trace')

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Error: an exception.
at Object.<anonymous> (*typescript-sourcemapping_url_string.ts:3:7)
at Module._compile (node:internal*modules*cjs*loader:1242:14)
at Module._compile (node:internal*modules*cjs*loader:1241:14)

0 comments on commit b151075

Please sign in to comment.