1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-24 08:09:08 -05:00

fix(ext/node): resolve exports even if parent module filename isn't present (#26553)

Fixes https://github.com/denoland/deno/issues/26505

I'm not exactly sure how this case comes about (I tried to write tests
for it but couldn't manage to reproduce it), but what happens is the
parent filename ends up null, and we bail out of resolving the specifier
in package exports.

I've checked, and in node the parent filename is also null (so that's
not a bug on our part), but node continues to resolve even in that case.
So this PR should match node's behavior more closely than we currently
do.
This commit is contained in:
Nathan Whitaker 2024-10-31 10:02:31 -07:00 committed by Bartek Iwańczuk
parent d284d1c6a8
commit 68cee70838
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750
8 changed files with 55 additions and 7 deletions

View file

@ -529,12 +529,16 @@ where
return Ok(None);
};
let referrer = Url::from_file_path(parent_path).unwrap();
let referrer = if parent_path.is_empty() {
None
} else {
Some(Url::from_file_path(parent_path).unwrap())
};
let r = node_resolver.package_exports_resolve(
&pkg.path,
&format!(".{expansion}"),
exports,
Some(&referrer),
referrer.as_ref(),
NodeModuleKind::Cjs,
REQUIRE_CONDITIONS,
NodeResolutionMode::Execution,

View file

@ -523,17 +523,13 @@ function resolveExports(
return;
}
if (!parentPath) {
return false;
}
return op_require_resolve_exports(
usesLocalNodeModulesDir,
modulesPath,
request,
name,
expansion,
parentPath,
parentPath ?? "",
) ?? false;
}

View file

@ -0,0 +1,8 @@
{
"name": "@denotest/cjs-multiple-exports",
"version": "1.0.0",
"exports": {
".": "./src/index.js",
"./add": "./src/add.js"
}
}

View file

@ -0,0 +1,3 @@
module.exports = function add(a, b) {
return a + b;
};

View file

@ -0,0 +1,3 @@
module.exports = {
hello: "world"
};

View file

@ -0,0 +1,13 @@
{
"tempDir": true,
"steps": [
{
"args": "install",
"output": "[WILDCARD]"
},
{
"args": "run -A main.cjs",
"output": "3\n"
}
]
}

View file

@ -0,0 +1,16 @@
const path = require("node:path");
const Module = require("node:module");
function requireFromString(code, filename) {
const paths = Module._nodeModulePaths((0, path.dirname)(filename));
const m = new Module(filename, module.parent);
m.paths = paths;
m._compile(code, filename);
return m.exports;
}
const code = `
const add = require("@denotest/cjs-multiple-exports/add");
console.log(add(1, 2));
`;
requireFromString(code, "fake.js");

View file

@ -0,0 +1,5 @@
{
"dependencies": {
"@denotest/cjs-multiple-exports": "1.0.0"
}
}