Skip to content

Commit

Permalink
Fix edge case in path.relative. (#3952)
Browse files Browse the repository at this point in the history
Close: #3924
  • Loading branch information
Hanaasagi authored Aug 3, 2023
1 parent 08cf0d5 commit a4d996c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/resolver/resolve_path.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions test/js/node/path/path.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
],
],
];
Expand Down

0 comments on commit a4d996c

Please sign in to comment.