Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: add getCWDURL internal util fn #48434

Merged
merged 9 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const experimentalNetworkImports =
getOptionValue('--experimental-network-imports');
const typeFlag = getOptionValue('--input-type');
const { URL, pathToFileURL, fileURLToPath, isURL } = require('internal/url');
const { getCWDURL } = require('internal/util');
const { canParse: URLCanParse } = internalBinding('url');
const { legacyMainResolve: FSLegacyMainResolve } = internalBinding('fs');
const {
Expand Down Expand Up @@ -1095,7 +1096,7 @@ function defaultResolve(specifier, context = {}) {

const isMain = parentURL === undefined;
if (isMain) {
parentURL = pathToFileURL(`${process.cwd()}/`).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
13 changes: 2 additions & 11 deletions lib/internal/modules/esm/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const {
loadPreloadModules,
initializeFrozenIntrinsics,
} = require('internal/process/pre_execution');
const { pathToFileURL } = require('internal/url');
const { getCWDURL } = require('internal/util');
const {
setImportModuleDynamicallyCallback,
setInitializeImportMetaObjectCallback,
Expand Down Expand Up @@ -195,15 +195,6 @@ function isLoaderWorker() {
async function initializeHooks() {
const customLoaderURLs = getOptionValue('--experimental-loader');

let cwd;
try {
// `process.cwd()` can fail if the parent directory is deleted while the process runs.
cwd = process.cwd() + '/';
} catch {
cwd = '/';
}


const { Hooks } = require('internal/modules/esm/hooks');
const esmLoader = require('internal/process/esm_loader').esmLoader;

Expand All @@ -220,7 +211,7 @@ async function initializeHooks() {
loadPreloadModules();
initializeFrozenIntrinsics();

const parentURL = pathToFileURL(cwd).href;
const parentURL = getCWDURL().href;
for (let i = 0; i < customLoaderURLs.length; i++) {
await hooks.register(
customLoaderURLs[i],
Expand Down
12 changes: 2 additions & 10 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 } = require('internal/util');
const { kEmptyObject, getCWDURL } = require('internal/util');

let esmLoader;

Expand All @@ -23,14 +22,7 @@ module.exports = {
try {
const userImports = getOptionValue('--import');
if (userImports.length > 0) {
let cwd;
try {
// `process.cwd()` can fail if the parent directory is deleted while the process runs.
cwd = process.cwd() + '/';
} catch {
cwd = '/';
}
const parentURL = pathToFileURL(cwd).href;
const parentURL = getCWDURL().href;
await SafePromiseAllReturnVoid(userImports, (specifier) => esmLoader.import(
specifier,
parentURL,
Expand Down
31 changes: 31 additions & 0 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,36 @@ 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 {URL} The current working directory or the volume root if it cannot be determined.
*/
function getCWDURL() {
const { sep } = require('path');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move that at the top of the file please?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I do that, the tests fail in the beginning with the following error:

node:path:1101
      validateString(path, `paths[${i}]`);
      ^

TypeError: validateString is not a function
    at Object.resolve (node:path:1101:7)
    at Module._initPaths (node:internal/modules/cjs/loader:1590:10)
    at initializeCJS (node:internal/modules/cjs/loader:387:12)
    at initializeCJSLoader (node:internal/process/pre_execution:661:3)
    at setupUserModules (node:internal/process/pre_execution:162:3)
    at prepareExecution (node:internal/process/pre_execution:132:5)
    at prepareMainThreadExecution (node:internal/process/pre_execution:55:3)
    at node:internal/main/embedding:11:1

const { pathToFileURL } = require('internal/url');

let cwd;

try {
// The implementation of `process.cwd()` already uses proper cache when it can.
// It's a relatively cheap call performance-wise for the most common use case.
cwd = process.cwd();
} catch {
cachedURL ??= pathToFileURL(sep);
}

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

return cachedURL;
}

function getSystemErrorName(err) {
const entry = uvErrmapGet(err);
return entry ? entry[0] : `Unknown system error ${err}`;
Expand Down Expand Up @@ -853,6 +883,7 @@ module.exports = {
filterDuplicateStrings,
filterOwnProperties,
getConstructorOf,
getCWDURL,
getInternalGlobal,
getSystemErrorMap,
getSystemErrorName,
Expand Down