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

Rethrow yaml parsing error (add a clickable link to _pkgdown.yml) #2559

Merged
merged 13 commits into from
May 21, 2024
Merged
4 changes: 1 addition & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Imports:
purrr (>= 1.0.0),
ragg,
rlang (>= 1.1.0),
rmarkdown (>= 2.26.2),
rmarkdown (>= 2.27),
olivroy marked this conversation as resolved.
Show resolved Hide resolved
tibble,
whisker,
withr (>= 2.4.3),
Expand Down Expand Up @@ -72,5 +72,3 @@ Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
SystemRequirements: pandoc
Remotes:
rstudio/rmarkdown
31 changes: 27 additions & 4 deletions R/package.R
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,38 @@ pkgdown_config_path <- function(path) {
)
}

read_meta <- function(path) {
path <- pkgdown_config_path(path)
read_meta <- function(path, check_path = TRUE, call = caller_env()) {
if (check_path) {
# check_path = FALSE can be used to supply a direct path to
# read_meta instead of a pkgdown object.
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) %||% list(),
error = function(e) {
# Tweak the original message to put the location of the error at the end
# based on yaml 2.3.8 error message structure
# (<<path>>) Parser error: <<parsing error>>
yaml_err <- conditionMessage(e)
# extract parsing error from original error (i.e. remove the path)
parsing_error <- sub("[^\\)]+\\)", "", yaml_err)
# Extract path from original error
path_yaml <- regmatches(yaml_err, m = regexpr("\\(([^\\)]+)\\)", yaml_err))
path_yaml <- gsub("\\(([^\\)]+)\\)", "\\1", path_yaml)
# Rethrow cli-styled error!
cli::cli_abort(c(
"x" = "Could not parse the config file.",
"!" = parsing_error,
"i" = "Edit {.path {path_yaml}} to fix the problem."
),
call = call
)
})
}

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
read_meta(file, check_path = FALSE)
Condition
Error:
x Could not parse the config file.
! Parser error: while parsing a block mapping at line 1, column 1 did not find expected key at line 9, column 3
olivroy marked this conversation as resolved.
Show resolved Hide resolved
i Edit 'assets/bad-yaml/_pkgdown.yml' to fix the problem.

16 changes: 16 additions & 0 deletions tests/testthat/assets/bad-yaml/_pkgdown.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
url: https://pkgdown.r-lib.org

home:
title: Build websites for R packages

authors:
Jay Hesselberth:
href: https://hesselberthlab.org
Maëlle Salmon:
href: https://masalmon.eu
Hadley Wickham:
href: http://hadley.nz
Posit Software, PBC:
href: https://www.posit.co
html: >-
<img src='https://www.tidyverse.org/posit-logo.svg' alt='Posit' width='62' height='16' style="margin-bottom: 3px;" />
15 changes: 13 additions & 2 deletions tests/testthat/test-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ test_that("package_vignettes() sorts articles alphabetically by file name", {

test_that("override works correctly for as_pkgdown", {
pkgdown <- as_pkgdown(test_path("assets/articles-images"))
olivroy marked this conversation as resolved.
Show resolved Hide resolved

expected_list <- list(dev = "jpeg", fig.ext = "jpg", fig.width = 3, fig.asp = 1)
expect_equal(pkgdown$meta$figures, expected_list)

modified_pkgdown <- as_pkgdown(pkgdown, override = list(figures = list(dev = "png")))
expect_equal(modified_pkgdown$meta$figures$dev, "png")
})
Expand All @@ -82,3 +82,14 @@ 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", {
file <- test_path("assets", "bad-yaml", "_pkgdown.yml")
expect_snapshot(
error = TRUE,
read_meta(
file,
check_path = FALSE
)
)
})
Loading