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: short params parsing within zig-clap fork #14845

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

renanmav
Copy link

@renanmav renanmav commented Oct 26, 2024

What does this PR do?

This pull request fixes an issue with parsing short parameters (e.g., -t) in Bun's fork of zig-clap (upstream repo). The bug was causing errors when parsing single short flags, as reported in #7114. The fix ensures that short flags are correctly recognized and processed while maintaining compatibility with existing behavior.

Note

I'm not super familiar with the Zig language, so I pushed this fix with the help of Cursor AI.

Apologies for any mistakes, appreciate your review.

Changes Made

How did you verify your code works?

I've built the package locally with code changes, and did testing using these commands:

$ bun run build
$ ./build/debug/bun-debug create expo ./awesome-project -t tabs

Output:

Before
[SYS] read(3[/Users/renanmav/renanmav/bun/build/debug/bun-debug], 4096) = 4096 (4.244ms)
[SYS] close(3[/Users/renanmav/renanmav/bun/build/debug/bun-debug])
error: Invalid Argument '-t'
note: Release build will not have this trace by default:
/Users/renanmav/renanmav/bun/src/deps/zig-clap/clap/streaming.zig:231:13: 0x10285137b in err__anon_122110 (bun-debug)
            return _err;
            ^
/Users/renanmav/renanmav/bun/src/deps/zig-clap/clap/streaming.zig:187:13: 0x1022bc2cf in chainging (bun-debug)
            return parser.err(arg, .{ .short = arg[index] }, error.InvalidArgument);
            ^
/Users/renanmav/renanmav/bun/src/deps/zig-clap/clap/streaming.zig:121:34: 0x1022bb33b in normal (bun-debug)
                .short => return try parser.chainging(.{
                                 ^
/Users/renanmav/renanmav/bun/src/deps/zig-clap/clap/streaming.zig:55:35: 0x101e359cf in next (bun-debug)
                .normal => return try parser.normal(),
                                  ^
/Users/renanmav/renanmav/bun/src/deps/zig-clap/clap/comptime.zig:76:20: 0x1020e51b7 in parse__anon_87277 (bun-debug)
            while (try stream.next()) |arg| {
                   ^
/Users/renanmav/renanmav/bun/src/deps/zig-clap/clap.zig:333:12: 0x1020e58eb in parseEx__anon_87276 (bun-debug)
    return try Clap.parse(iter, opt);
           ^
/Users/renanmav/renanmav/bun/src/deps/zig-clap/clap.zig:316:16: 0x1020e5ac7 in parse__anon_87275 (bun-debug)
    res.clap = try parseEx(Id, params, &iter, .{
               ^
/Users/renanmav/renanmav/bun/src/cli/create_command.zig:216:13: 0x1020e5d37 in parse (bun-debug)
            return err;
            ^
/Users/renanmav/renanmav/bun/src/cli/create_command.zig:1680:32: 0x1020e64a3 in extractInfo (bun-debug)
        const create_options = try CreateOptions.parse(ctx);
                               ^
/Users/renanmav/renanmav/bun/src/cli.zig:1972:45: 0x102108fff in start (bun-debug)
                const create_command_info = try CreateCommand.extractInfo(ctx);
After
[SYS] read(3[/Users/renanmav/renanmav/bun/build/debug/bun-debug], 4096) = 4096 (0.108ms)
[SYS] close(3[/Users/renanmav/renanmav/bun/build/debug/bun-debug])
[bunx] initial_bin_name: create-expo
[SYS] openat(7[/Users/renanmav/renanmav/bun], package.json) = 8
[fs] openat(7[/Users/renanmav/renanmav/bun], /Users/renanmav/renanmav/bun/package.json) = 8[/Users/renanmav/renanmav/bun/package.json]
[SYS] close(8[/Users/renanmav/renanmav/bun/package.json])
[alloc] new(PackageJSON) = src.resolver.package_json.PackageJSON@11ef04080
[bunx] package_fmt: create-expo@latest
[bunx] install_param: create-expo@latest
[bunx] result_package_name: create-expo
[bunx] bunx_cache_dir: /private/tmp/bunx-501-create-expo@latest
[SYS] openat(7[/Users/renanmav/renanmav/bun], node_modules/create-expo/package.json) = -1
[SYS] openat(-2, /private/tmp/bunx-501-create-expo@latest/package.json) = -1
[bunx] installing package: /Users/renanmav/renanmav/bun/build/debug/bun-debug add create-expo@latest --no-summary --no-cache --force
[SYS] posix_spawn(/Users/renanmav/renanmav/bun/build/debug/bun-debug) = 0 (74079)
[SYS] read(3[/Users/renanmav/renanmav/bun/build/debug/bun-debug], 4096) = 4096 (0.035ms)
[SYS] close(3[/Users/renanmav/renanmav/bun/build/debug/bun-debug])
[SYS] openat(-2, /Users/renanmav/.bunfig.toml) = -1
[SYS] openat(-2, /private/tmp/bunx-501-create-expo@latest/bunfig.toml) = -1
[SYS] openat(-2, /private/tmp/bunx-501-create-expo@latest) = 4
[SYS] openat(-2, /Users/renanmav/.npmrc) = 5
[SYS] fstat(5[/Users/renanmav/.npmrc]) = 0
[SYS] close(5[/Users/renanmav/.npmrc])
[SYS] openat(-2, .npmrc) = -1
[SYS] openat(-2, /private/tmp/bunx-501-create-expo@latest/package.json) = 7
[SYS] fstat(7[/private/tmp/bunx-501-create-expo@latest/package.json]) = 0
[SYS] close(7[/private/tmp/bunx-501-create-expo@latest/package.json])
[alloc] new(Request) = src.bun.js.api.bun.dns_resolver.InternalDNS.Request@144804080
[SYS] FilePoll.init(0x144904080, generation_number=1, fd=4355)
[SYS] register: FilePoll(0x144904080, generation_number=1) machport (4355)
[SYS] openat(-2, bun.lockb) = -1
[SYS] openat(-2, package-lock.json) = -1
[OverrideMap] parsed 0 overrides
[OverrideMap] looking up override for b22dbb75aa0c7f9c
  🔍 Resolving [1/1] [alloc] new(ThreadlocalAsyncHTTP) = src.http.ThreadlocalAsyncHTTP@146808200
[uws] connect(registry.npmjs.org, 443)
[fetch] Processed 1 tasks
  🔍 Resolving [1/1] [SYS] onKQueueEvent: FilePoll(fd=4355, generation_number=1) = poll_machport | machport | one_shot | has_incremented_poll_count | was_ever_registered
[SYS] onUpdate kevent (fd: 4355) InternalDNSRequest
  🔍 Resolving [1/1] [fetch] Connected https://registry.npmjs.org/create-expo
[uws] us_socket_write(*src.deps.boringssl.translated.SSL@600002af0008, 314) = 314
[uws] us_socket_write(*src.deps.boringssl.translated.SSL@600002af0008, 0) = 0
[fetch] onData 449
[fetch] handleResponseMetadata: content_length is null and transfer_encoding src.http.Encoding.chunked
[fetch] onData 1369
[fetch] onData 2738
[fetch] onData 3348
[fetch] onData 1369
[fetch] onData 1369
[fetch] onData 2908
[fetch] onData 2473
[fetch] Decompressing 15543 bytes
[alloc] new(BrotliReaderArrayList) = src.brotli.BrotliReaderArrayList@147c04080
[fetch] progressUpdate true
[alloc] destroy(BrotliReaderArrayList) = src.brotli.BrotliReaderArrayList@147c04080
[fetch] onAsyncHTTPCallback: 1.384s
[alloc] destroy(ThreadlocalAsyncHTTP) = src.http.ThreadlocalAsyncHTTP@146808200
  🔍 create-expo [2/2] [OverrideMap] looking up override for b22dbb75aa0c7f9c
[SYS] faccessat(8[/Users/renanmav/.bun/install/cache], [email protected]@@@1, O_RDONLY, 0) = 0
[SYS] openat(-2, node_modules) = -1
[SYS] openat(-2, node_modules) = 13
[SYS] renameat(14[/private/tmp/bunx-501-create-expo@latest/node_modules], create-expo, 14[/private/tmp/bunx-501-create-expo@latest/node_modules], .old-61CACBD352C0255A) = 2
[SYS] renameat(14[/private/tmp/bunx-501-create-expo@latest/node_modules], create-expo, 14[/private/tmp/bunx-501-create-expo@latest/node_modules], .old-A9B89845C06917DB) = 2
[SYS] openat([invalid_fd], /private/tmp/bunx-501-create-expo@latest/node_modules/create-expo/build/index.js) = 15
[SYS] read(15[/private/tmp/bunx-501-create-expo@latest/node_modules/create-expo/build/index.js], 1024) = 1024 (0.191ms)
[SYS] close(15[/private/tmp/bunx-501-create-expo@latest/node_modules/create-expo/build/index.js])
  🔒 Saving lockfile... [SYS] openat(-2, .lockb-0e3795f981cd3ebf.tmp) = 14
[SYS] write(14[/private/tmp/bunx-501-create-expo@latest/.lockb-0e3795f981cd3ebf.tmp], 1255) = 1255 (0.035ms)
[SYS] renameat(-2, .lockb-0e3795f981cd3ebf.tmp, -2, bun.lockb) = 0
[SYS] close(14[/private/tmp/bunx-501-create-expo@latest/bun.lockb])
[SYS] openat([invalid_fd], /private/tmp/bunx-501-create-expo@latest/package.json) = 14
✔ Downloaded and extracted project files.
> bun install
bun install v1.1.30 (7996d06b)

+ @babel/[email protected]
+ @types/[email protected] (v18.3.12 available)
+ [email protected]
+ [email protected]
+ [email protected]
+ [email protected] (v5.6.3 available)
+ @expo/[email protected]
+ @react-navigation/[email protected]
+ [email protected]
+ [email protected]
+ [email protected]
+ [email protected]
+ [email protected]
+ [email protected]
+ [email protected]
+ [email protected]
+ [email protected]
+ [email protected]
+ [email protected]
+ [email protected] (v3.16.1 available)
+ [email protected]
+ [email protected]
+ [email protected]

1134 packages installed [14.78s]

✅ Your project is ready!

To run your project, navigate to the directory and run one of the following bun commands.

- cd awesome-project
- bun run android
- bun run iOS
- bun run web

@Jarred-Sumner
Copy link
Collaborator

Can you add automated tests?

@renanmav renanmav force-pushed the renan/fix-short-params branch from c6660a5 to eec63e1 Compare November 6, 2024 16:27
@renanmav
Copy link
Author

renanmav commented Nov 6, 2024

@Jarred-Sumner of course, just rebased this branch against latest main.

How do I unblock CI to run?

@renanmav renanmav force-pushed the renan/fix-short-params branch from 2233c05 to b97ff56 Compare November 9, 2024 23:07
@renanmav
Copy link
Author

renanmav commented Nov 9, 2024

I was curious to see the diff between the zig-clap fork and upstream.

Check it out here: main...renanmav:bun:renan/sync-zig-clap

@renanmav renanmav force-pushed the renan/fix-short-params branch from 1302eaf to 3b611eb Compare November 20, 2024 18:07
@renanmav renanmav force-pushed the renan/fix-short-params branch from 3b611eb to 78bb5f3 Compare November 20, 2024 18:08
Comment on lines -187 to +203
return parser.err(arg, .{ .short = arg[index] }, error.InvalidArgument);
if (warn_on_unrecognized_flag) {
Output.warn("unrecognized bun flag: -{c}, continuing to parse and evaluate flag downstream...\n", .{arg[index]});
Output.flush();
}

// Continue parsing after unrecognized flag
parser.state = .normal;
return parser.next();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main change of this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants