Skip to content

feat: provide context around the language config (merged TOML) on load error #13755

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lmmx
Copy link

@lmmx lmmx commented Jun 12, 2025

I noticed while working with Helix recently (noted in #11666) that if I produced a bad configuration, it was hard to tell what I'd done wrong.

This is made doubly hard as the true source of the bug is a merged TOML (the default language config and the user's language config). The user's config may have many languages in, but if a language fails to parse into a Configuration (serde-derived struct) then the entire block will be given as the error source, so you would just see for example:

duplicate field `comment-tokens` in key "language"

I added a second step where, in the case of an error, this merged TOML value is retrieved, then converted to a string (thus allowing an error from parsing it to have a span, i.e. to know which byte and thus which line it occurred at, and thus to track down which language section it was in).

I then added a simple "go to the end of the section" and put a sensible limit of no more than 50 lines of context (for the example of the Rust language section it was 20 lines, and you need to read at least 8 lines to see the name = "rust line, as it's alphabetised when merging by the looks of it).

The resulting diagnostic would be very helpful when in a tricky situtation. In the case shown here, you'd be able to see that the error was misleading: the duplicate field is actually due to an alias. With only the language line though you could be really stumped trying to figure this one out.

Error at line 5872, column 1: TOML parse error at line 5872, column 1
     |
5872 | [[language]]
     | ^^^^^^^^^^^^
duplicate field `comment-tokens`

Context around error:
  >>> 5872: [[language]]
      5873: auto-format = true
      5874: comment-token = "//"
      5875: comment-tokens = ["//", "///", "//!"]
      5876: file-types = ["rs"]
      5877: injection-regex = "rs|rust"
      5878: language-servers = ["rust-analyzer"]
      5879: name = "rust"
      5880: persistent-diagnostic-sources = ["rustc", "clippy"]
      5881: roots = ["Cargo.toml", "Cargo.lock"]
      5882: scope = "source.rust"
      5883: shebangs = ["rust-script", "cargo"]
      5884: 
      5885: [language.auto-pairs]
      5886: '"' = '"'
      5887: "(" = ")"
      5888: "[" = "]"
      5889: "`" = "`"
      5890: "{" = "}"
      5891: 
Failed to parse language config: TOML parse error at line 5872, column 1
     |
5872 | [[language]]
     | ^^^^^^^^^^^^
duplicate field `comment-tokens`

Press <ENTER> to continue with default language config

Copy link

@icorbrey icorbrey left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants