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

Regression with wrap_comments=true #5862

Open
rwestphal opened this issue Jul 28, 2023 · 8 comments
Open

Regression with wrap_comments=true #5862

rwestphal opened this issue Jul 28, 2023 · 8 comments
Labels
a-comments only-with-option requires a non-default option value to reproduce p-low

Comments

@rwestphal
Copy link

Hi all,

I've noticed a regression that happened between rustfmt 1.5.2-nightly (fe7454b 2023-06-19) and rustfmt 1.5.3-nightly (4651421 2023-06-20).

In my project [1], whenever I run cargo fmt, some comment lines are mangled unexpectedly. Here's the full diff: https://pastebin.com/raw/HwqgrCUz

If I disable wrap_comments=true in rustfmt.toml, the problem no longer occurs.

Interestingly, I couldn't reproduce the problem using a small demonstration program. It only happens when using rustfmt or cargo fmt in my project (which has around 75k LoC).

This is similar to #5835, but not quite the same problem.

Please let me know if you need more info to look into this.

[1] https://github.com/rwestphal/holo

@xxchan
Copy link
Contributor

xxchan commented Jul 29, 2023

This looks exactly the same as #5835 Oh, it's not. . is due to numbered list, but why + is also affected 🤔️

@xxchan
Copy link
Contributor

xxchan commented Jul 29, 2023

It seems #5423 also changed how unordered list works

@xxchan
Copy link
Contributor

xxchan commented Jul 29, 2023

In theory your graph contains markdown lists. You can change it to table characters or wrap it in "```", which will also make hovering doc nicer.

image
/// ```text
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |                                                               |
/// +                                                               +
/// |                     Mandatory Parameters                      |
/// +                                                               +
/// |                                                               |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// ``` 
fn a() {}
image

@xxchan
Copy link
Contributor

xxchan commented Jul 29, 2023

But you used comment // instead of doc comment ///. Not sure whether it's reasonable to assume // is markdown 🤔️


Rust style guide doesn't mention Markdown

https://doc.rust-lang.org/nightly/style-guide/index.html#comments

Rustdoc says it uses Markdown

https://doc.rust-lang.org/rustdoc/how-to-write-documentation.html?highlight=markdown#markdown

@ytmimi ytmimi added a-comments only-with-option requires a non-default option value to reproduce p-low labels Aug 1, 2023
@ytmimi
Copy link
Contributor

ytmimi commented Aug 1, 2023

@xxchan thanks for helping to look into this and for providing a workaround for the issue.

@rwestphal Thanks for the report. Confirming I can reproduce this with rustfmt 1.6.0-nightly (a9ae7462 2023-07-19). Here's a minimal reproducible example.

input

// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// |                                                               |
// +                                                               +
// |                     Mandatory Parameters                      |
// +                                                               +
// |                                                               |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

output (using wrap_comments=true):

// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// |                                                               |
// + +
// |                     Mandatory Parameters                      |
// + +
// |                                                               |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

@xxchan
Copy link
Contributor

xxchan commented Aug 4, 2023

Basically #5423 will remove all spaces after item markers (+, 1., etc.), so I cannot tell it's a bug or feature, unlike #5835 which is definitely a bug.

#5836 is somewhat similar. i.e., the style of how to format a markdown list is controversial..

@ytmimi
Copy link
Contributor

ytmimi commented Aug 25, 2023

But you used comment // instead of doc comment ///. Not sure whether it's reasonable to assume // is markdown 🤔️

I believe we're incorrectly calling code that goes down the doc comment rewrite path, even when we're rewriting a normal comment. #5536 should solve the issue highlighted in #5862 (comment)

On further reflection I don't think that's the case.

@ytmimi
Copy link
Contributor

ytmimi commented Aug 30, 2023

@xxchan Do you have any interest in workin on the? Part of me thinks that the solution might be as simple as wrapping this in a check for is_doc_comment so we don't try to parse out code blocks or lists if we're not in a doc comment.

rustfmt/src/comment.rs

Lines 806 to 813 in f89cd3c

if let Some(stripped) = line.strip_prefix("```") {
self.code_block_attr = Some(CodeBlockAttribute::new(stripped))
} else if self.fmt.config.wrap_comments() {
if let Some(ib) = ItemizedBlock::new(line) {
self.item_block = Some(ib);
return false;
}
}

rwestphal added a commit to holo-routing/holo that referenced this issue Sep 4, 2023
Add a CI pipeline to validate code formatting on Pull Requests.

Also, temporarily disable "wrap_comments = true" in rustfmt.toml until a
rustfmt regression [1] is fixed.

[1] rust-lang/rustfmt#5862

Signed-off-by: Renato Westphal <[email protected]>
rwestphal added a commit to holo-routing/holo that referenced this issue Sep 4, 2023
Add a CI pipeline to validate code formatting on Pull Requests.

Also, temporarily disable "wrap_comments = true" in rustfmt.toml until a
rustfmt regression [1] is fixed.

[1] rust-lang/rustfmt#5862

Signed-off-by: Renato Westphal <[email protected]>
rwestphal added a commit to holo-routing/holo that referenced this issue Jan 25, 2024
Add a CI pipeline to validate code formatting on Pull Requests.

Also, temporarily disable "wrap_comments = true" in rustfmt.toml until a
rustfmt regression [1] is fixed.

[1] rust-lang/rustfmt#5862

Signed-off-by: Renato Westphal <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-comments only-with-option requires a non-default option value to reproduce p-low
Projects
None yet
Development

No branches or pull requests

3 participants