Skip to content
Closed
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
17 changes: 11 additions & 6 deletions src/resolver/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ pub struct Bufs {
pub dir_info_uncached_path: PathBuffer,
pub tsconfig_base_url: PathBuffer,
pub relative_abs_path: PathBuffer,
pub custom_dir_path_abs: PathBuffer,
pub load_as_file_or_directory_via_tsconfig_base_path: PathBuffer,
pub node_modules_check: PathBuffer,
pub field_abs_path: PathBuffer,
Expand Down Expand Up @@ -2097,12 +2098,16 @@ impl<'a> Resolver<'a> {
bun_core::hint::cold();
for custom_path in custom_paths {
let custom_utf8 = custom_path.to_utf8_without_ref();
match self.check_package_path(
custom_utf8.slice(),
import_path,
kind,
global_cache,
) {
// `require.resolve` `paths` entries are user-provided and may be
// relative; Node resolves them against the current directory.
let Some(abs_custom_path) = self
.fs_ref()
.abs_buf_checked(&[custom_utf8.slice()], bufs!(custom_dir_path_abs))
else {
continue;
};
match self.check_package_path(abs_custom_path, import_path, kind, global_cache)
{
ResultUnion::Success(res) => return ResultUnion::Success(res),
ResultUnion::Pending(p) => return ResultUnion::Pending(p),
ResultUnion::Failure(p) => return ResultUnion::Failure(p),
Expand Down
22 changes: 21 additions & 1 deletion test/js/node/module/node-module-module.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, test } from "bun:test";
import { bunEnv, bunExe, ospath } from "harness";
import { bunEnv, bunExe, ospath, tempDir } from "harness";
import Module, { _nodeModulePaths, builtinModules, createRequire, isBuiltin, wrap } from "module";
import path from "path";

Expand Down Expand Up @@ -233,6 +233,26 @@ describe.concurrent("node-module-module", () => {
expect(require("./esm_to_cjs_interop.mjs")).toEqual(Symbol.for("meow"));
});

test("require.resolve options.paths resolves relative entries against the working directory", async () => {
using dir = tempDir("resolve-paths-relative", {
"sub/node_modules/paths-rel-pkg/package.json": JSON.stringify({ name: "paths-rel-pkg", main: "index.js" }),
"sub/node_modules/paths-rel-pkg/index.js": `module.exports = "from-sub";`,
"main.cjs": `console.log(require(require.resolve("paths-rel-pkg", { paths: ["sub"] })));`,
});

await using proc = Bun.spawn({
cmd: [bunExe(), "main.cjs"],
env: bunEnv,
cwd: String(dir),
stdout: "pipe",
stderr: "pipe",
});

const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(stdout.trim()).toBe("from-sub");
expect(exitCode).toBe(0);
});

test("Module.runMain", async () => {
await using proc = Bun.spawn({
cmd: [
Expand Down
Loading