Skip to content

Commit

Permalink
Rethrow yaml parsing error (add a clickable link to _pkgdown.yml) (#2559
Browse files Browse the repository at this point in the history
)

Co-authored-by: Hadley Wickham <[email protected]>
  • Loading branch information
olivroy and hadley authored May 21, 2024
1 parent 6757fe4 commit 573fee6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
14 changes: 11 additions & 3 deletions R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,23 @@ pkgdown_config_path <- function(path) {
)
}

read_meta <- function(path) {
read_meta <- function(path, call = caller_env()) {
path <- pkgdown_config_path(path)

if (is.null(path)) {
yaml <- list()
} else {
yaml <- yaml::yaml.load_file(path) %||% list()
yaml <- withCallingHandlers(
yaml::yaml.load_file(path, error.label = NULL) %||% list(),
error = function(e) {
cli::cli_abort(
"Could not parse config file at {.path {path}}.",
call = call,
parent = e
)
}
)
}

yaml
}

Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/_snaps/package.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,13 @@
! template.bootstrap must be 3 or 5, not 1.
i Edit _pkgdown.yml to fix the problem.

# read_meta() errors gracefully if _pkgdown.yml failed to parse

Code
as_pkgdown(pkg$src_path)
Condition
Error in `as_pkgdown()`:
! Could not parse config file at '<src>/_pkgdown.yml'.
Caused by error in `yaml.load()`:
! Scanner error: mapping values are not allowed in this context at line 2, column 8

13 changes: 13 additions & 0 deletions tests/testthat/test-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,16 @@ test_that("titles don't get autolinked code", {
rd <- rd_text("\\title{\\code{foo()}}", fragment = FALSE)
expect_equal(extract_title(rd), "<code>foo()</code>")
})

test_that("read_meta() errors gracefully if _pkgdown.yml failed to parse", {
pkg <- local_pkgdown_site()
write_lines(path = path(pkg$src_path, "_pkgdown.yml"), c(
"url: https://pkgdown.r-lib.org",
" title: Build websites for R packages"
))
expect_snapshot(
as_pkgdown(pkg$src_path),
error = TRUE,
transform = function(x) gsub(pkg$src_path, "<src>", x, fixed = TRUE)
)
})

0 comments on commit 573fee6

Please sign in to comment.