diff options
author | 2023-03-05 18:54:00 +0200 | |
---|---|---|
committer | 2023-03-05 08:54:00 -0800 | |
commit | c7bfb3aa3acdd1c8b11782530110aaf417cb79b8 (patch) | |
tree | b5a14c85dc652142757790a2141ea9e9b574dc11 | |
parent | 72737131581bd30fbae66b7de00dc88cee08e207 (diff) | |
download | bun-c7bfb3aa3acdd1c8b11782530110aaf417cb79b8.tar.gz bun-c7bfb3aa3acdd1c8b11782530110aaf417cb79b8.tar.zst bun-c7bfb3aa3acdd1c8b11782530110aaf417cb79b8.zip |
consider current working directory when resolving relative paths (#2313)
* consider current working directory when resolving relative paths
fixes #2298
* comment test
---------
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
-rw-r--r-- | .github/workflows/bun-linux-build.yml | 2 | ||||
-rw-r--r-- | src/resolver/resolve_path.zig | 66 | ||||
-rw-r--r-- | test/bun.js/install/bunx.test.ts | 30 | ||||
-rw-r--r-- | test/bun.js/path.test.js | 1 |
4 files changed, 65 insertions, 34 deletions
diff --git a/.github/workflows/bun-linux-build.yml b/.github/workflows/bun-linux-build.yml index 748c14c55..2b3074b87 100644 --- a/.github/workflows/bun-linux-build.yml +++ b/.github/workflows/bun-linux-build.yml @@ -190,7 +190,7 @@ jobs: bun install bun install --cwd test/bun.js bun install --cwd test/bun.js/third-party/body-parser-test - bun install --cwd test/bun.js/third-party/napi_create_external + #bun install --cwd test/bun.js/third-party/napi_create_external bun install --cwd packages/bun-internal-test node packages/bun-internal-test/src/runner.node.mjs || true - name: Comment on PR diff --git a/src/resolver/resolve_path.zig b/src/resolver/resolve_path.zig index c44c2597d..b1cd6e0bf 100644 --- a/src/resolver/resolve_path.zig +++ b/src/resolver/resolve_path.zig @@ -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; @@ -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]; } } } @@ -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].* = "/..".*; } } } @@ -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 { @@ -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); } diff --git a/test/bun.js/install/bunx.test.ts b/test/bun.js/install/bunx.test.ts index e43dd673d..06266fe94 100644 --- a/test/bun.js/install/bunx.test.ts +++ b/test/bun.js/install/bunx.test.ts @@ -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 }); @@ -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"]); +}); diff --git a/test/bun.js/path.test.js b/test/bun.js/path.test.js index 176d98c8e..ad5688ea7 100644 --- a/test/bun.js/path.test.js +++ b/test/bun.js/path.test.js @@ -274,6 +274,7 @@ it("path.relative", () => { ["/baz-quux", "/baz", "../baz"], ["/baz", "/baz-quux", "../baz-quux"], ["/page1/page2/foo", "/", "../../.."], + [process.cwd(), "foo", "foo"], ], ], ]; |