buf icon indicating copy to clipboard operation
buf copied to clipboard

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

Open bhollis opened this issue 1 year ago • 1 comments

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 avatar Jul 10 '24 16:07 bhollis

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 avatar Jul 16 '24 19:07 doriable