diff options
author | 2023-08-04 06:40:37 +0800 | |
---|---|---|
committer | 2023-08-03 15:40:37 -0700 | |
commit | a4d996c337605c40018e997900ee9638f3ea9edf (patch) | |
tree | 7074388542e37f4a5b209428762866f1dc97bef4 | |
parent | 08cf0d562ae81b4fb30aa5e599ca76056582ac5f (diff) | |
download | bun-a4d996c337605c40018e997900ee9638f3ea9edf.tar.gz bun-a4d996c337605c40018e997900ee9638f3ea9edf.tar.zst bun-a4d996c337605c40018e997900ee9638f3ea9edf.zip |
Fix edge case in `path.relative`. (#3952)
Close: #3924
-rw-r--r-- | src/resolver/resolve_path.zig | 13 | ||||
-rw-r--r-- | test/js/node/path/path.test.js | 8 |
2 files changed, 18 insertions, 3 deletions
diff --git a/src/resolver/resolve_path.zig b/src/resolver/resolve_path.zig index 877f32ded..4800d7d90 100644 --- a/src/resolver/resolve_path.zig +++ b/src/resolver/resolve_path.zig @@ -294,13 +294,20 @@ pub fn longestCommonPathGeneric(input: []const []const u8, comptime separator: u // /app/public/ // To detect /app/public is actually a folder, we do one more loop through the strings // and say, "do one of you have a path separator after what we thought was the end?" - for (input) |str| { + var idx = input.len; // Use this value as an invalid value. + for (input, 0..) |str, i| { if (str.len > index) { if (@call(.always_inline, isPathSeparator, .{str[index]})) { - return str[0 .. index + 1]; + idx = i; + } else { + idx = input.len; + break; } } } + if (idx != input.len) { + return input[idx][0 .. index + 1]; + } return input[0][0 .. last_common_separator + 1]; } @@ -340,7 +347,7 @@ pub fn relativeToCommonPath( const shortest = @min(normalized_from.len, normalized_to.len); - var last_common_separator = strings.lastIndexOfChar(_common_path, separator) orelse 0; + const last_common_separator = strings.lastIndexOfChar(_common_path, separator) orelse 0; if (shortest == common_path.len) { if (normalized_to.len > normalized_from.len) { diff --git a/test/js/node/path/path.test.js b/test/js/node/path/path.test.js index 030a7443a..103809d2e 100644 --- a/test/js/node/path/path.test.js +++ b/test/js/node/path/path.test.js @@ -469,6 +469,14 @@ it("path.relative", () => { ["/baz", "/baz-quux", "../baz-quux"], ["/page1/page2/foo", "/", "../../.."], [process.cwd(), "foo", "foo"], + ["/webpack", "/webpack", ""], + ["/webpack/", "/webpack", ""], + ["/webpack", "/webpack/", ""], + ["/webpack/", "/webpack/", ""], + ["/webpack-hot-middleware", "/webpack/buildin/module.js", "../webpack/buildin/module.js"], + ["/webp4ck-hot-middleware", "/webpack/buildin/module.js", "../webpack/buildin/module.js"], + ["/webpack-hot-middleware", "/webp4ck/buildin/module.js", "../webp4ck/buildin/module.js"], + ["/var/webpack-hot-middleware", "/var/webpack/buildin/module.js", "../webpack/buildin/module.js"], ], ], ]; |