Skip to content

Add support for formatting from stdin - #4681

Merged
anuraaga merged 4 commits into
bufbuild:mainfrom
anuraaga:stdin-format
Sep 16, 2026
Merged

anuraaga merged 4 commits into
bufbuild:mainfrom
anuraaga:stdin-format

Conversation

@anuraaga

@anuraaga anuraaga commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

This adds the --stdin-filepath flag which when set, indicates a filepath to use to identify input passed via stdin instead. One use case is simple editor integration, though I suspect since the issue was filed LSP integration on both sides may have improved enough that this is less of an issue. Another still important one is integration with meta formatters, the Gradle spotless plugin in the linked issue invokes other formatters and unfortunately needs to prepare a temp file only for protos, and rumdl also rejected the tempfile approach for formatting protos within markdown files. With this change, we can eventually have formatting of proto in markdown files which is pretty great for docs pages.

There are various names for a similar flag in many formatters, --filename etc. I went with this one only because it matches Zed's docs which feels like a neutral third party.

Fixes #1035

/cc @stefanvanburen

@anuraaga
anuraaga requested a review from doriable September 15, 2026 06:59
stdinFilePathFlagName,
)
}
data, err := io.ReadAll(container.Stdin())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should there be some sort of limit on this? https://pkg.go.dev/io#LimitReader

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

io.ReadAll matches the current file behavior. Don't think theres a reasonable limit to apply.

if err != nil {
return err
}
diffBuffer := bytes.NewBuffer(nil)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

When creating an initially empty buffer, usually var diffBuffer bytes.Buffer is used, and then you use &diffBuffer.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

diffBuffer := bytes.NewBuffer(nil) matches the current file.

@jonbodner-buf jonbodner-buf left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

one question and one suggestion.

stdinFilePathFlagName,
)
}
data, err := io.ReadAll(container.Stdin())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

io.ReadAll matches the current file behavior. Don't think theres a reasonable limit to apply.

if err != nil {
return err
}
diffBuffer := bytes.NewBuffer(nil)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

diffBuffer := bytes.NewBuffer(nil) matches the current file.

@anuraaga
anuraaga merged commit 634533a into bufbuild:main Sep 16, 2026
9 checks passed
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.

Format accepts input from stdin

3 participants