Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(resolver): Add NODE_PATH support #14089

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/resolver/resolver.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2135,8 +2135,22 @@ pub const Resolver = struct {
}
}

// Mostly to cut scope, we don't resolve `NODE_PATH` environment variable.
// But also: https://github.com/nodejs/node/issues/38128#issuecomment-814969356
// resolve `NODE_PATH`
// https://nodejs.org/api/modules.html#loading-from-the-global-folders
const node_path: []const u8 = if (r.env_loader) |env_loader| env_loader.get("NODE_PATH") orelse "" else "";
if (node_path.len > 0) {
var it = std.mem.tokenize(u8, node_path, if (Environment.isWindows) ";" else ":");
while (it.next()) |path| {
const abs_path = r.fs.absBuf(&[_]string{ path, import_path }, bufs(.node_modules_check));
if (r.debug_logs) |*debug| {
debug.addNoteFmt("Checking for a package in the NODE_PATH directory \"{s}\"", .{abs_path});
}
if (r.loadAsFileOrDirectory(abs_path, kind)) |res| {
return .{ .success = res };
}
}
}

return .{ .not_found = {} };
}
fn dirInfoForResolution(
Expand Down
80 changes: 78 additions & 2 deletions test/js/bun/resolve/resolve.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { pathToFileURL } from "bun";
import { expect, it } from "bun:test";
import { expect, it, describe } from "bun:test";
import { mkdirSync, writeFileSync } from "fs";
import { bunEnv, bunExe, tempDirWithFiles } from "harness";
import { bunEnv, bunExe, joinP, tempDirWithFiles } from "harness";
import { isWindows } from "../../node/test/common";
import { join, sep } from "path";

it("spawn test file", () => {
Expand Down Expand Up @@ -311,3 +312,78 @@ it.todo("import override to bun:test", async () => {
// @ts-expect-error
expect(await import("#bun_test")).toBeDefined();
});

describe("NODE_PATH test", () => {
const prepareTest = () => {
const tempDir = tempDirWithFiles("node_path", {
"modules/node_modules/node-path-test/index.js": "exports.testValue = 'NODE_PATH works';",
"modules/node_modules/node-path-test/package.json": JSON.stringify({
name: "node-path-test",
version: "1.0.0",
description: "A node_path test module",
main: "index.js",
}),
"lib/node_modules/node-path-test/index.js": "exports.testValue = 'NODE_PATH from lib works';",
"lib/node_modules/node-path-test/package.json": JSON.stringify({
name: "node-path-test",
version: "1.0.0",
description: "A node_path test module from lib",
main: "index.js",
}),
"test/index.js": "const { testValue } = require('node-path-test');\nconsole.log(testValue);",
});

const nodePath = joinP(tempDir, "modules/node_modules");
const nodePathLib = joinP(tempDir, "lib/node_modules");
const testDir = joinP(tempDir, "test");

const delimiter = isWindows ? ";" : ":";

return {
tempDir,
nodePath,
nodePathLib,
testDir,
delimiter,
};
};

it("should resolve modules from NODE_PATH", () => {
const { nodePath, testDir } = prepareTest();

const { exitCode, stdout, stderr } = Bun.spawnSync({
cmd: [bunExe(), "index.js", "--no-install"],
env: { ...bunEnv, NODE_PATH: nodePath },
cwd: testDir,
});

expect(exitCode).toBe(0);
expect(stdout.toString().trim()).toBe("NODE_PATH works");
});

it("should resolve modules from NODE_PATH entries", () => {
const { nodePath, testDir, delimiter } = prepareTest();

const { exitCode, stdout, stderr } = Bun.spawnSync({
cmd: [bunExe(), "index.js", "--no-install"],
env: { ...bunEnv, NODE_PATH: ["/a/path/not/exist", nodePath].join(delimiter) },
cwd: testDir,
});

expect(exitCode).toBe(0);
expect(stdout.toString().trim()).toBe("NODE_PATH works");
});

it("should resolve first modules from NODE_PATH entries", () => {
const { nodePath, nodePathLib, testDir, delimiter } = prepareTest();

const { exitCode, stdout, stderr } = Bun.spawnSync({
cmd: [bunExe(), "index.js", "--no-install"],
env: { ...bunEnv, NODE_PATH: ["/a/path/not/exist", nodePathLib, nodePath].join(delimiter) },
cwd: testDir,
});

expect(exitCode).toBe(0);
expect(stdout.toString().trim()).toBe("NODE_PATH from lib works");
});
});
Loading