helix icon indicating copy to clipboard operation
helix copied to clipboard

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

Open lmmx opened this issue 5 months ago • 0 comments

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

lmmx avatar Jun 12 '25 22:06 lmmx