Skip to content

feat(fmt): support --stdin-filepath - #167

Merged
chenjiahan merged 1 commit into
mainfrom
feat/fmt-stdin-filepath
Aug 4, 2026
Merged

feat(fmt): support --stdin-filepath#167
chenjiahan merged 1 commit into
mainfrom
feat/fmt-stdin-filepath

Conversation

@fi3ework

@fi3ework fi3ework commented Aug 3, 2026

Copy link
Copy Markdown
Member

Summary

Adds rs fmt --stdin-filepath <path>, the stdin formatting mode that editor integrations (conform.nvim, none-ls, etc.) rely on — the equivalent of Prettier's --stdin-filepath. Content is read from stdin, formatted as if it were saved at <path>, and written to stdout.

Note: this flag is an interim solution for editor integration. The long-term plan is to serve editors through LSP (the route Biome's biome lsp-proxy and oxfmt's oxfmt --lsp take). Prettier itself ships no official LSP server, so --stdin-filepath remains the de-facto editor protocol for Prettier-based formatters, and editor plugins already know how to drive it.

The semantics were decided after surveying how Prettier, Biome, and oxfmt implement their equivalent flags:

  • Only the flag triggers stdin mode; combining it with --write / --check / --list-different or file arguments is an error (exit 2), matching oxfmt's parser-level exclusivity rather than Prettier/Biome's silent-ignore behavior.
  • The path does not need to exist on disk and is resolved against cwd; it drives parser inference, overrides matching, and sortPackageJson.
  • An ignored path (ignorePatterns or default lock files) skips formatting and writes the input unchanged with exit 0 — all three surveyed tools agree on this, and it keeps editor buffers safe. .gitignore is not consulted, consistent with how explicitly passed file arguments already behave.
  • stdout carries only the formatted output; diagnostics go to stderr. No parser / parse errors exit 2 with an empty stdout. Empty input produces empty output with exit 0. EPIPE from an early-closing reader (| head) is not an error.
  • stdin is fully drained before any failable work surfaces, so a blocked writer sees the real error instead of EPIPE; config loading overlaps with the drain.

Implementation

  • src/fmt/format.ts: formatting core extracted from worker.ts (plugins → parser inference → Prettier format), with a lazy source reader so unsupported files are still skipped before reading. worker.ts keeps only file I/O and changed/unchanged semantics.
  • src/fmt/stdin.ts: the stdin runner — single file, main thread, no worker pool. Plugins load on demand through the shared fmtPlugins chunk, keeping the lazy-loading behavior introduced in perf(fmt): load plugin resolver on demand #164.
  • src/fmt/cli.ts: flag parsing (kebab + camel, following the existing convention), conflict validation, and a one-line stdin branch.

Tests

  • Unit: flag parsing and conflicts, formatting core (options, sortPackageJson, unsupported-before-read, parse errors).
  • Integration (spawned CLI with piped stdin): byte-exact stdout for .ts/.json, overrides application, ignored-path passthrough, unknown-extension and parse-error exit codes, flag conflicts, empty input.

Full suite: 176 tests green; lint, type-check, and format check clean.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Aug 3, 2026

Copy link
Copy Markdown

Deploying rstack-cli with  Cloudflare Pages  Cloudflare Pages

Latest commit: d7e8c28
Status: ✅  Deploy successful!
Preview URL: https://b6e53233.rstack-cli.pages.dev
Branch Preview URL: https://feat-fmt-stdin-filepath.rstack-cli.pages.dev

View logs

@fi3ework
fi3ework force-pushed the feat/fmt-stdin-filepath branch from 841fdd7 to d7e8c28 Compare August 3, 2026 16:51
@fi3ework
fi3ework requested a review from chenjiahan August 3, 2026 17:14

@chenjiahan chenjiahan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👍

@chenjiahan
chenjiahan merged commit d30c190 into main Aug 4, 2026
4 checks passed
@chenjiahan
chenjiahan deleted the feat/fmt-stdin-filepath branch August 4, 2026 02:13
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