From 573fee6905c58cb77cb97d28679fc6678bb9e2a2 Mon Sep 17 00:00:00 2001 From: olivroy <52606734+olivroy@users.noreply.github.com> Date: Tue, 21 May 2024 17:58:14 -0400 Subject: [PATCH] Rethrow yaml parsing error (add a clickable link to _pkgdown.yml) (#2559) Co-authored-by: Hadley Wickham --- R/package.R | 14 +++++++++++--- tests/testthat/_snaps/package.md | 10 ++++++++++ tests/testthat/test-package.R | 13 +++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/R/package.R b/R/package.R index e02d3b8f0..8fab37bda 100644 --- a/R/package.R +++ b/R/package.R @@ -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 } diff --git a/tests/testthat/_snaps/package.md b/tests/testthat/_snaps/package.md index b29f034fd..08b1e5e6f 100644 --- a/tests/testthat/_snaps/package.md +++ b/tests/testthat/_snaps/package.md @@ -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 '/_pkgdown.yml'. + Caused by error in `yaml.load()`: + ! Scanner error: mapping values are not allowed in this context at line 2, column 8 + diff --git a/tests/testthat/test-package.R b/tests/testthat/test-package.R index 16288556a..a016207a9 100644 --- a/tests/testthat/test-package.R +++ b/tests/testthat/test-package.R @@ -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), "foo()") }) + +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, "", x, fixed = TRUE) + ) +})