From 7d554ed1756c94ba0e37dd8d034ad5e60ee06df2 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Tue, 26 Oct 2021 16:49:34 -0700 Subject: [PATCH] [bun run] Fix passthrough behavior --- src/cli/run_command.zig | 42 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/src/cli/run_command.zig b/src/cli/run_command.zig index c0f994ec79..3a80399782 100644 --- a/src/cli/run_command.zig +++ b/src/cli/run_command.zig @@ -402,8 +402,46 @@ pub const RunCommand = struct { var passthrough: []const string = &[_]string{}; - if (positionals.len > 1) { - passthrough = positionals[1..]; + var passthrough_list = std.ArrayList(string).init(ctx.allocator); + if (script_name_to_search.len > 0) { + get_passthrough: { + + // If they explicitly pass "--", that means they want everything after that to be passed through. + for (std.os.argv) |argv, i| { + if (strings.eqlComptime(std.mem.span(argv), "--")) { + if (std.os.argv.len > i + 1) { + var count: usize = 0; + for (std.os.argv[i + 1 ..]) |arg| { + count += 1; + } + try passthrough_list.ensureTotalCapacity(count); + + for (std.os.argv[i + 1 ..]) |arg| { + passthrough_list.appendAssumeCapacity(std.mem.span(arg)); + } + + passthrough = passthrough_list.toOwnedSlice(); + break :get_passthrough; + } + } + } + + // If they do not pass "--", assume they want everything after the script name to be passed through. + for (std.os.argv) |argv, i| { + if (strings.eql(std.mem.span(argv), script_name_to_search)) { + if (std.os.argv.len > i + 1) { + try passthrough_list.ensureTotalCapacity(std.os.argv[i + 1 ..].len); + + for (std.os.argv[i + 1 ..]) |arg| { + passthrough_list.appendAssumeCapacity(std.mem.span(arg)); + } + + passthrough = passthrough_list.toOwnedSlice(); + break :get_passthrough; + } + } + } + } } var did_print = false;