|
| 1 | +--- |
| 2 | +outline: deep |
| 3 | +--- |
| 4 | + |
| 5 | +# Stdin Specification |
| 6 | + |
| 7 | +Formatters **MAY** also implement the Stdin Specification, which allows |
| 8 | +formatting "virtual files" passed via stdin. |
| 9 | + |
| 10 | +A formatter **MUST** implement the Stdin Specification if its formatting behavior |
| 11 | +can depend on the name of the file being formatted. |
| 12 | + |
| 13 | +## Rules |
| 14 | + |
| 15 | +In order for the formatter to comply with this spec, it **MUST** implement the |
| 16 | +vanilla [Formatter Specification](/reference/formatter-spec), and additionally |
| 17 | +satisfy the following: |
| 18 | + |
| 19 | +### 1. `--stdin-filepath` flag |
| 20 | + |
| 21 | +The formatter's CLI **MUST** be of the form: |
| 22 | + |
| 23 | +``` |
| 24 | +<command> [options] [--stdin-filepath <path>] |
| 25 | +``` |
| 26 | + |
| 27 | +Where: |
| 28 | + |
| 29 | +- `<command>` is the name of the formatting tool. |
| 30 | +- `[options]` are any number of flags and options that the formatter accepts. |
| 31 | +- `--stdin-filepath <path>` is an optional flag that puts the formatter in |
| 32 | + "stdin mode". In stdin mode, the formatter reads file contents from stdin |
| 33 | + rather than the filesystem. |
| 34 | + - The formatter _MAY_ alter its behavior based on the given `<path>`. For |
| 35 | + example, if a there are different formatting rules in different |
| 36 | + directories. If the formatter's behavior doesn't depend on the given |
| 37 | + `<path>`, it's ok to ignore it. |
| 38 | + - The formatter _MAY_ understand `--stdin-filepath=<path>` as well, but **MUST** |
| 39 | + understand the space separated variant. |
| 40 | + |
| 41 | +Example: |
| 42 | + |
| 43 | +``` |
| 44 | +$ echo "{}" | nixfmt --stdin-filepath path/to/file.nix |
| 45 | +``` |
| 46 | + |
| 47 | +### 2. Print to stdout, do not assume file is present on filesystem |
| 48 | + |
| 49 | +When in stdin mode, the formatter: |
| 50 | + |
| 51 | +1. **MUST** print the formatted file to stdout. |
| 52 | +2. **MUST NOT** attempt to read the file on the filesystem. Instead, it |
| 53 | + **MUST** read from stdin. |
| 54 | +3. **MUST NOT** write to the given path on the filesytem. It _MAY_ write to |
| 55 | + temporary files elsewhere on disk, but _SHOULD_ clean them up when done. |
0 commit comments