diff --git a/cli/tests/unit_node/module_test.ts b/cli/tests/unit_node/module_test.ts index a5c819d960..3a675c7a17 100644 --- a/cli/tests/unit_node/module_test.ts +++ b/cli/tests/unit_node/module_test.ts @@ -1,8 +1,12 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. import { Module } from "node:module"; -import { assertEquals } from "../../../test_util/std/testing/asserts.ts"; +import { + assert, + assertEquals, +} from "../../../test_util/std/testing/asserts.ts"; import process from "node:process"; +import * as path from "node:path"; Deno.test("[node/module _preloadModules] has internal require hook", () => { // Check if it's there @@ -25,3 +29,32 @@ Deno.test("[node/module runMain] loads module using the current process.argv", ( // deno-lint-ignore no-explicit-any assertEquals((globalThis as any).calledViaRunMain, true); }); + +Deno.test("[node/module _nodeModulePaths] prevents duplicate /node_modules/node_modules suffix", () => { + // deno-lint-ignore no-explicit-any + const actual: string[] = (Module as any)._nodeModulePaths( + path.join(process.cwd(), "testdata", "node_modules", "foo"), + ); + + assert( + !actual.some((dir) => /node_modules[/\\]node_modules/g.test(dir)), + "Duplicate 'node_modules/node_modules' suffix found", + ); +}); + +Deno.test("[node/module _nodeModulePaths] prevents duplicate root /node_modules", () => { + // deno-lint-ignore no-explicit-any + const actual: string[] = (Module as any)._nodeModulePaths( + path.join(process.cwd(), "testdata", "node_modules", "foo"), + ); + + assert( + new Set(actual).size === actual.length, + "Duplicate path entries found", + ); + const root = path.parse(actual[0]).root; + assert( + actual.includes(path.join(root, "node_modules")), + "Missing root 'node_modules' directory", + ); +}); diff --git a/ext/node/ops/require.rs b/ext/node/ops/require.rs index 9e13681aed..eb092ab862 100644 --- a/ext/node/ops/require.rs +++ b/ext/node/ops/require.rs @@ -128,16 +128,11 @@ where let mut current_path = from.as_path(); let mut maybe_parent = Some(current_path); while let Some(parent) = maybe_parent { - if !parent.ends_with("/node_modules") { + if !parent.ends_with("node_modules") { paths.push(parent.join("node_modules").to_string_lossy().to_string()); - current_path = parent; - maybe_parent = current_path.parent(); } - } - - if !cfg!(windows) { - // Append /node_modules to handle root paths. - paths.push("/node_modules".to_string()); + current_path = parent; + maybe_parent = current_path.parent(); } Ok(paths) diff --git a/ext/node/polyfills/01_require.js b/ext/node/polyfills/01_require.js index 394015e31c..508a32e126 100644 --- a/ext/node/polyfills/01_require.js +++ b/ext/node/polyfills/01_require.js @@ -607,6 +607,11 @@ Module._findPath = function (request, paths, isMain, parentPath) { return false; }; +/** + * Get a list of potential module directories + * @param {string} fromPath The directory name of the module + * @returns {string[]} List of module directories + */ Module._nodeModulePaths = function (fromPath) { return ops.op_require_node_module_paths(fromPath); };