Skip to content

Commit

Permalink
fix(resolver): wildcard tsconfig path not including suffix (#7612)
Browse files Browse the repository at this point in the history
* fix(resolver): wildcard tsconfig path not including suffix

* different approach
  • Loading branch information
james-elicx authored Dec 14, 2023
1 parent 1fc9b56 commit c0ccdcc
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/resolver/resolver.zig
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const bufs = struct {
pub threadlocal var extension_path: [512]u8 = undefined;
pub threadlocal var tsconfig_match_full_buf: [bun.MAX_PATH_BYTES]u8 = undefined;
pub threadlocal var tsconfig_match_full_buf2: [bun.MAX_PATH_BYTES]u8 = undefined;
pub threadlocal var tsconfig_match_full_buf3: [bun.MAX_PATH_BYTES]u8 = undefined;

pub threadlocal var esm_subpath: [512]u8 = undefined;
pub threadlocal var esm_absolute_package_path: [bun.MAX_PATH_BYTES]u8 = undefined;
Expand Down Expand Up @@ -2846,6 +2847,15 @@ pub const Resolver = struct {
const total_length: ?u32 = strings.indexOfChar(original_path, '*');
var prefix_parts = [_]string{ abs_base_url, original_path[0 .. total_length orelse original_path.len] };

// Concatenate the matched text with the suffix from the wildcard path
var matched_text_with_suffix = bufs(.tsconfig_match_full_buf3);
var matched_text_with_suffix_len: usize = 0;
if (total_length != null) {
const suffix = std.mem.trimLeft(u8, original_path[total_length orelse original_path.len ..], "*");
matched_text_with_suffix_len = matched_text.len + suffix.len;
bun.concat(u8, matched_text_with_suffix, &.{ matched_text, suffix });
}

// 1. Normalize the base path
// so that "/Users/foo/project/", "../components/*" => "/Users/foo/components/""
var prefix = r.fs.absBuf(&prefix_parts, bufs(.tsconfig_match_full_buf2));
Expand All @@ -2854,7 +2864,7 @@ pub const Resolver = struct {
// so that "/Users/foo/components/", "/foo/bar" => /Users/foo/components/foo/bar
var parts = [_]string{
prefix,
if (total_length != null) std.mem.trimLeft(u8, matched_text, "/") else "",
if (matched_text_with_suffix_len > 0) std.mem.trimLeft(u8, matched_text_with_suffix[0..matched_text_with_suffix_len], "/") else "",
std.mem.trimLeft(u8, longest_match.suffix, "/"),
};
var absolute_original_path = r.fs.absBuf(
Expand Down
2 changes: 2 additions & 0 deletions test/js/bun/resolve/bar/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// this file is used in resolve.test.js
export default {};
1 change: 1 addition & 0 deletions test/js/bun/resolve/resolve-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ it("import.meta.resolve", async () => {
// works with tsconfig.json "paths"
expect(await import.meta.resolve("foo/bar")).toBe(join(import.meta.path, "../baz.js"));
expect(await import.meta.resolve("@faasjs/baz")).toBe(join(import.meta.path, "../baz.js"));
expect(await import.meta.resolve("@faasjs/bar")).toBe(join(import.meta.path, "../bar/src/index.js"));

// works with package.json "exports"
expect(await import.meta.resolve("package-json-exports/baz")).toBe(
Expand Down
2 changes: 1 addition & 1 deletion test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"node-harness": ["js/node/harness.ts"],
"deno:harness": ["js/deno/harness.ts"],
"foo/bar": ["js/bun/resolve/baz.js"],
"@faasjs/*": ["js/bun/resolve/*.js"]
"@faasjs/*": ["js/bun/resolve/*.js", "js/bun/resolve/*/src/index.js"]
}
},

Expand Down

0 comments on commit c0ccdcc

Please sign in to comment.