Skip to content

Fix argv handling for standalone binaries - remove extra executable name (#22157) - #22169

Merged
Jarred-Sumner merged 4 commits into
mainfrom
claude/fix-argv-issue-22157
Aug 27, 2025
Merged

Jarred-Sumner merged 4 commits into
mainfrom
claude/fix-argv-issue-22157

Conversation

@robobun

@robobun robobun commented Aug 27, 2025

Copy link
Copy Markdown
Collaborator

Summary

Fixes an issue where compiled standalone binaries included an extra executable name argument in process.argv, breaking code that uses node:util.parseArgs() with process.argv.slice(2).

Problem

When running a compiled binary, process.argv incorrectly included the executable name as a third argument:

./my-app
# process.argv = ["bun", "/$bunfs/root/my-app", "./my-app"]  # BUG

This caused parseArgs() to fail with "Unexpected argument" errors, breaking previously valid code.

Solution

Fixed the offset_for_passthrough calculation in cli.zig to always skip the executable name for standalone binaries, ensuring process.argv only contains the runtime name and script path:

./my-app
# process.argv = ["bun", "/$bunfs/root/my-app"]  # FIXED

Test plan

  • Added regression test in test/regression/issue/22157.test.ts
  • Verified existing exec-argv functionality still works correctly
  • Manual testing confirms the fix resolves the parseArgs issue

Fixes #22157

🤖 Generated with Claude Code

@robobun

robobun commented Aug 27, 2025 •

Copy link
Copy Markdown
Collaborator Author
Updated 1:33 AM PT - Aug 27th, 2025

❌ @autofix-ci[bot], your commit 477b14d has 4 failures in Build #24021:


🧪   To try this PR locally:

bunx bun-pr 22169

That installs a local version of the PR into your bun-22169 executable, so you can run:

bun-22169 --bun

Comment thread src/cli.zig Resolved
## Summary

Fixes issue where compiled standalone binaries incorrectly included the
executable name in `process.argv`, breaking code using `parseArgs()`.

## Problem

When running `./my-app`, `process.argv` incorrectly contained:
```
["bun", "/$bunfs/root/my-app", "./my-app"]  // extra executable name
```

This caused `parseArgs({ args: process.argv.slice(2) })` to throw
"Unexpected argument" errors.

## Solution

Fixed offset calculation in `cli.zig` to safely skip executable name:

```zig
// Before: offset_for_passthrough = if (bun.argv.len > 1) 1 else 0;
// After:  offset_for_passthrough = @min(1, bun.argv.len);
```

This handles all edge cases safely:
- `argv.len >= 1`: Skip executable name, pass user args
- `argv.len == 0`: Return empty slice (no crash)

## Test plan

- Added regression test covering basic case and user arguments
- Verified exec-argv functionality still works correctly
- Tested edge case with empty argv (no integer overflow)

Fixes #22157

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@robobun
robobun force-pushed the claude/fix-argv-issue-22157 branch from 275285c to 48bb211 Compare August 27, 2025 03:48
Windows uses B:/~BUN/root/ instead of //root/ for standalone binaries.
Updated test to match both path formats with regex.
@robobun
robobun force-pushed the claude/fix-argv-issue-22157 branch from 631bd8b to 77dfb5f Compare August 27, 2025 07:23
@Jarred-Sumner
Jarred-Sumner merged commit 0315c97 into main Aug 27, 2025
@Jarred-Sumner
Jarred-Sumner deleted the claude/fix-argv-issue-22157 branch August 27, 2025 22:31
robobun added a commit that referenced this pull request Aug 28, 2025
…ame (#22157) (#22169)

## Summary

Fixes an issue where compiled standalone binaries included an extra
executable name argument in `process.argv`, breaking code that uses
`node:util.parseArgs()` with `process.argv.slice(2)`.

## Problem

When running a compiled binary, `process.argv` incorrectly included the
executable name as a third argument:

```bash
./my-app
# process.argv = ["bun", "/$bunfs/root/my-app", "./my-app"]  # BUG
```

This caused `parseArgs()` to fail with "Unexpected argument" errors,
breaking previously valid code.

## Solution

Fixed the `offset_for_passthrough` calculation in `cli.zig` to always
skip the executable name for standalone binaries, ensuring
`process.argv` only contains the runtime name and script path:

```bash  
./my-app
# process.argv = ["bun", "/$bunfs/root/my-app"]  # FIXED
```

## Test plan

- [x] Added regression test in `test/regression/issue/22157.test.ts`
- [x] Verified existing exec-argv functionality still works correctly  
- [x] Manual testing confirms the fix resolves the parseArgs issue

Fixes #22157

🤖 Generated with [Claude Code](https://claude.ai/code)

---------

Co-authored-by: Claude Bot <[email protected]>
Co-authored-by: Claude <[email protected]>
Co-authored-by: Michael H <[email protected]>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
@tram98

tram98 commented Sep 11, 2025

Copy link
Copy Markdown

Thank you for the quick fix @Jarred-Sumner .

You really rock our world, thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bun 1.2.21 includes an extra argument in process.argv in compiled binaries which breaks previously valid code

4 participants