-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Distinguish between fenced and indented code blocks in error message #64162
Comments
The four leading chars start a markdown code block. The error is the same as having a .rs file whose only contents are a backslash. |
@estebank is this the expected behavior? I can't find it documented anywhere. |
It is. It is equivalent to writing
Which is the prevalent style in the echosystem, but you can also write it
like you did here. These code blocks get identified as rust code and get compiled and run as tests. |
I don't think it is clearly documented as such, but indented content is a code block per the markdown spec, and here it is documented that code blocks default to the Rust language, and are tested by default. |
If I replace my above example with: /// \
It compiles successfully, which seems to contradict the markdown specification @ehuss pointed to. Am I missing something more? |
I believe that is caused by the unindent pass which detects the indentation level and removes it. |
It does occasionally trip people up. I think there are a few things that could be done:
This is also mentioned in syntax reference. I'm not sure if repeating it in the docs will really help, since I doubt most people will thoroughly read the entire rustdoc manual (and remember everything). But I think it's up to the rustdoc team if they want more documentation, or changing the error message. |
I do think it would be interesting to extend parser errors emitted by rustdoc to have a note similar to "errors found while parsing code block in doc string". The interaction of indented code blocks and unindent is non-obvious and I would guess not entirely intended. |
I agree, it's been already a few times that people are having issues with this. |
Rustdoc auto-treating indented text as a Rust code block has been a massive pain point for us transitioning from C code with existing documentation. The default should be to treat all indented code blocks as if it were the
should be treated semantically identically to:
rather than trying to interpret the indents as Rust code. Is there data on how often rustdoc tests are written with indented blocks? My suspicion is next-to-never. |
@kupiakos If you remove the blank line, then it should not interpret it as a code block anymore:
according to "An indented code block is composed of one or more indented chunks separated by blank lines." |
@hkBst Tips on how to modify the text to avoid accidental parsing as code is not helpful. There's already a better workaround - surround everything with backticks. The issue is that the rustdoc default to treat all indented blocks as Rust code is unintuitive as evidenced by the number of similar issues. It requires cognizant modification to docs ported from C (or opting out of Rustdoc) in order to prevent breakage, rather than simply rendering poorly to be fixed later. I would be surprised if distinguishing indented code blocks from fenced code blocks actually broke anyone, especially if it were done on an edition boundary. |
I don't see why that would be better. The blank lines are essential to the interpretation as code blocks, so if they are not there, then there is no code block and no need to mark it as "
I also don't see how this breakage which can be easily fixed by removing erroneous/extraneous double newlines is so much worse than a poor rendering. But if that really is a problem, can't you get back to poor rendering by just removing all double newlines?
If you silently reinterpret code blocks that were previously treated as doc tests as mere text, then doctests would silently stop working. The only indication would be the sudden decrease in the number of such tests reported when running cargo test manually... How is that any good? |
@hkBst Because placing a
I asserted that few people intentionally write tests like this, not that we should ever make the change without checking. The right way to migrate users from current behavior to a newer default is to identify the impact and then inform them of the change. Something like:
|
EDIT: It turns out that the issue I was facing was caused by my doc comment being interpreted as a code block, based on the indented code block rule in markdown.
--
Summary
The following doc comment fails to compile using
cargo test --doc
:Example repository here.
Error Output
What I've Tried
I've found a few things (I'm sure this is not an exhaustive list to describe the problem):
///
Expected
The snippet compiles without issue.
Meta
I've verified the issue is present using
1.37.0
on bothx86_64-apple-darwin
andx86_64-unknown-linux-gnu
.The text was updated successfully, but these errors were encountered: