Skip to content
/ bun Public
forked from oven-sh/bun

Commit

Permalink
consider current working directory when resolving relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Mar 5, 2023
1 parent 42f6b35 commit 4176a73
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 33 deletions.
66 changes: 34 additions & 32 deletions src/resolver/resolve_path.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const strings = @import("../string_immutable.zig");
const FeatureFlags = @import("../feature_flags.zig");
const default_allocator = @import("../memory_allocator.zig").c_allocator;
const bun = @import("bun");
const Fs = @import("../fs.zig");

threadlocal var parser_join_input_buffer: [4096]u8 = undefined;
threadlocal var parser_buffer: [1024]u8 = undefined;

Expand Down Expand Up @@ -153,9 +155,9 @@ pub fn longestCommonPathGeneric(input: []const []const u8, comptime separator: u
// 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| {
if (str.len > index + 1) {
if (str.len > index) {
if (@call(.always_inline, isPathSeparator, .{str[index]})) {
return str[0 .. index + 2];
return str[0 .. index + 1];
}
}
}
Expand Down Expand Up @@ -235,12 +237,11 @@ pub fn relativeToCommonPath(
while (i <= normalized_from.len) : (i += 1) {
if (i == normalized_from.len or (normalized_from[i] == separator and i + 1 < normalized_from.len)) {
if (out_slice.len == 0) {
out_slice = buf[0 .. out_slice.len + 2];
out_slice[0..2].* = "..".*;
buf[0..2].* = "..".*;
out_slice.len = 2;
} else {
const old_len = out_slice.len;
buf[out_slice.len..][0..3].* = "/..".*;
out_slice.len += 3;
out_slice[old_len..][0..3].* = "/..".*;
}
}
}
Expand All @@ -254,23 +255,20 @@ pub fn relativeToCommonPath(
}
}

const insert_leading_slash = last_common_separator > 0 and normalized_to[last_common_separator] != separator and tail[0] != separator;

// avoid making non-absolute paths absolute
const insert_leading_slash = tail[0] != separator and out_slice.len > 0 and out_slice[out_slice.len - 1] != separator;
if (insert_leading_slash) {
buf[out_slice.len] = separator;
out_slice = buf[0 .. out_slice.len + 1];
out_slice.len += 1;
}

// Lastly, append the rest of the destination (`to`) path that comes after
// the common path parts.
const start = out_slice.len;
out_slice = buf[0 .. out_slice.len + tail.len];

bun.copy(u8, out_slice[start..], tail);
bun.copy(u8, buf[out_slice.len..], tail);
out_slice.len += tail.len;
}

return buf[0..out_slice.len];
return out_slice;
}

pub fn relativeNormalized(from: []const u8, to: []const u8, comptime platform: Platform, comptime always_copy: bool) []const u8 {
Expand Down Expand Up @@ -314,27 +312,31 @@ pub fn relative(from: []const u8, to: []const u8) []const u8 {
}

pub fn relativePlatform(from: []const u8, to: []const u8, comptime platform: Platform, comptime always_copy: bool) []const u8 {
const from_allow_above_root = (from.len > 1 and from[0] == platform.separator());
const to_allow_above_root = (to.len > 1 and to[0] == platform.separator());
var normalized_from =
if (!from_allow_above_root)
normalizeStringBuf(from, relative_from_buf[1..], false, platform, true)
else
normalizeStringBuf(from, relative_from_buf[1..], true, platform, true);
var normalized_to =
if (!to_allow_above_root)
normalizeStringBuf(to, relative_to_buf[1..], false, platform, true)
else
normalizeStringBuf(to, relative_to_buf[1..], true, platform, true);

if (from_allow_above_root == to_allow_above_root and from_allow_above_root) {
const normalized_from = if (from.len > 0 and from[0] == platform.separator()) brk: {
var path = normalizeStringBuf(from, relative_from_buf[1..], true, platform, true);
relative_from_buf[0] = platform.separator();
normalized_from = relative_from_buf[0 .. normalized_from.len + 1];
break :brk relative_from_buf[0 .. path.len + 1];
} else joinAbsStringBuf(
Fs.FileSystem.instance.top_level_dir,
&relative_from_buf,
&[_][]const u8{
normalizeStringBuf(from, relative_from_buf[1..], false, platform, true),
},
platform,
);

const normalized_to = if (to.len > 0 and to[0] == platform.separator()) brk: {
var path = normalizeStringBuf(to, relative_to_buf[1..], true, platform, true);
relative_to_buf[0] = platform.separator();
normalized_to = relative_to_buf[0 .. normalized_to.len + 1];
}
//
break :brk relative_to_buf[0 .. path.len + 1];
} else joinAbsStringBuf(
Fs.FileSystem.instance.top_level_dir,
&relative_to_buf,
&[_][]const u8{
normalizeStringBuf(to, relative_to_buf[1..], false, platform, true),
},
platform,
);

return relativeNormalized(normalized_from, normalized_to, platform, always_copy);
}
Expand Down
30 changes: 29 additions & 1 deletion test/bun.js/install/bunx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { readdirSorted } from "./dummy.registry";
let x_dir;

beforeEach(async () => {
x_dir = realpathSync(await mkdtemp(join(tmpdir(), "bun-install.test")));
x_dir = realpathSync(await mkdtemp(join(tmpdir(), "bun-x.test")));
});
afterEach(async () => {
await rm(x_dir, { force: true, recursive: true });
Expand Down Expand Up @@ -179,3 +179,31 @@ for (const entry of await decompress(Buffer.from(buffer))) {
expect(await exited).toBe(0);
expect(await readdirSorted(x_dir)).toEqual([".cache", "test.js"]);
});

it("should execute from current working directory", async () => {
await writeFile(
join(x_dir, "test.js"),
`
console.log(
6
*
7
)`,
);
const { stdout, stderr, exited } = spawn({
cmd: [bunExe(), "--bun", "x", "uglify-js", "test.js", "--compress"],
cwd: x_dir,
stdout: null,
stdin: "pipe",
stderr: "pipe",
env,
});
expect(stderr).toBeDefined();
const err = await new Response(stderr).text();
expect(err).not.toContain("error");
expect(stdout).toBeDefined();
const out = await new Response(stdout).text();
expect(out.split(/\r?\n/)).toEqual(["console.log(42);", ""]);
expect(await exited).toBe(0);
expect(await readdirSorted(x_dir)).toEqual(["test.js"]);
});
1 change: 1 addition & 0 deletions test/bun.js/path.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ it("path.relative", () => {
["/baz-quux", "/baz", "../baz"],
["/baz", "/baz-quux", "../baz-quux"],
["/page1/page2/foo", "/", "../../.."],
[process.cwd(), "foo", "foo"],
],
],
];
Expand Down

0 comments on commit 4176a73

Please sign in to comment.