diff options
author | 2023-01-24 02:21:43 -0800 | |
---|---|---|
committer | 2023-01-24 02:21:43 -0800 | |
commit | e2f709b2a569bb0eff888ac518757c0b2a0dc3ae (patch) | |
tree | e16deec8061bc7605aab5b11ff6f82a60272d6de | |
parent | 4ea104f5bd2cef425051571dadf876d51a024d00 (diff) | |
download | bun-e2f709b2a569bb0eff888ac518757c0b2a0dc3ae.tar.gz bun-e2f709b2a569bb0eff888ac518757c0b2a0dc3ae.tar.zst bun-e2f709b2a569bb0eff888ac518757c0b2a0dc3ae.zip |
Always allow importing `${package}/package.json`
-rw-r--r-- | src/resolver/resolver.zig | 25 | ||||
-rw-r--r-- | test/bun.js/resolve.test.js | 10 |
2 files changed, 35 insertions, 0 deletions
diff --git a/src/resolver/resolver.zig b/src/resolver/resolver.zig index 5ba5eb74f..ca96270f7 100644 --- a/src/resolver/resolver.zig +++ b/src/resolver/resolver.zig @@ -1485,6 +1485,18 @@ pub const Resolver = struct { if (r.handleESMResolution(esm_resolution, abs_package_path, kind, package_json, esm.subpath)) |result| { return .{ .success = result }; + // if they hid "package.json" from "exports", still allow importing it. + if (strings.eqlComptime(esm.subpath, "./package.json")) { + return .{ + .success = .{ + .path_pair = .{ .primary = package_json.source.path }, + .dirname_fd = pkg_dir_info.getFileDescriptor(), + .file_fd = 0, + .is_node_module = package_json.source.path.isNodeModule(), + .package_json = package_json, + .dir_info = dir_info, + }, + }; } return .{ .not_found = {} }; @@ -1704,6 +1716,19 @@ pub const Resolver = struct { var result_copy = result; result_copy.is_node_module = true; return .{ .success = result_copy }; + + // if they hid "package.json" from "exports", still allow importing it. + if (strings.eqlComptime(esm.subpath, "./package.json")) { + return .{ + .success = .{ + .path_pair = .{ .primary = package_json.source.path }, + .dirname_fd = pkg_dir_info.getFileDescriptor(), + .file_fd = 0, + .is_node_module = package_json.source.path.isNodeModule(), + .package_json = package_json, + .dir_info = dir_info, + }, + }; } return .{ .not_found = {} }; diff --git a/test/bun.js/resolve.test.js b/test/bun.js/resolve.test.js index 80e2bb7ba..b794c7604 100644 --- a/test/bun.js/resolve.test.js +++ b/test/bun.js/resolve.test.js @@ -97,6 +97,16 @@ it("import.meta.resolve", async () => { join(import.meta.path, "../node_modules/package-json-exports/foo/bar.js"), ); + // if they never exported /package.json, allow reading from it too + expect(await import.meta.resolve("package-json-exports/package.json")).toBe( + join(import.meta.path, "../node_modules/package-json-exports/package.json"), + ); + + // if an unnecessary ".js" extension was added, try against /baz + expect(await import.meta.resolve("package-json-exports/baz.js")).toBe( + join(import.meta.path, "../node_modules/package-json-exports/foo/bar.js"), + ); + // works with TypeScript compiler edgecases like: // - If the file ends with .js and it doesn't exist, try again with .ts and .tsx expect(await import.meta.resolve("./resolve-typescript-file.js")).toBe( |