Skip to content
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

buf format: Unstable results with comment after a value in an array in an option #3148

Open
bhollis opened this issue Jul 10, 2024 · 1 comment
Assignees
Labels
Bug Something isn't working

Comments

@bhollis
Copy link

bhollis commented Jul 10, 2024

GitHub Repository

https://github.com/StatelyCloud/buf-format-unstable

Commands

buf format -w && cat format_repro.proto

Output

Either:

syntax = "proto3";

package test;

message FormatTest {
  option (foo.bar) = {
    array: [
      {key: "value"} /* comment */]
  };
}

or

syntax = "proto3";

package test;

message FormatTest {
  option (foo.bar) = {
    array: [
      {key: "value"}/* comment */
    ]
  };
}

Depending on where the closing ] was before you ran buf format.

Expected Output

I expect the formatting to be stable - repeated invocations of buf format should produce the same results.

Anything else?

This is the smallest repro I could find for this - it requires the comment, the object in the array, etc.

@bhollis bhollis added the Bug Something isn't working label Jul 10, 2024
@bhollis bhollis changed the title buf format: Unstable results with buf format: Unstable results with comment after a value in an array in an option Jul 10, 2024
@doriable
Copy link
Member

Hello, thanks for filing this issue, this has to do with the way the comment is being parsed by the AST. In the first example, where there is no whitespace between the comment and the closing bracket, the comment is being parsed as a leading comment of the closing bracket ], whereas in the second example, the comment is being parsed as a trailing comment of the message literal, {key: "value"}.

syntax = "proto3";

package test;

message FormatTest {
  option (foo.bar) = {
    array: [
      {key: "value"} /* comment */]  // comment is considered leading comment of "]"
  };
}

syntax = "proto3";

package test;

message FormatTest {
  option (foo.bar) = {
    array: [
      {key: "value"}/* comment */ // comment is considered trailing comment of {key: "value"}
    ]
  };
}

We are currently working on making this output stable, since you are correct, regardless of how the AST parses it, we should provide a consistent behaviour here.

@doriable doriable self-assigned this Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants