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

feat: Add rustc style errors for manifest parsing #13172

Merged
merged 1 commit into from
Jan 11, 2024

Conversation

Muscraft
Copy link
Contributor

@Muscraft Muscraft commented Dec 14, 2023

#12235 is tracking user control over warnings. One part of that is making cargo's diagnostic system output messages in the style of rustc. To make it so that cargo doesn't have to manage a formatter and renderer, I decided to use annotate-snippets, which matches rustc's style well (at one time it was meant to be the formatted for rustc).

To get this work kicked off, it was suggested to me that we should start with styling manifest parsing errors, and that is what his PR does.

What manifest parsing errors look like after this change:
image


Note: cargo does not currently match rustc in color (#12740), rustc specifies their colors here and here. I used annotate-snippets default colors which match what rustc currently uses for colors. I did this as a stopgap while I work to unify the colors used between rustc and cargo.


Note: the error messages come directly from toml and can be quite long. It would be nice if we could put some of the message below the highlight but this would require changes to toml.
Example:

error: invalid type: integer `3`
 --> Cargo.toml:7:7
  |
7 | bar = 3
  |       ^ expected a version string like "0.9.8" or a detailed dependency like { version = "0.9.8" }
  |

@rustbot
Copy link
Collaborator

rustbot commented Dec 14, 2023

r? @ehuss

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added A-manifest Area: Cargo.toml issues S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 14, 2023
@Muscraft Muscraft force-pushed the diagnostic-system branch 3 times, most recently from 8a0c58a to 31b833d Compare December 14, 2023 23:16
src/cargo/util/toml/mod.rs Outdated Show resolved Hide resolved
Comment on lines 124 to 131
// Add back the new line that was stripped by `.lines()`
if line_count > line + 1 {
source.push('\n');
// If we are trying to highlight past the end of the file, add a
// placeholder character to the end of the line.
} else if span.end >= source.len() - 1 {
source.push('∅');
}
Copy link
Contributor

@epage epage Dec 15, 2023

Choose a reason for hiding this comment

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

  1. This should be in annotate-snippets, not cargo
  2. The '\n' should be "\\n" so it gets rendered
  3. These should be Dimmed to indicate they aren't literal text
  4. These should only show up when they are being highlighted

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm also not clear why the is being added, can you say more about the motivation there? I'm a little concerned about any potential confusion if users are not familiar with what that symbol means. I don't think rustc typically displays errors like that, and I think it is reasonably clear without a special character. For example:

error: this file contains an unclosed delimiter
 --> src/main.rs:4:10
  |
4 | fn x() {
  |        - ^
  |        |
  |        unclosed delimiter

or

error: expected one of `->`, `where`, or `{`, found `<eof>`
 --> src/main.rs:4:9
  |
4 | fn main()
  |         ^ expected one of `->`, `where`, or `{`

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm trying to find the conversation around invisible characters but it is a bit buried.

I think an aspect is that TOML is newline sensitive, unlike Rust, which elevates the importance of invisible characters and makes the chance of an error more frequent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed cargo's ability to render invisible characters, as it is something that should be discussed in another issue

src/cargo/util/toml/mod.rs Outdated Show resolved Hide resolved
Cargo.toml Outdated Show resolved Hide resolved
@weihanglo weihanglo added the A-diagnostics Area: Error and warning messages generated by Cargo itself. label Dec 15, 2023
@bors
Copy link
Collaborator

bors commented Dec 15, 2023

☔ The latest upstream changes (presumably #13166) made this pull request unmergeable. Please resolve the merge conflicts.

@ehuss
Copy link
Contributor

ehuss commented Dec 28, 2023

This PR probably isn't the best place to have a broader discussion, but I was wondering about what the longer-term higher-level design approach will be with this. Some questions I have:

  • Is the error-chain approach cargo currently uses incompatible with the annotated diagnostics? Is there some way the two can coexist?
  • Is it possible to have an annotated diagnostic encoded in a type that impls Error so that it can be returned like a normal error, and have cargo's top-level error handler (display_error) display the diagnostics correctly?
    • I'm a little concerned about what looks like a special-cased AlreadyPrintedError.
  • If there is an annotated structured Error type, is it possible to also have that work with JSON output (Provide Cargo messages as JSON messages #8283).
  • If it isn't possible to have an annotated Error type, would it make sense to have something like rustc's emit_err and keep track of whether or not errors have been encountered in a centralized place?

I'm happy to have a sync discussion sometime if that would be a better way to discuss the design ideas.

@epage
Copy link
Contributor

epage commented Dec 28, 2023

For myself, I see this PR as picking the smallest subsection of cargo to allow us to start introducing rustc-style diagnostics and most larger discussions (how to spread this pattern to other parts of the code, json output) would be handled in incremental steps on top. I'm willing to accept some level of hacks (AlreadyPrintedError) to help keep the scope of this experiment small.

The rough roadmap I see is

  1. Render TOML errors like rustc
  2. Try to turn all toml/mod.rs errors into rustc-style errors
  3. Create a new warning (implicit features) that leverages a new emit_diagnostic (intentionally unstable feature so its not shown to users while we work out the kinks)
  4. Identify shell().warn candidates to migrate to emit_diagnostic
  5. Stabilize [lints.cargo] (doesn't have to block on all warnings being turned into diagnostics but we'll likely want some)

At this point, depending on priorities, we can better evaluate

  • emit_err: after emit_diagnostic as that will need to handle deny and isn't as critical to normal operations. We'll need to discover how we want to handle "exit immediate" vs "fail on exit" (ie error recovery) in both cases.
  • json output as this isn't in the critical path towards the goal we're working towards but having more structured information will help!

tests/testsuite/bad_config.rs Show resolved Hide resolved
tests/testsuite/alt_registry.rs Outdated Show resolved Hide resolved
src/cargo/util/toml/mod.rs Outdated Show resolved Hide resolved
src/cargo/util/toml/mod.rs Outdated Show resolved Hide resolved
src/cargo/util/toml/mod.rs Outdated Show resolved Hide resolved
src/cargo/util/toml/mod.rs Outdated Show resolved Hide resolved
src/cargo/util/toml/mod.rs Outdated Show resolved Hide resolved
src/cargo/util/toml/mod.rs Outdated Show resolved Hide resolved
@ehuss
Copy link
Contributor

ehuss commented Jan 8, 2024

r? @epage

@rustbot rustbot assigned epage and unassigned ehuss Jan 8, 2024
@Muscraft Muscraft force-pushed the diagnostic-system branch 2 times, most recently from 0186fd2 to 0d62ae2 Compare January 10, 2024 22:39
@epage
Copy link
Contributor

epage commented Jan 10, 2024

@bors r+

@bors
Copy link
Collaborator

bors commented Jan 10, 2024

📌 Commit 0d62ae2 has been approved by epage

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 10, 2024
@bors
Copy link
Collaborator

bors commented Jan 10, 2024

⌛ Testing commit 0d62ae2 with merge 48601a3...

bors added a commit that referenced this pull request Jan 10, 2024
feat: Add `rustc` style errors for manifest parsing

#12235 is tracking user control over warnings. One part of that is making `cargo`'s diagnostic system output messages in the style of `rustc`. To make it so that `cargo` doesn't have to manage a formatter and renderer, I decided to use [`annotate-snippets`](https://crates.io/crates/annotate-snippets), which matches `rustc`'s style well (at one time it was meant to be the formatted for `rustc`).

To get this work kicked off, it was suggested to me that we should start with styling manifest parsing errors, and that is what his PR does.

What manifest parsing errors look like after this change:
![image](https://github.com/rust-lang/cargo/assets/23045215/0eb388b9-8d72-48ad-84a9-585160995078)

---

Note: `cargo` does not currently match `rustc` in color (#12740), `rustc`  specifies their colors [here](https://github.com/rust-lang/rust/blob/740cea81d6e34fc03c4495890e29f1c5ecb40b96/compiler/rustc_errors/src/lib.rs#L1755-L1768) and [here](https://github.com/rust-lang/rust/blob/740cea81d6e34fc03c4495890e29f1c5ecb40b96/compiler/rustc_errors/src/emitter.rs#L2689-L2723). I used `annotate-snippets` default colors which match what `rustc` currently uses for colors. I did this as a stopgap while I work to unify the colors used between `rustc` and `cargo`.

---

Note: the error messages come directly from `toml` and can be quite long. It would be nice if we could put some of the message below the highlight but this would require changes to `toml`.
Example:
```
error: invalid type: integer `3`
 --> Cargo.toml:7:7
  |
7 | bar = 3
  |       ^ expected a version string like "0.9.8" or a detailed dependency like { version = "0.9.8" }
  |
```
@epage epage mentioned this pull request Jan 10, 2024
5 tasks
@bors
Copy link
Collaborator

bors commented Jan 11, 2024

💥 Test timed out

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jan 11, 2024
@ehuss
Copy link
Contributor

ehuss commented Jan 11, 2024

@bors retry

macos never started

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 11, 2024
@bors
Copy link
Collaborator

bors commented Jan 11, 2024

⌛ Testing commit 0d62ae2 with merge 4eef543...

@bors
Copy link
Collaborator

bors commented Jan 11, 2024

☀️ Test successful - checks-actions
Approved by: epage
Pushing 4eef543 to master...

@bors bors merged commit 4eef543 into rust-lang:master Jan 11, 2024
22 checks passed
@Muscraft Muscraft deleted the diagnostic-system branch January 11, 2024 01:38
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 13, 2024
Update cargo

8 commits in 3e428a38a34e820a461d2cc082e726d3bda71bcb..84976cd699f4aea56cb3a90ce3eedeed9e20d5a5
2024-01-09 20:46:36 +0000 to 2024-01-12 15:55:43 +0000
- fix(resolver): do not panic when sorting empty summaries (rust-lang/cargo#13287)
- Implementation of shallow libgit2 fetches behind an unstable flag (rust-lang/cargo#13252)
- Add documentation entry for unstable `--output-format` flag (rust-lang/cargo#13284)
- doc: add `public` info in `cargo-add` man page. (rust-lang/cargo#13272)
- More docs on prerelease compat (rust-lang/cargo#13286)
- Add unstable `--output-format` option to  `cargo rustdoc` (rust-lang/cargo#12252)
- feat: Add `rustc` style errors for manifest parsing (rust-lang/cargo#13172)
- Document why `du` function uses mutex (rust-lang/cargo#13273)

r? ghost
@weihanglo weihanglo added this to the 1.77.0 milestone Jan 31, 2024
epage added a commit to epage/cargo that referenced this pull request Feb 5, 2024
The fix was backported to 1.77 (rust-lang#13393) which is when it got introduced
(rust-lang#13172).
bors added a commit that referenced this pull request Feb 5, 2024
docs(changelog): Slight cleanup

### What does this PR try to resolve?

For strip, I felt the nuance of where this change applies isn't as obvious and might give people the wrong impression, so I tried to make that front and center.

For the panic, the fix was backported to 1.77 (#13393) which is when it got introduced (#13172).

### How should we test and review this PR?

### Additional information
stupendoussuperpowers pushed a commit to stupendoussuperpowers/cargo that referenced this pull request Feb 28, 2024
The fix was backported to 1.77 (rust-lang#13393) which is when it got introduced
(rust-lang#13172).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Error and warning messages generated by Cargo itself. A-manifest Area: Cargo.toml issues S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants