Skip to content

Commit 9a329c0

Browse files
authored
pass test-module-globalpaths-nodepath.js (#18879)
1 parent f677ac3 commit 9a329c0

File tree

4 files changed

+76
-8
lines changed

4 files changed

+76
-8
lines changed

src/bun.js/modules/NodeModuleModule.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -712,11 +712,6 @@ JSC_DEFINE_CUSTOM_SETTER(setNodeModuleWrapper,
712712
return true;
713713
}
714714

715-
JSC_DEFINE_HOST_FUNCTION(jsFunctionInitPaths, (JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
716-
{
717-
return JSC::JSValue::encode(JSC::jsUndefined());
718-
}
719-
720715
static JSValue getModulePrototypeObject(VM& vm, JSObject* moduleObject)
721716
{
722717
auto* globalObject = defaultGlobalObject(moduleObject->globalObject());
@@ -849,8 +844,8 @@ static JSValue getModuleObject(VM& vm, JSObject* moduleObject)
849844
_cache getModuleCacheObject PropertyCallback
850845
_debug getModuleDebugObject PropertyCallback
851846
_extensions getModuleExtensionsObject PropertyCallback
852-
_findPath jsFunctionFindPath Function 3
853-
_initPaths jsFunctionInitPaths Function 0
847+
_findPath jsFunctionFindPath Function 3
848+
_initPaths JSBuiltin Function|Builtin 0
854849
_load jsFunctionLoad Function 1
855850
_nodeModulePaths Resolver__nodeModulePathsForJS Function 1
856851
_pathCache getPathCacheObject PropertyCallback

src/codegen/bundle-functions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ async function processFileSplit(filename: string): Promise<{ functions: BundledB
146146
contents = contents.slice(directive[0].length);
147147
} else if (match[1] === "export function" || match[1] === "export async function") {
148148
// consume async token and function name
149-
const nameMatch = contents.match(/^export\s+(async\s+)?function\s([a-zA-Z0-9]+)\s*/);
149+
const nameMatch = contents.match(/^export\s+(async\s+)?function\s([_a-zA-Z0-9]+)\s*/);
150150
if (!nameMatch)
151151
throw new SyntaxError("Could not parse function name:\n" + contents.slice(0, contents.indexOf("\n")));
152152
const async = Boolean(nameMatch[1]);

src/js/builtins/NodeModuleObject.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Implementation for `require('node:module')._initPaths`. Exists only as a
2+
// compatibility stub. Calling this does not affect the actual CommonJS loader.
3+
export function _initPaths() {
4+
const homeDir = process.platform === "win32" ? process.env.USERPROFILE : Bun.env.HOME;
5+
const nodePath = process.platform === "win32" ? process.env.NODE_PATH : Bun.env.NODE_PATH;
6+
7+
// process.execPath is $PREFIX/bin/node except on Windows where it is
8+
// $PREFIX\node.exe where $PREFIX is the root of the Node.js installation.
9+
const path = require("node:path");
10+
const prefixDir = process.platform === "win32" ?
11+
path.resolve(process.execPath, '..') :
12+
path.resolve(process.execPath, '..', '..');
13+
14+
const paths = [path.resolve(prefixDir, 'lib', 'node')];
15+
16+
if (homeDir) {
17+
paths.unshift(path.resolve(homeDir, '.node_libraries'));
18+
paths.unshift(path.resolve(homeDir, '.node_modules'));
19+
}
20+
21+
if (nodePath) {
22+
paths.unshift(...nodePath.split(path.delimiter).filter(Boolean));
23+
}
24+
25+
const M = require('node:module');
26+
M.globalPaths = paths;
27+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
'use strict';
23+
const common = require('../common');
24+
const assert = require('assert');
25+
const mod = require('module');
26+
27+
let partA, partB;
28+
const partC = '';
29+
30+
if (common.isWindows) {
31+
partA = 'C:\\Users\\Rocko Artischocko\\AppData\\Roaming\\npm';
32+
partB = 'C:\\Program Files (x86)\\nodejs\\';
33+
process.env.NODE_PATH = `${partA};${partB};${partC}`;
34+
} else {
35+
partA = '/usr/test/lib/node_modules';
36+
partB = '/usr/test/lib/node';
37+
process.env.NODE_PATH = `${partA}:${partB}:${partC}`;
38+
}
39+
40+
mod._initPaths();
41+
42+
assert.ok(mod.globalPaths.includes(partA));
43+
assert.ok(mod.globalPaths.includes(partB));
44+
assert.ok(!mod.globalPaths.includes(partC));
45+
46+
assert.ok(Array.isArray(mod.globalPaths));

0 commit comments

Comments
 (0)