Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: execArgv sometimes could include user args #11987

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 49 additions & 18 deletions src/bun.js/node/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4889,7 +4889,8 @@ pub const Process = struct {
}

pub fn getExecArgv(globalObject: *JSC.JSGlobalObject) callconv(.C) JSC.JSValue {
const allocator = globalObject.allocator();
var sfb = std.heap.stackFallback(4096, globalObject.allocator());
const temp_alloc = sfb.get();
const vm = globalObject.bunVM();

if (vm.worker) |worker| {
Expand All @@ -4903,32 +4904,62 @@ pub const Process = struct {
}
}

var args = allocator.alloc(
JSC.ZigString,
// argv omits "bun" because it could be "bun run" or "bun" and it's kind of ambiguous
// argv also omits the script name
bun.argv.len -| 1,
) catch bun.outOfMemory();
defer allocator.free(args);
var used: usize = 0;
const offset = 1;
var args = std.ArrayList(bun.String).initCapacity(temp_alloc, bun.argv.len - 1) catch bun.outOfMemory();
defer args.deinit();

var seen_run = false;
var prev: ?[]const u8 = null;

for (bun.argv[@min(bun.argv.len, offset)..]) |arg| {
if (arg.len == 0)
// we re-parse the process argv to extract execArgv, since this is a very uncommon operation
// it isn't worth doing this as a part of the CLI
for (bun.argv[1..]) |arg| {
defer prev = arg;

if (arg.len >= 1 and arg[0] == '-') {
args.append(bun.String.createUTF8(arg)) catch bun.outOfMemory();
continue;
}

if (arg[0] != '-')
if (!seen_run and bun.strings.eqlComptime(arg, "run")) {
seen_run = true;
continue;
}

if (vm.argv.len > 0 and strings.eqlLong(vm.argv[0], arg, true))
break;
// A set of execArgv args consume an extra argument, so we do not want to
// confuse these with script names.
const map = bun.ComptimeStringMap(void, comptime brk: {
const auto_params = bun.CLI.Arguments.auto_params;
const KV = struct { []const u8, void };
var entries: [auto_params.len]KV = undefined;
var i = 0;
for (auto_params) |param| {
if (param.takes_value != .none) {
if (param.names.long) |name| {
entries[i] = .{ "--" ++ name, {} };
i += 1;
}
if (param.names.short) |name| {
entries[i] = .{ &[_]u8{ '-', name }, {} };
i += 1;
}
}
}

args[used] = JSC.ZigString.fromUTF8(arg);
var result: [i]KV = undefined;
@memcpy(&result, entries[0..i]);
break :brk result;
});

if (prev) |p| if (map.has(p)) {
args.append(bun.String.createUTF8(arg)) catch @panic("OOM");
continue;
};

used += 1;
// we hit the script name
break;
}

return JSC.JSValue.createStringArray(globalObject, args.ptr, used, true);
return bun.String.toJSArray(globalObject, args.items);
}

pub fn getArgv(globalObject: *JSC.JSGlobalObject) callconv(.C) JSC.JSValue {
Expand Down
2 changes: 1 addition & 1 deletion src/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ pub const Arguments = struct {
clap.parseParam("-v, --version Print version and exit") catch unreachable,
clap.parseParam("--revision Print version with revision and exit") catch unreachable,
} ++ auto_or_run_params;
const auto_params = auto_only_params ++ runtime_params_ ++ transpiler_params_ ++ base_params_;
pub const auto_params = auto_only_params ++ runtime_params_ ++ transpiler_params_ ++ base_params_;

const run_only_params = [_]ParamType{
clap.parseParam("--silent Don't print the script command") catch unreachable,
Expand Down
1 change: 1 addition & 0 deletions test/js/node/process/print-process-execArgv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(JSON.stringify({ execArgv: process.execArgv, argv: process.argv.slice(2) }));
14 changes: 14 additions & 0 deletions test/js/node/process/process.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -634,3 +634,17 @@ it("process.hasUncaughtExceptionCaptureCallback", () => {
expect(process.hasUncaughtExceptionCaptureCallback()).toBe(true);
process.setUncaughtExceptionCaptureCallback(null);
});

it("process.execArgv", async () => {
const fixtures = [
["index.ts --bun -a -b -c", [], ["--bun", "-a", "-b", "-c"]],
["--bun index.ts index.ts", ["--bun"], ["index.ts"]],
["run -e bruh -b index.ts foo -a -b -c", ["-e", "bruh", "-b"], ["foo", "-a", "-b", "-c"]],
];

for (const [cmd, execArgv, argv] of fixtures) {
const replacedCmd = cmd.replace("index.ts", join(__dirname, "print-process-execArgv.js"));
const result = await Bun.$`${bunExe()} ${{ raw: replacedCmd }}`.json();
expect(result, `bun ${cmd}`).toEqual({ execArgv, argv });
}
});
Loading