2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-03-27 15:54:22 -04:00
|
|
|
|
2023-07-02 14:19:30 -04:00
|
|
|
import { createRequire, Module } from "node:module";
|
2023-12-01 21:20:06 -05:00
|
|
|
import { assert, assertEquals } from "../../../test_util/std/assert/mod.ts";
|
2023-05-13 01:49:11 -04:00
|
|
|
import process from "node:process";
|
fix(node): duplicate node_module suffixes (#19222)
Noticed that we're checking more module paths than necessary. In
particular the module path array contains a couple of entries with a
duplicated `node_modules/node_modules` suffix.
```js
[
// ... more entries before here, where some also contain duplicate suffixes
"/Users/marvinhagemeister/dev/preact-render-to-string/node_modules/.deno/node_modules",
"/Users/marvinhagemeister/dev/preact-render-to-string/node_modules/node_modules", // <-- duplicate suffix
"/Users/marvinhagemeister/dev/preact-render-to-string/node_modules",
"/Users/marvinhagemeister/dev/node_modules",
"/Users/marvinhagemeister/node_modules",
"/Users/node_modules",
"/node_modules",
"/node_modules" // <-- duplicate entry
]
```
This was caused by a misunderstanding in how Rust's
[`Path::ends_with()`](https://doc.rust-lang.org/std/path/struct.Path.html#method.ends_with)
works. It's designed to match on whole path segments and the suffix
`/node_modules` is not that, except for the root entry. This meant that
our check for if the path already ended with `node_module` always
returned `false`. Removing the leading slash fixes that.
While we're at it, we can remove the last condition where we explicitly
added the root `/node_modules` entry since the while loop prior to that
takes care of it already.
2023-05-23 06:46:14 -04:00
|
|
|
import * as path from "node:path";
|
2023-03-27 15:54:22 -04:00
|
|
|
|
|
|
|
Deno.test("[node/module _preloadModules] has internal require hook", () => {
|
|
|
|
// Check if it's there
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
(Module as any)._preloadModules([
|
|
|
|
"./cli/tests/unit_node/testdata/add_global_property.js",
|
|
|
|
]);
|
|
|
|
// deno-lint-ignore no-explicit-any
|
2023-05-13 01:49:11 -04:00
|
|
|
assertEquals((globalThis as any).foo, "Hello");
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("[node/module runMain] loads module using the current process.argv", () => {
|
|
|
|
process.argv = [
|
|
|
|
process.argv[0],
|
|
|
|
"./cli/tests/unit_node/testdata/add_global_property_run_main.js",
|
|
|
|
];
|
|
|
|
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
(Module as any).runMain();
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
assertEquals((globalThis as any).calledViaRunMain, true);
|
2023-03-27 15:54:22 -04:00
|
|
|
});
|
fix(node): duplicate node_module suffixes (#19222)
Noticed that we're checking more module paths than necessary. In
particular the module path array contains a couple of entries with a
duplicated `node_modules/node_modules` suffix.
```js
[
// ... more entries before here, where some also contain duplicate suffixes
"/Users/marvinhagemeister/dev/preact-render-to-string/node_modules/.deno/node_modules",
"/Users/marvinhagemeister/dev/preact-render-to-string/node_modules/node_modules", // <-- duplicate suffix
"/Users/marvinhagemeister/dev/preact-render-to-string/node_modules",
"/Users/marvinhagemeister/dev/node_modules",
"/Users/marvinhagemeister/node_modules",
"/Users/node_modules",
"/node_modules",
"/node_modules" // <-- duplicate entry
]
```
This was caused by a misunderstanding in how Rust's
[`Path::ends_with()`](https://doc.rust-lang.org/std/path/struct.Path.html#method.ends_with)
works. It's designed to match on whole path segments and the suffix
`/node_modules` is not that, except for the root entry. This meant that
our check for if the path already ended with `node_module` always
returned `false`. Removing the leading slash fixes that.
While we're at it, we can remove the last condition where we explicitly
added the root `/node_modules` entry since the while loop prior to that
takes care of it already.
2023-05-23 06:46:14 -04:00
|
|
|
|
|
|
|
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",
|
|
|
|
);
|
|
|
|
});
|
2023-07-02 14:19:30 -04:00
|
|
|
|
|
|
|
Deno.test("Built-in Node modules have `node:` prefix", () => {
|
|
|
|
let thrown = false;
|
|
|
|
try {
|
|
|
|
// @ts-ignore We want to explicitly test wrong call signature
|
|
|
|
createRequire();
|
|
|
|
} catch (e) {
|
|
|
|
thrown = true;
|
|
|
|
const stackLines = e.stack.split("\n");
|
|
|
|
// Assert that built-in node modules have `node:<mod_name>` specifiers.
|
|
|
|
assert(stackLines.some((line: string) => line.includes("(node:module:")));
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(thrown);
|
|
|
|
});
|