Skip to content

refactor(oxfmt): Rewrite --init mode in JS#16769

Merged
graphite-app[bot] merged 1 commit intomainfrom
12-12-refactor_oxfmt_rewrite_--init_mode_in_js
Dec 12, 2025
Merged

refactor(oxfmt): Rewrite --init mode in JS#16769
graphite-app[bot] merged 1 commit intomainfrom
12-12-refactor_oxfmt_rewrite_--init_mode_in_js

Conversation

@leaysgur
Copy link
Member

@leaysgur leaysgur commented Dec 12, 2025

After giving it some thought, I decided to lean these features toward JS after all.

The reasons are:

  • I want Rust side to focus on complex formatting tasks, while moving infrequently used commands elsewhere
  • Code defining napi callbacks tends to become verbose

While --init itself worked on the Rust side, it will share some lines with upcoming --migrate, so I'm extracting this beforehand too.

Also, We were generating .jsonc, but I've switched to creating .json for now.

This is because running oxfmt immediately after generating .jsonc causes trailing comma diffs. 😨
I was hesitant to introduce a jsonc writer, so .json will enough for now.

@github-actions github-actions bot added A-cli Area - CLI A-formatter Area - Formatter C-cleanup Category - technical debt or refactoring. Solution not expected to change behavior labels Dec 12, 2025
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.

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 biggest concern is Mode::Init => unreachable!(...) in the NAPI path, which can hard-crash the process if --init reaches Rust due to packaging/regression or direct invocation. The JS --init implementation is mostly fine, but $schema detection should match the previous Rust behavior (is_file) to avoid false positives. Consider tightening --init UX by rejecting extra arguments to prevent ambiguous behavior. Lint suppression for console is broader than necessary and should be scoped down.

Additional notes (2)
  • Maintainability | apps/oxfmt/src-js/migration/init.ts:1-2
    The file disables no-console globally, which broadens the blast radius and makes it easy to accidentally introduce unrelated console usage later. Since this module has only a couple of logging lines, prefer narrowing the suppression to the specific lines or using a small logger wrapper that can be mocked in tests. (This is especially important for CLI code where stdout/stderr shape is part of the API.)

  • Readability | apps/oxfmt/src/main_napi.rs:69-73
    Mode::Init => unreachable!(...) will panic if JS ever fails to intercept --init (e.g., someone uses the NAPI entrypoint directly, or a packaging/regression causes the JS CLI to not run this path). Panicking turns a user-facing CLI error into a crash.

Given this mode still exists for help output, prefer returning a controlled non-zero result with a clear message. That preserves stability and makes failures diagnosable.

Summary of changes

Summary

JavaScript CLI

  • Wrapped apps/oxfmt/src-js/cli.ts in an async IIFE and added a JS-side fast path for --init via runInit().
  • Added apps/oxfmt/src-js/migration/init.ts implementing --init scaffolding for .oxfmtrc.json and optional $schema injection.

Rust CLI/NAPI surface

  • Kept Mode::Init for help/arg parsing but changed behavior so Rust/NAPI treats it as unreachable (apps/oxfmt/src/main_napi.rs).
  • Updated help text from .oxfmtrc.jsonc to .oxfmtrc.json (apps/oxfmt/src/cli/command.rs).
  • Removed Rust init module (apps/oxfmt/src/init/mod.rs) and its exposure from apps/oxfmt/src/lib.rs.
  • Simplified CLI exit code mapping by removing init-related variants (apps/oxfmt/src/cli/result.rs).

Tests

  • Added apps/oxfmt/test/init.test.ts covering creation, $schema insertion, and abort behavior.
  • Exported runCli and RunResult from apps/oxfmt/test/utils.ts for reuse.

@charliecreates charliecreates bot removed the request for review from CharlieHelps December 12, 2025 04:45
@leaysgur leaysgur force-pushed the 12-12-refactor_oxfmt_rewrite_--init_mode_in_js branch 2 times, most recently from 86a1ca1 to fb899cf Compare December 12, 2025 05:08
@leaysgur leaysgur changed the title refactor(oxfmt): Rewrite --init mode in JS refactor(oxfmt): Rewrite --init mode in JS Dec 12, 2025
@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

@graphite-app graphite-app bot force-pushed the 12-12-fix_oxfmt_make_rust_cli_as_just_formatting_cli branch from ca7f3b3 to b6f4a3e Compare December 12, 2025 05:31
graphite-app bot pushed a commit that referenced this pull request Dec 12, 2025
After giving it some thought, I decided to lean these features toward JS after all.

The reasons are:

- I want Rust side to focus on complex formatting tasks, while moving infrequently used commands elsewhere
- Code defining `napi` callbacks tends to become verbose

While `--init` itself worked on the Rust side, it will share some lines with upcoming `--migrate`, so I'm extracting this beforehand too.

Also, We were generating `.jsonc`, but I've switched to creating `.json` for now.

This is because running `oxfmt` immediately after generating `.jsonc` causes trailing comma diffs. 😨
I was hesitant to introduce a jsonc writer, so `.json` will enough for now.
@graphite-app graphite-app bot force-pushed the 12-12-refactor_oxfmt_rewrite_--init_mode_in_js branch from fb899cf to 15067ce Compare December 12, 2025 05:31
@graphite-app graphite-app bot added 0-merge Merge with Graphite Merge Queue and removed 0-merge Merge with Graphite Merge Queue labels Dec 12, 2025
@graphite-app graphite-app bot force-pushed the 12-12-fix_oxfmt_make_rust_cli_as_just_formatting_cli branch from b6f4a3e to d719988 Compare December 12, 2025 05:44
After giving it some thought, I decided to lean these features toward JS after all.

The reasons are:

- I want Rust side to focus on complex formatting tasks, while moving infrequently used commands elsewhere
- Code defining `napi` callbacks tends to become verbose

While `--init` itself worked on the Rust side, it will share some lines with upcoming `--migrate`, so I'm extracting this beforehand too.

Also, We were generating `.jsonc`, but I've switched to creating `.json` for now.

This is because running `oxfmt` immediately after generating `.jsonc` causes trailing comma diffs. 😨
I was hesitant to introduce a jsonc writer, so `.json` will enough for now.
graphite-app bot pushed a commit that referenced this pull request Dec 12, 2025
After giving it some thought, I decided to lean these features toward JS after all.

The reasons are:

- I want Rust side to focus on complex formatting tasks, while moving infrequently used commands elsewhere
- Code defining `napi` callbacks tends to become verbose

While `--init` itself worked on the Rust side, it will share some lines with upcoming `--migrate`, so I'm extracting this beforehand too.

Also, We were generating `.jsonc`, but I've switched to creating `.json` for now.

This is because running `oxfmt` immediately after generating `.jsonc` causes trailing comma diffs. 😨
I was hesitant to introduce a jsonc writer, so `.json` will enough for now.
@graphite-app graphite-app bot force-pushed the 12-12-refactor_oxfmt_rewrite_--init_mode_in_js branch from 15067ce to 8f9b5c2 Compare December 12, 2025 05:44
@leaysgur leaysgur force-pushed the 12-12-refactor_oxfmt_rewrite_--init_mode_in_js branch from 8f9b5c2 to bbec437 Compare December 12, 2025 05:46
Base automatically changed from 12-12-fix_oxfmt_make_rust_cli_as_just_formatting_cli to main December 12, 2025 05:49
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Dec 12, 2025
@graphite-app graphite-app bot merged commit bbec437 into main Dec 12, 2025
19 checks passed
@graphite-app graphite-app bot deleted the 12-12-refactor_oxfmt_rewrite_--init_mode_in_js branch December 12, 2025 05:52
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-cleanup Category - technical debt or refactoring. Solution not expected to change behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants