Skip to content

Add a Stdin Specification #586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Conversation

jfly
Copy link
Collaborator

@jfly jfly commented May 13, 2025

This completes step 1 of
#573 (comment).

@jfly
Copy link
Collaborator Author

jfly commented Jun 24, 2025

@brianmcgee @zimbatm, is there anything I can do to help move this forward?

@brianmcgee
Copy link
Member

It's unclear to me if this documents the current behaviour or the desired behaviour. If it's the desired behaviour, isn't there some work needing to be done in treefmt to support that before we publish a spec?

@jfly jfly marked this pull request as draft June 28, 2025 15:00
@jfly
Copy link
Collaborator Author

jfly commented Jun 28, 2025

If it's the desired behaviour, isn't there some work needing to be done in treefmt to support that before we publish a spec?

Yes, treefmt needs changes to support this. I'm happy to implement that (in this PR or a separate one). I just don't want to put in the effort until I know we like this approach.

@brianmcgee
Copy link
Member

I'll try to put my brain on this in the next few days. Been a busy few weeks 😫

@jfly
Copy link
Collaborator Author

jfly commented Jul 21, 2025

@brianmcgee, friendly ping

Copy link
Member

@brianmcgee brianmcgee left a comment

Choose a reason for hiding this comment

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

I agree with some of your own musings in the comments you'd left. Otherwise, it seems like a sane spec and one which should be easy enough for formatters to follow.

I also like that this is opt-in.

@brianmcgee
Copy link
Member

P.S. Thanks for taking the time to flesh this out, and sorry for taking a while to get around to reviewing this.

@jfly jfly force-pushed the issue-573-stdin-spec branch 4 times, most recently from 8e6e1cb to 5671029 Compare July 21, 2025 22:25
@jfly jfly force-pushed the issue-573-stdin-spec branch from 5671029 to d8276a6 Compare July 22, 2025 20:02
@jfly jfly requested a review from brianmcgee July 22, 2025 20:09
Copy link
Contributor

@infinisil infinisil left a comment

Choose a reason for hiding this comment

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

LGTM!

@jfly
Copy link
Collaborator Author

jfly commented Jul 22, 2025

@brianmcgee @zimbatm, I could use another review on this. If we're happy with the state of this, then I'd like to work on the underlying implementation.

Copy link
Member

@zimbatm zimbatm left a comment

Choose a reason for hiding this comment

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

Some minor things, but directionally it makes sense.

formatting "virtual files" passed via stdin.

A formatter **MUST** implement the Stdin Specification if its formatting behavior
can depend on the name of the file being formatted.
Copy link
Member

Choose a reason for hiding this comment

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

this is a bit hard to read

Copy link
Collaborator Author

@jfly jfly Aug 10, 2025

Choose a reason for hiding this comment

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

I agree, but I don't know how to fix it. Does flipping the sentence around help? Suggestions appreciated.

If a formatter's behavior can depend on the name of the file being formatted, then it MUST implement the Stdin Specification.

"stdin mode". In stdin mode, the formatter reads file contents from stdin
rather than the filesystem.
- The formatter _MAY_ understand `--stdin-filepath=<path>` as well, but **MUST**
understand the space separated variant.
Copy link
Member

Choose a reason for hiding this comment

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

It would make sense to outline the purpose of the <path> argument explicitly. Both you and I know why it's there, but maybe not the reader.

Copy link
Member

Choose a reason for hiding this comment

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

If the path argument is not needed by the tool, it's also valid to ignore it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've taken a stab at this. Please take a look.

vanilla [Formatter Specification](/reference/formatter-spec), and additionally
satisfy the following:

### 1. `--stdin-filepath` flag
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
### 1. `--stdin-filepath` flag
### 1. `--stdin` flag

KISS?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm lightly opposed to this, for 2 reasons:

  1. I think --stdin-filepath is clearer that the option takes a value.
  2. treefmt itself currently implements a --stdin flag with different semantics, and I would like for treefmt to implement the new stdin spec. Changing this might be awkward?

WDYT?

Comment on lines +7 to +8
Formatters **MAY** also implement the Stdin Specification, which allows
formatting "virtual files" passed via stdin.
Copy link
Member

Choose a reason for hiding this comment

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

How is this support expressed in the treefmt.toml config?

Copy link
Collaborator Author

@jfly jfly Aug 10, 2025

Choose a reason for hiding this comment

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

I was thinking we'd add a new formatter.<mylanguage>.stdin (or stdin_supported) bool that defaults to false if unspecified. Feedback appreciated.

Do you think the stdin spec is the place to put this? I was thinking this would go in our config docs.

@jfly jfly force-pushed the issue-573-stdin-spec branch from d8276a6 to a94c619 Compare August 10, 2025 19:17
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.

4 participants