From 9be8dce0c7f8deaeff5523735e0867194ec04c59 Mon Sep 17 00:00:00 2001 From: Nathan Whitaker <17734409+nathanwhit@users.noreply.github.com> Date: Sat, 21 Sep 2024 16:10:38 -0700 Subject: [PATCH] fix(node): Include "node" condition during CJS re-export analysis (#25785) Fixes #25777. We were missing the "node" condition, so we were resolving to the wrong conditional export, causing our analysis to be incorrect. --- ext/node_resolver/analyze.rs | 2 +- .../@denotest/conditional-exports-node/1.0.0/bad.js | 3 +++ .../conditional-exports-node/1.0.0/good.cjs | 3 +++ .../conditional-exports-node/1.0.0/index.js | 3 +++ .../conditional-exports-node/1.0.0/package.json | 13 +++++++++++++ .../mjs-reexport-cjs/1.0.0/dist/package.cjs.js | 7 +++++++ .../npm/@denotest/mjs-reexport-cjs/1.0.0/index.js | 1 + .../npm/@denotest/mjs-reexport-cjs/1.0.0/index.mjs | 1 + .../@denotest/mjs-reexport-cjs/1.0.0/package.json | 11 +++++++++++ .../node/cjs_reexport_node_condition/__test__.jsonc | 13 +++++++++++++ .../node/cjs_reexport_node_condition/deno.json | 3 +++ .../specs/node/cjs_reexport_node_condition/main.ts | 3 +++ .../node/cjs_reexport_node_condition/package.json | 5 +++++ 13 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tests/registry/npm/@denotest/conditional-exports-node/1.0.0/bad.js create mode 100644 tests/registry/npm/@denotest/conditional-exports-node/1.0.0/good.cjs create mode 100644 tests/registry/npm/@denotest/conditional-exports-node/1.0.0/index.js create mode 100644 tests/registry/npm/@denotest/conditional-exports-node/1.0.0/package.json create mode 100644 tests/registry/npm/@denotest/mjs-reexport-cjs/1.0.0/dist/package.cjs.js create mode 100644 tests/registry/npm/@denotest/mjs-reexport-cjs/1.0.0/index.js create mode 100644 tests/registry/npm/@denotest/mjs-reexport-cjs/1.0.0/index.mjs create mode 100644 tests/registry/npm/@denotest/mjs-reexport-cjs/1.0.0/package.json create mode 100644 tests/specs/node/cjs_reexport_node_condition/__test__.jsonc create mode 100644 tests/specs/node/cjs_reexport_node_condition/deno.json create mode 100644 tests/specs/node/cjs_reexport_node_condition/main.ts create mode 100644 tests/specs/node/cjs_reexport_node_condition/package.json diff --git a/ext/node_resolver/analyze.rs b/ext/node_resolver/analyze.rs index aea33e29ad..deb56d064e 100644 --- a/ext/node_resolver/analyze.rs +++ b/ext/node_resolver/analyze.rs @@ -196,7 +196,7 @@ impl &referrer, // FIXME(bartlomieju): check if these conditions are okay, probably // should be `deno-require`, because `deno` is already used in `esm_resolver.rs` - &["deno", "require", "default"], + &["deno", "node", "require", "default"], NodeResolutionMode::Execution, ); let reexport_specifier = match result { diff --git a/tests/registry/npm/@denotest/conditional-exports-node/1.0.0/bad.js b/tests/registry/npm/@denotest/conditional-exports-node/1.0.0/bad.js new file mode 100644 index 0000000000..e4f1bed50e --- /dev/null +++ b/tests/registry/npm/@denotest/conditional-exports-node/1.0.0/bad.js @@ -0,0 +1,3 @@ +module.exports = { + hello: "bad" +}; \ No newline at end of file diff --git a/tests/registry/npm/@denotest/conditional-exports-node/1.0.0/good.cjs b/tests/registry/npm/@denotest/conditional-exports-node/1.0.0/good.cjs new file mode 100644 index 0000000000..f99f7732aa --- /dev/null +++ b/tests/registry/npm/@denotest/conditional-exports-node/1.0.0/good.cjs @@ -0,0 +1,3 @@ +module.exports = { + hello: "from node" +}; \ No newline at end of file diff --git a/tests/registry/npm/@denotest/conditional-exports-node/1.0.0/index.js b/tests/registry/npm/@denotest/conditional-exports-node/1.0.0/index.js new file mode 100644 index 0000000000..a440bdc744 --- /dev/null +++ b/tests/registry/npm/@denotest/conditional-exports-node/1.0.0/index.js @@ -0,0 +1,3 @@ +export default { + hello: "default export" +} \ No newline at end of file diff --git a/tests/registry/npm/@denotest/conditional-exports-node/1.0.0/package.json b/tests/registry/npm/@denotest/conditional-exports-node/1.0.0/package.json new file mode 100644 index 0000000000..00a43c78f8 --- /dev/null +++ b/tests/registry/npm/@denotest/conditional-exports-node/1.0.0/package.json @@ -0,0 +1,13 @@ +{ + "name": "@denotest/conditional-exports", + "version": "1.0.0", + "type": "module", + "exports": { + ".": { + "node": "./good.cjs", + "require": "./bad.js", + "default": "./index.js" + }, + "./*": "./*" + } +} diff --git a/tests/registry/npm/@denotest/mjs-reexport-cjs/1.0.0/dist/package.cjs.js b/tests/registry/npm/@denotest/mjs-reexport-cjs/1.0.0/dist/package.cjs.js new file mode 100644 index 0000000000..1d1499e2cc --- /dev/null +++ b/tests/registry/npm/@denotest/mjs-reexport-cjs/1.0.0/dist/package.cjs.js @@ -0,0 +1,7 @@ +Object.defineProperty(exports, '__esModule', { value: true }); + +const pkg = require("@denotest/conditional-exports-node"); + +Object.keys(pkg).forEach(function (k) { + if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) exports[k] = pkg[k]; +}); \ No newline at end of file diff --git a/tests/registry/npm/@denotest/mjs-reexport-cjs/1.0.0/index.js b/tests/registry/npm/@denotest/mjs-reexport-cjs/1.0.0/index.js new file mode 100644 index 0000000000..9f407c0b14 --- /dev/null +++ b/tests/registry/npm/@denotest/mjs-reexport-cjs/1.0.0/index.js @@ -0,0 +1 @@ +module.exports = require('./dist/package.cjs.js') \ No newline at end of file diff --git a/tests/registry/npm/@denotest/mjs-reexport-cjs/1.0.0/index.mjs b/tests/registry/npm/@denotest/mjs-reexport-cjs/1.0.0/index.mjs new file mode 100644 index 0000000000..3767e9fcdc --- /dev/null +++ b/tests/registry/npm/@denotest/mjs-reexport-cjs/1.0.0/index.mjs @@ -0,0 +1 @@ +export * from "./index.js"; \ No newline at end of file diff --git a/tests/registry/npm/@denotest/mjs-reexport-cjs/1.0.0/package.json b/tests/registry/npm/@denotest/mjs-reexport-cjs/1.0.0/package.json new file mode 100644 index 0000000000..9a3ef3c842 --- /dev/null +++ b/tests/registry/npm/@denotest/mjs-reexport-cjs/1.0.0/package.json @@ -0,0 +1,11 @@ +{ + "name": "@denotest/mjs-reexport-cjs", + "version": "1.0.0", + "dependencies": { + "@denotest/conditional-exports-node": "*" + }, + "exports": { + "node": "./index.mjs", + "default": "./index.js" + } +} \ No newline at end of file diff --git a/tests/specs/node/cjs_reexport_node_condition/__test__.jsonc b/tests/specs/node/cjs_reexport_node_condition/__test__.jsonc new file mode 100644 index 0000000000..030f2aa072 --- /dev/null +++ b/tests/specs/node/cjs_reexport_node_condition/__test__.jsonc @@ -0,0 +1,13 @@ +{ + "tempDir": true, + "steps": [ + { + "args": "install", + "output": "[WILDCARD]" + }, + { + "args": "run -A ./main.ts", + "output": "from node\n" + } + ] +} diff --git a/tests/specs/node/cjs_reexport_node_condition/deno.json b/tests/specs/node/cjs_reexport_node_condition/deno.json new file mode 100644 index 0000000000..fde86a1efb --- /dev/null +++ b/tests/specs/node/cjs_reexport_node_condition/deno.json @@ -0,0 +1,3 @@ +{ + "nodeModulesDir": "manual" +} diff --git a/tests/specs/node/cjs_reexport_node_condition/main.ts b/tests/specs/node/cjs_reexport_node_condition/main.ts new file mode 100644 index 0000000000..09b8418a67 --- /dev/null +++ b/tests/specs/node/cjs_reexport_node_condition/main.ts @@ -0,0 +1,3 @@ +import { hello } from "@denotest/mjs-reexport-cjs"; + +console.log(hello); diff --git a/tests/specs/node/cjs_reexport_node_condition/package.json b/tests/specs/node/cjs_reexport_node_condition/package.json new file mode 100644 index 0000000000..85ded5d507 --- /dev/null +++ b/tests/specs/node/cjs_reexport_node_condition/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "@denotest/mjs-reexport-cjs": "*" + } +}