Skip to content

Conversation

@leaysgur
Copy link
Member

@leaysgur leaysgur commented Dec 12, 2025

Previously,

  • oxfmt(napi) not-exists.js returns exitCode: 1
  • oxfmt(rust) not-exists.js returns exitCode: 2 = Expected

To fix this, I updated napi callback return value from bool.

Additionally, I realized that by returning the processing Mode simultaneously, Rust can handle the CLI arguments in better way. Like --init --help, which now shows help.

And finally changed napi binding name from format() to runCli() for the future Node API.

Copy link
Member Author

leaysgur commented Dec 12, 2025


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@leaysgur leaysgur changed the title fix(oxfmt): Report exitCode correctly fix(oxfmt): Report exitCode correctly Dec 12, 2025
@graphite-app graphite-app bot changed the base branch from 12-12-refactor_oxfmt_rewrite_--init_mode_in_js to graphite-base/16770 December 12, 2025 05:31
Copy link

@charliecreates charliecreates bot left a comment

Choose a reason for hiding this comment

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

The change generally aligns exit code reporting between Rust and JS, but the new tuple-based contract has a few correctness and robustness gaps. cli.ts can accidentally assign process.exitCode = undefined if exitCode is missing, and the N-API arg-parse error path currently collapses non-zero parser exit codes into 1, which may undermine “report exitCode correctly”. The .d.ts type is looser than runtime (null), and returning mode as free-form String increases the chance of drift/typos across the FFI boundary.

Additional notes (1)
  • Maintainability | apps/oxfmt/src/main_napi.rs:28-28
    run_cli returns (String, Option<u8>) and constructs mode strings via "cli".to_string() etc. This is error-prone (typos become runtime bugs) and pushes a looser type across the FFI boundary. Since the JS side branches on a closed set of modes, it’s better to encode that as a Rust enum exported to N-API (or at least a &'static str / Cow<'static, str> that can only be one of the known values).

Even if you keep a string, consider centralizing the literals to avoid drift between Rust docs, Rust return values, and TS declaration.

Summary of changes

What changed

  • N-API surface changed from boolean to structured result

    • Replaced exported binding format with runCli / run_cli.
    • Updated TypeScript declarations to return a tuple [mode, exitCode] where mode is one of "cli" | "lsp" | "init" and exitCode is set only for CLI.
  • JS CLI now delegates mode parsing to Rust (apps/oxfmt/src-js/cli.ts)

    • Calls runCli(args, ...) and switches on mode.
    • Sets process.exitCode based on Rust-provided exitCode for "cli" mode.
    • Reintroduces --init handling, but only after Rust indicates "init" mode.
  • Rust exit code handling is centralized (apps/oxfmt/src/cli/result.rs)

    • Added CliRunResult::exit_code() returning u8.
    • Termination::report() now uses self.exit_code().
  • N-API entry point now returns mode + exit code (apps/oxfmt/src/main_napi.rs)

    • format(...) -> bool replaced with run_cli(...) -> (String, Option<u8>).
    • On arg-parse failures, returns ("cli", Some(0|1)) after printing message.
    • Mode::Init now returns ("init", None) instead of unreachable!.
  • Tests updated

    • Snapshots updated to expect exit code 2 for “no files found/expected at least one target file” scenarios.

@charliecreates charliecreates bot removed the request for review from CharlieHelps December 12, 2025 05:34
@graphite-app graphite-app bot force-pushed the 12-12-fix_oxfmt_report_exitcode_correctly branch from f91d0f1 to 2bbdef9 Compare December 12, 2025 05:37
@graphite-app graphite-app bot force-pushed the graphite-base/16770 branch from fb899cf to 2577814 Compare December 12, 2025 05:37
@graphite-app graphite-app bot changed the base branch from graphite-base/16770 to main December 12, 2025 05:37
@graphite-app graphite-app bot force-pushed the 12-12-fix_oxfmt_report_exitcode_correctly branch from 2bbdef9 to aab3331 Compare December 12, 2025 05:37
@Boshen Boshen added the 0-merge Merge with Graphite Merge Queue label Dec 12, 2025
Copy link
Member

Boshen commented Dec 12, 2025

Merge activity

  • Dec 12, 5:41 AM UTC: The merge label '0-merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • Dec 12, 5:53 AM UTC: The merge label '0-merge' was detected. This PR will be added to the Graphite merge queue once it meets the requirements.
  • Dec 12, 5:56 AM UTC: leaysgur added this pull request to the Graphite merge queue.
  • Dec 12, 6:01 AM UTC: Merged by the Graphite merge queue.

@leaysgur
Copy link
Member Author

Too fast approve! I was fixing CI...😗

@graphite-app graphite-app bot changed the base branch from main to graphite-base/16770 December 12, 2025 05:44
@leaysgur leaysgur force-pushed the 12-12-fix_oxfmt_report_exitcode_correctly branch from aab3331 to 6a9424d Compare December 12, 2025 05:46
@leaysgur leaysgur changed the base branch from graphite-base/16770 to 12-12-refactor_oxfmt_rewrite_--init_mode_in_js December 12, 2025 05:46
Base automatically changed from 12-12-refactor_oxfmt_rewrite_--init_mode_in_js to main December 12, 2025 05:52
@leaysgur leaysgur added 0-merge Merge with Graphite Merge Queue and removed 0-merge Merge with Graphite Merge Queue labels Dec 12, 2025
Previously,

- `oxfmt(napi) not-exists.js` returns `exitCode: 1`
- `oxfmt(rust) not-exists.js` returns `exitCode: 2` = Expected

To fix this, I updated napi callback return value from `bool`.

Additionally, I realized that by returning the processing `Mode` simultaneously, Rust can handle the CLI arguments in better way. Like `--init --help`, which now shows help.

And finally changed napi binding name from `format()` to `runCli()` for the future Node API.
@graphite-app graphite-app bot force-pushed the 12-12-fix_oxfmt_report_exitcode_correctly branch from 6a9424d to bc2e0f8 Compare December 12, 2025 05:56
@graphite-app graphite-app bot merged commit bc2e0f8 into main Dec 12, 2025
18 checks passed
@graphite-app graphite-app bot deleted the 12-12-fix_oxfmt_report_exitcode_correctly branch December 12, 2025 06:01
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Dec 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-cli Area - CLI A-formatter Area - Formatter C-bug Category - Bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants