Skip to content
Merged
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
4 changes: 4 additions & 0 deletions src/fs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1672,6 +1672,10 @@ pub const Path = struct {

pub fn isJSONCFile(this: *const Path) bool {
const str = this.name.filename;
if (strings.eqlComptime(str, "package.json")) {
return true;
}

if (!(strings.hasPrefixComptime(str, "tsconfig.") or strings.hasPrefixComptime(str, "jsconfig."))) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/js/builtins/ImportMetaObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export function internalRequire(this: ImportMetaObject, id) {
}

// TODO: remove this hardcoding
if (last5 === ".json") {
if (last5 === ".json" && !id.endsWith?.("package.json")) {
var fs = (globalThis[Symbol.for("_fs")] ||= Bun.fs());
var exports = JSON.parse(fs.readFileSync(id, "utf8"));
$requireMap.$set(id, $createCommonJSModule(id, exports, true, undefined));
Expand Down
29 changes: 29 additions & 0 deletions test/cli/run/require-and-import-trailing.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { test, expect, describe } from "bun:test";
import { bunEnv, bunExe, isWindows, tempDirWithFiles } from "harness";
import { join } from "path";

test("require() with trailing slash", () => {
const requireDir = tempDirWithFiles("require-trailing", {
"package.json": `
{
// Comments!
"name": "require-and-import-trailing",
"version": "1.0.0",
},`,
});

expect(require(requireDir + "/package.json").name).toBe("require-and-import-trailing");
});

test("import() with trailing slash", async () => {
const importDir = tempDirWithFiles("import-trailing", {
"package.json": `
{
// Comments!
"name": "require-and-import-trailing",
"version": "1.0.0",
},`,
});

expect((await import(importDir + "/package.json")).default.name).toBe("require-and-import-trailing");
});