James-Yu / LaTeX-Workshop

Boost LaTeX typesetting efficiency with preview, compile, autocomplete, colorize, and more.
MIT License
10.74k stars 535 forks source link

bibtex/biber discrepancy in *.bib is not reported, but key lookup fails silently #4449

Closed v4hn closed 3 weeks ago

v4hn commented 4 weeks ago

Pre-checks

Environment

The Issue

Looking further into my local error behind #4445 (the broken links for \cite keys), I noticed a bibtex syntax error in line ~900 which is correct with biber:

% @misc{foobar,
@misc{test,
  author = {Test},
  title = {Test},
}

This is hopefully an uncommon problem, but it left me really confused for way too long. The problem is that LM uses latex-utensils's bibtex parser, whereas the rest of my (and any reasonably modern configuration) uses biblatex/biber. Comments are only supported in biber, so the bibtex parser finds the @ in the first line and chokes with a SyntaxError. No error is reported via Diagnostics, but the extension registers 0 entries from the bib file. The only indication something went wrong somewhere is a stack trace in the LaTeX Workshop logger:

SyntaxError: Expected "%", "}", [ \t\r\n], or [^%@={}()"#, \t\r\n] but "@" found.
    at new peg$SyntaxError (~/.vscode/extensions/james-yu.latex-workshop-10.5.5/node_modules/latex-utensils/out/src/bibtex/bibtex_parser_simple.js:14:20)
    at peg$buildStructuredError (~/.vscode/extensions/james-yu.latex-workshop-10.5.5/node_modules/latex-utensils/out/src/bibtex/bibtex_parser_simple.js:446:12)
    at Object.peg$parse [as parse] (~/.vscode/extensions/james-yu.latex-workshop-10.5.5/node_modules/latex-utensils/out/src/bibtex/bibtex_parser_simple.js:1982:11)
    at Object.parse (~/.vscode/extensions/james-yu.latex-workshop-10.5.5/node_modules/latex-utensils/out/src/bibtex/bibtex_parser.js:36:30)
    at Function.parseBibTeX (~/.vscode/extensions/james-yu.latex-workshop-10.5.5/out/src/parse/parser/unified.js:47:46)
    at MessagePort.<anonymous> (~/.vscode/extensions/james-yu.latex-workshop-10.5.5/node_modules/workerpool/src/worker.js:150:27)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:820:20)
    at MessagePort.<anonymous> (node:internal/per_context/messageport:23:28)

Notice that no location information is printed, although the SyntaxError exception provides a location field. I managed to patch in the information around here through

if (bibtexParser.isSyntaxError(err)) {
  return new Error('line ' + err.location.start.line + ' column ' + err.location.start.column + ': ' + err.message)
}

which helped a lot to address multiple such tiny issues and after all were resolved citation linking magically worked again. :cake:

A few hours of getting used to the code base were not enough to address this in a reasonable way, so I will have to leave it to upstream to address this properly. My main show stopper for a PR was that I tried to add diagnostic messages (with a few structural changes) by returning SyntaxError from here, but the non-Error data fields do not survive serialization because of the async await and location will be undefined.

Reproduction Steps

create this `literature.bib` ``` % @misc{test, @misc{test, author = {Test}, title = {Test}, } ```
and this `main.tex` ``` \documentclass{minimal} \usepackage{biblatex} \addbibresource{literature.bib} \begin{document} test~\cite{test} \end{document} ```
  1. CTRL + mouse over on the key of \cite{test} will not create the link in test and a SyntaxError will be logged in the extensions output.
  2. remove the @ behind % in the first line in in literature.bib
  3. now the link will work as expected

Expected Behavior

Sorry for the long issue description, it took quite some investigation.