Skip to content

Port baml wrapper to clap and unify formatting via baml_term (B-886) - #4069

Closed
codeshaunted wants to merge 4 commits into
canaryfrom
avery/b-886
Closed

Port baml wrapper to clap and unify formatting via baml_term (B-886)#4069
codeshaunted wants to merge 4 commits into
canaryfrom
avery/b-886

Conversation

@codeshaunted

@codeshaunted codeshaunted commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Fixes B-886.

The baml wrapper hand-rolled its arg parsing, help text, and error/warning output, so its rendering diverged from baml-cli within the same invocation stream: plain unstyled help vs clap's brand-purple help, unprefixed one-line anyhow errors vs the bold-red error: + cause chain, a copy-pasted warning prefix, and no --color/agent-aware color policy.

What changed

  • New baml_term crate (deps: anyhow/clap/console only) owning the shared presentation pieces: CLAP_STYLING, ColorChoice + agent-aware init_color, and the error:/warning: printers. It exists because the wrapper can't depend on baml_exec (pulls the engine) and the pack host can't depend on baml_cli.
  • baml_exec::diag_print deleted; baml_exec re-exports the printers so the pack host keeps its baml_exec-only footprint. baml_cli::paint/reporter re-export from baml_term so internal call sites are unchanged. console dropped from baml_exec.
  • Wrapper ported to clap: toolchain/self-update are now a clap derive tree styled with CLAP_STYLING, with a global --color flag and --manifest-base-url as a proper global arg. Top-level dispatch still peeks at argv[1], so everything except wrapper-owned commands passes through to baml-cli byte-for-byte (a top-level clap parse would break flag forwarding).
  • Wrapper main errors route through print_anyhow_error; the freshness warning uses the shared print_warning.

Deliberate behavior changes

  • Wrapper usage errors follow clap conventions: styled error + usage + did-you-mean, exit 2 (was ad-hoc text, exit 1). Bare baml toolchain prints help and exits 2 (was stdout, exit 0).
  • The shared printers gate styling on stderr (for_stderr) instead of console's stdout default — diag_print was gating on the wrong stream; the docs already claimed stderr behavior.
  • baml-cli's direct-invocation warning now uses the styled printer instead of a raw writeln!; Reporter::warning deduped into print_warning.
  • Removed a hardcoded error: inside the no-toolchain message that the printer now doubles.

Testing

  • cargo nextest on baml (incl. freshness e2e — warning text unchanged), baml_term, baml_exec, baml_cli; clippy clean.
  • Manually exercised: styled toolchain/self-update help, usage errors, typo suggestions, runtime error rendering, --version, --manifest-base-url before/after subcommand, pass-through.

Summary by CodeRabbit

  • New Features

    • Added clap-based command handling for toolchain operations and self-update.
  • Improvements

    • Standardized terminal styling, color initialization, and shared help formatting across CLI surfaces.
    • Unified diagnostic output with consistent error: / warning: prefixes and improved chained-cause reporting.
    • Updated toolchain status messaging to use consistent terminal status formatting.
  • Tests

    • Added a test to validate CLI clap command wiring.

…rm (B-886)

The baml wrapper hand-rolled arg parsing, help text, and error/warning
output, so its rendering diverged from baml-cli in the same invocation
stream. Extract the shared presentation pieces (CLAP_STYLING, ColorChoice
and agent-aware init_color, the error:/warning: printers) into a new tiny
baml_term crate that the wrapper, baml_exec, baml_cli, and the pack host
all consume, and port the wrapper's toolchain/self-update commands to clap.

The wrapper still dispatches on argv[1] so everything except its own
commands passes through to baml-cli byte-for-byte; clap only parses
wrapper-owned commands.

Deliberate behavior changes:
- wrapper usage errors follow clap conventions (styled error + usage,
  exit 2; bare 'baml toolchain' prints help, exit 2)
- wrapper main errors render via print_anyhow_error (bold-red error:
  header + cause chain) instead of anyhow's one-line alternate format
- the shared printers gate styling on stderr (for_stderr) instead of
  console's stdout default, matching where they write
- baml-cli's direct-invocation warning uses the styled printer
@linear

linear Bot commented Jul 17, 2026

Copy link
Copy Markdown

B-886

@vercel

vercel Bot commented Jul 17, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
beps Ready Ready Preview, Comment Jul 17, 2026 6:15pm
promptfiddle Ready Ready Preview, Comment Jul 17, 2026 6:15pm
promptfiddle2 Ready Ready Preview, Comment Jul 17, 2026 6:15pm

Request Review

@coderabbitai

coderabbitai Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: b49a0f6b-ae1d-473e-b201-e7be9d332bcc

📥 Commits

Reviewing files that changed from the base of the PR and between 00a3ab3 and 2bec157.

📒 Files selected for processing (3)
  • baml_language/crates/baml/src/main.rs
  • baml_language/crates/baml_cli/src/reporter.rs
  • baml_language/crates/baml_term/src/lib.rs
🚧 Files skipped from review as they are similar to previous changes (2)
  • baml_language/crates/baml_term/src/lib.rs
  • baml_language/crates/baml/src/main.rs

📝 Walkthrough

Walkthrough

This change adds the shared baml_term crate for terminal styling, diagnostics, help rendering, and status output. CLI consumers migrate to it, while wrapper toolchain and self-update handling move to Clap-derived commands with pass-through behavior preserved.

Changes

Terminal output and wrapper CLI

Layer / File(s) Summary
Shared terminal utilities
baml_language/Cargo.toml, baml_language/crates/baml_term/*, baml_language/stow.toml
Adds shared color initialization, Clap styling, help helpers, status formatting, and error/warning printers, with workspace and dependency-policy registration.
Terminal utility integration
baml_language/crates/baml_cli/*, baml_language/crates/baml_exec/*
Routes color, help styling, status output, and diagnostics through baml_term, removing duplicated implementations and the old diagnostic module.
Clap-based toolchain routing
baml_language/crates/baml/Cargo.toml, baml_language/crates/baml/src/main.rs
Adds Clap command structures for toolchain and self-update flows, forwards parsed arguments to handlers, retains pass-through handling, and validates command wiring in a test.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant User
  participant WrapperCli
  participant baml_term
  participant ToolchainCommand
  User->>WrapperCli: Invoke toolchain or self-update
  WrapperCli->>baml_term: Initialize color
  WrapperCli->>ToolchainCommand: Parse Clap command
  ToolchainCommand->>ToolchainCommand: Dispatch operation
  ToolchainCommand->>baml_term: Print status or diagnostic
  baml_term-->>User: Render terminal output
Loading

Possibly related PRs

Poem

A rabbit hops through purple light,
Sharing warnings crisp and bright.
Clap commands line up in rows,
Toolchain paths now neatly pose.
One terminal crate, paws held high—
“No duplicated styles!” cries I.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly reflects the main changes: clap-based wrapper parsing and shared formatting via baml_term.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch avery/b-886

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown

⏭️ Performance benchmarks were skipped

Perf benchmarks (CodSpeed) are opt-in on pull requests — they no longer run on every push. They always run automatically after merge to canary/main.

To run them on this PR, do any of the following, then push a commit (or re-run CI):

  • Add RUN_CODSPEED=1 to the PR description, or
  • Include run-perf or /perf in the PR title or any commit message.

@github-actions

github-actions Bot commented Jul 17, 2026

Copy link
Copy Markdown

Binary size checks failed

2 violations · ✅ 5 passed

⚠️ Please fix the size gate issues or acknowledge them by updating baselines.

Artifact Platform File Gzip Gated on Baseline Delta Status
baml-cli Linux 🔒 25.2 MB 10.7 MB file 24.5 MB +727.8 KB (+3.0%) OK
packed-program Linux 🔒 17.0 MB 7.0 MB file 17.0 MB -4.1 KB (-0.0%) OK
baml-cli macOS 🔒 19.5 MB 9.3 MB file 18.9 MB +628.4 KB (+3.3%) FAIL
packed-program macOS 🔒 13.2 MB 6.2 MB file 13.2 MB +0 B (+0.0%) OK
baml-cli Windows 🔒 21.0 MB 9.5 MB file 20.4 MB +612.4 KB (+3.0%) OK
packed-program Windows 🔒 14.1 MB 6.2 MB file 14.1 MB -512 B (-0.0%) OK
bridge_wasm WASM 16.2 MB 🔒 4.4 MB gzip 4.3 MB +130.5 KB (+3.1%) FAIL

🔒 = the size this artifact is GATED on (ceiling + delta). Binaries gate on file size (installed binary); WASM gates on gzip (download size). The other size is shown for information only.

Details & how to fix

Violations:

  • baml-cli (macOS) file_bytes: 19.5 MB exceeds limit of 19.5 MB (exceeded by +61.4 KB, policy: max_file_bytes)
  • baml-cli (macOS) file_delta_pct: +3.3% exceeds limit of 3.0% (exceeded by +0.3pp, policy: max_delta_pct)
  • bridge_wasm (WASM) gzip_bytes: 4.4 MB exceeds limit of 4.4 MB (exceeded by +2.3 KB, policy: max_gzip_bytes)
  • bridge_wasm (WASM) gzip_delta_pct: +3.1% exceeds limit of 3.0% (exceeded by +0.1pp, policy: max_delta_pct)

Add/update baselines:

.ci/size-gate/aarch64-apple-darwin.toml:

[artifacts.baml-cli]
file_bytes = 19528576
stripped_bytes = 19528624
gzip_bytes = 9319790

.ci/size-gate/wasm32-unknown-unknown.toml:

[artifacts.bridge_wasm]
file_bytes = 16160508
gzip_bytes = 4402886

Generated by cargo size-gate · workflow run

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
baml_language/crates/baml_term/src/lib.rs (1)

47-54: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Add unit tests for the agent-detection helpers.
A small #[cfg(test)] module here can cover env_truthy and running_in_agent with a few env-var cases.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@baml_language/crates/baml_term/src/lib.rs` around lines 47 - 54, Add a
#[cfg(test)] module in lib.rs covering env_truthy with unset, empty, "0", and
nonzero values, and covering running_in_agent when the configured AGENT_ENV_VARS
variables are absent and when one is set. Ensure tests isolate and clean up
environment variables to avoid affecting other tests.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@baml_language/crates/baml_term/src/lib.rs`:
- Around line 47-54: Add a #[cfg(test)] module in lib.rs covering env_truthy
with unset, empty, "0", and nonzero values, and covering running_in_agent when
the configured AGENT_ENV_VARS variables are absent and when one is set. Ensure
tests isolate and clean up environment variables to avoid affecting other tests.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 328f3fc3-2e62-4385-840f-ae5e1bdc9931

📥 Commits

Reviewing files that changed from the base of the PR and between c67c61e and 09c6a8b.

⛔ Files ignored due to path filters (1)
  • baml_language/Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (14)
  • baml_language/Cargo.toml
  • baml_language/crates/baml/Cargo.toml
  • baml_language/crates/baml/src/main.rs
  • baml_language/crates/baml_cli/Cargo.toml
  • baml_language/crates/baml_cli/src/main.rs
  • baml_language/crates/baml_cli/src/paint.rs
  • baml_language/crates/baml_cli/src/reporter.rs
  • baml_language/crates/baml_exec/Cargo.toml
  • baml_language/crates/baml_exec/src/clap_target.rs
  • baml_language/crates/baml_exec/src/diag_print.rs
  • baml_language/crates/baml_exec/src/lib.rs
  • baml_language/crates/baml_term/Cargo.toml
  • baml_language/crates/baml_term/src/lib.rs
  • baml_language/stow.toml
💤 Files with no reviewable changes (1)
  • baml_language/crates/baml_exec/src/diag_print.rs

@vercel
vercel Bot temporarily deployed to Preview – beps July 17, 2026 00:22 Inactive
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle2 July 17, 2026 00:31 Inactive
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle July 17, 2026 00:43 Inactive
@vercel
vercel Bot temporarily deployed to Preview – beps July 17, 2026 01:15 Inactive
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle2 July 17, 2026 01:22 Inactive
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle July 17, 2026 01:36 Inactive
…ctions

after_help strings render verbatim, so the wrapper's 'Network behavior' /
'Wrapper updates' sections and baml-cli's toolchain pointer read as
unformatted text next to clap's styled output. Add help_heading /
help_literal to baml_term (rendered with CLAP_STYLING; clap prints help
through anstream, which strips the escapes when color is off) and use
them in both binaries.
@vercel
vercel Bot temporarily deployed to Preview – beps July 17, 2026 01:56 Inactive
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle2 July 17, 2026 02:04 Inactive
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle July 17, 2026 02:16 Inactive
…lines

Move format_status into baml_term (verb style now gated on stderr, where
Reporter prints, instead of console's stdout default) and use it for the
wrapper's install/select/uninstall/self-update confirmations, which were
plain lowercase sentences on stdout. They now render like baml-cli's
Reporter lines (12-char right-aligned bold purple verb) and move to
stderr, matching cargo's stream conventions. Informational listings
(--version, toolchain status/list) stay plain, like cargo and rustup.
@vercel
vercel Bot temporarily deployed to Preview – beps July 17, 2026 17:53 Inactive
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle2 July 17, 2026 17:59 Inactive
@blacksmith-sh

blacksmith-sh Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Blacksmith runners detected OOM events on the following jobs:

Job Details
Cargo Tests / cargo build (msrv) View Job

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.

1 participant