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

fix(ext/node): attach error code when require fails on package exports #26631

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
29 changes: 21 additions & 8 deletions ext/node/polyfills/01_require.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ Module.globalPaths = modulePaths;

const CHAR_FORWARD_SLASH = 47;
const TRAILING_SLASH_REGEX = /(?:^|\/)\.?\.$/;
const ERROR_CODE_REGEX = /^\[(\w+)\]/;

// This only applies to requests of a specific form:
// 1. name/.*
Expand All @@ -527,14 +528,26 @@ function resolveExports(
return false;
}

return op_require_resolve_exports(
usesLocalNodeModulesDir,
modulesPath,
request,
name,
expansion,
parentPath,
) ?? false;
try {
return op_require_resolve_exports(
usesLocalNodeModulesDir,
modulesPath,
request,
name,
expansion,
parentPath ?? "",
) ?? false;
} catch (error) {
if (error instanceof Error) {
// TODO(nathanwhit): properly throw an error with the `code` property set
// from rust once the error rework lands
const m = error.message.match(ERROR_CODE_REGEX);
if (m) {
error.code = m[1];
}
}
throw error;
}
}

Module._findPath = function (request, paths, isMain, parentPath) {
Expand Down