aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Ai Hoshino <ambiguous404@gmail.com> 2023-09-12 08:53:43 +0800
committerGravatar GitHub <noreply@github.com> 2023-09-11 17:53:43 -0700
commitf267c1d097923a2d2992f9f60a6dd365fe706512 (patch)
tree7f7ad390882e11595fe10744bc8377ff5e760c08
parentc4507a5db33910258278bd6641dcbe34ab33628e (diff)
downloadbun-f267c1d097923a2d2992f9f60a6dd365fe706512.tar.gz
bun-f267c1d097923a2d2992f9f60a6dd365fe706512.tar.zst
bun-f267c1d097923a2d2992f9f60a6dd365fe706512.zip
fix(path): Fix edge case in `path.relative` (#4811)
Close: #4789
-rw-r--r--src/resolver/resolve_path.zig4
-rw-r--r--test/js/node/path/path.test.js13
2 files changed, 15 insertions, 2 deletions
diff --git a/src/resolver/resolve_path.zig b/src/resolver/resolve_path.zig
index 4800d7d90..f74211709 100644
--- a/src/resolver/resolve_path.zig
+++ b/src/resolver/resolve_path.zig
@@ -471,7 +471,7 @@ pub fn relativePlatform(from: []const u8, to: []const u8, comptime platform: Pla
Fs.FileSystem.instance.top_level_dir,
&relative_from_buf,
&[_][]const u8{
- normalizeStringBuf(from, relative_from_buf[1..], false, platform, true),
+ normalizeStringBuf(from, relative_from_buf[1..], true, platform, true),
},
platform,
);
@@ -484,7 +484,7 @@ pub fn relativePlatform(from: []const u8, to: []const u8, comptime platform: Pla
Fs.FileSystem.instance.top_level_dir,
&relative_to_buf,
&[_][]const u8{
- normalizeStringBuf(to, relative_to_buf[1..], false, platform, true),
+ normalizeStringBuf(to, relative_to_buf[1..], true, platform, true),
},
platform,
);
diff --git a/test/js/node/path/path.test.js b/test/js/node/path/path.test.js
index 3c8a04d72..5865d6182 100644
--- a/test/js/node/path/path.test.js
+++ b/test/js/node/path/path.test.js
@@ -415,6 +415,9 @@ it("path.join", () => {
it("path.relative", () => {
const failures = [];
+ const cwd = process.cwd();
+ const cwdParent = path.dirname(cwd);
+ const parentIsRoot = cwdParent == "/";
const relativeTests = [
// [
@@ -477,6 +480,16 @@ it("path.relative", () => {
["/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"],
+ ["/app/node_modules/pkg", "../static", `../../..${parentIsRoot ? "" : cwdParent}/static`],
+ ["/app/node_modules/pkg", "../../static", `../../..${parentIsRoot ? "" : path.dirname(cwdParent)}/static`],
+ ["/app", "../static", `..${parentIsRoot ? "" : cwdParent}/static`],
+ ["/app", "../".repeat(64) + "static", "../static"],
+ [".", "../static", cwd == "/" ? "static" : "../static"],
+ ["/", "../static", parentIsRoot ? "static" : `${cwdParent}/static`.slice(1)],
+ ["../", "../", ""],
+ ["../", "../../", parentIsRoot ? "" : ".."],
+ ["../../", "../", parentIsRoot ? "" : path.basename(cwdParent)],
+ ["../../", "../../", ""],
],
],
];