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

Improve YAML error thrown when parsing in-chunk options #2226

Closed
cderv opened this issue Feb 10, 2023 · 2 comments · Fixed by #2294
Closed

Improve YAML error thrown when parsing in-chunk options #2226

cderv opened this issue Feb 10, 2023 · 2 comments · Fixed by #2294

Comments

@cderv
Copy link
Collaborator

cderv commented Feb 10, 2023

From discussion at quarto-dev/quarto-cli#4306 - @yihui I agree with your comment

---
title: "Example"
format: pdf
engine: knitr
editor: visual
---

```{r}
# install.packages(c("DBI", "RSQLite"))
db = DBI::dbConnect(RSQLite::SQLite(), ":memory:")
```


```{sql}
#| label: test multiqueries -5
#|  connection: db

select 1
```

Leading to this error

processing file: my-homework.qmd
Error in yaml::yaml.load(meta, handlers = list(expr = parse_only)) : 
  Scanner error: mapping values are not allowed in this context at line 2, column 12
Calls: .main ... FUN -> parse_block -> partition_chunk -> <Anonymous>
Execution halted

which is not that helpful to know about the error.

This is not specific to Qmd and would be useful to Rmd using YAML syntax too

@cderv cderv moved this to To discuss / To plan in R Markdown Team Projects Feb 10, 2023
@yihui
Copy link
Owner

yihui commented Feb 10, 2023

Below is what I'd do and you can try to implement it:

First, we need to improve the error message from yaml.load():

meta = yaml::yaml.load(meta, handlers = list(expr = parse_only))

This could be done in tryCatch(..., error = function(e) ...). In the error handler, we parse the line/column numbers from the original message (you can access from e$message):

  Scanner error: mapping values are not allowed in this context at line 2, column 12

Then print the original YAML text and insert ^~~~~~ beneath the problematic line, with ^ pointing to the error column. Users should see an error message like this:

Failed to parse YAML: 
  Scanner error: mapping values are not allowed in this context

label: test multiqueries -5
 connection: db
           ^~~~~~~~~~

I think this can greatly improve the error message. If we want to do better, we can point out the line numbers of the code chunk. To do that, first we need to apply this lapply() to seq_along(groups) instead of groups, and we will get the index of the current chunk:

lapply(groups, function(g) {

With the index, we can emit a message similar to:

knitr/R/output.R

Lines 311 to 322 in 77970b0

res[i] = withCallingHandlers(
if (tangle) process_tangle(group) else process_group(group),
error = function(e) {
setwd(wd)
cat(res, sep = '\n', file = output %n% '')
message(
'Quitting from lines ', paste(current_lines(i), collapse = '-'),
' (', knit_concord$get('infile'), ') '
)
}
)
}

That means we need to factor out these lines of code so that it can be reused, i.e., we pass an index i to a function, and it will emit a message Quitting from lines ....

Let me know if this is all clear to you. Happy to chat next week.

@cderv cderv self-assigned this Feb 11, 2023
@cderv cderv moved this from To discuss / To plan to Next / Ready for Dev in R Markdown Team Projects Feb 28, 2023
@cderv cderv moved this from Next / Ready for Dev to Backlog in R Markdown Team Projects May 23, 2023
@yihui yihui linked a pull request Oct 24, 2023 that will close this issue
@github-project-automation github-project-automation bot moved this from Backlog to Done in R Markdown Team Projects Oct 24, 2023
@cderv cderv removed their assignment Oct 24, 2023
Copy link

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants