From 97de9061e45f108051fccd1edfc1f166dec1b3d4 Mon Sep 17 00:00:00 2001 From: Vincent Guyader <10470699+VincentGuyader@users.noreply.github.com> Date: Sat, 25 Apr 2026 22:41:50 +0200 Subject: [PATCH] Use forward slashes when substituting bib path into Rmd template MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit create_biblio_file() interpolated tempdir() into the bibliography Rmd template as-is. On Windows, tempdir() returns a path with backslashes (e.g. "C:\Users\RUNNER~1\AppData\..."). Once that path landed in the Rmd, the chunk re-evaluating it tripped over substrings like \U, \R, \T, which R parses as Unicode escapes — yielding `Error in file(): cannot open the connection` from xfun::pkg_bib at vignette build time. Convert backslashes to forward slashes before substitution. R accepts forward-slash paths on Windows, so the resulting string is a valid file path on every OS. (There was already a commented-out attempt at this same fix in the source: `# escape \`.) Add a unit test that asserts the substitution result parses cleanly and contains no backslash, plus a smoke test that the html artifact is produced. --- R/bibliography.R | 5 +++- tests/testthat/test-bibliography.R | 40 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/test-bibliography.R diff --git a/R/bibliography.R b/R/bibliography.R index 0dead86..001e105 100644 --- a/R/bibliography.R +++ b/R/bibliography.R @@ -66,9 +66,12 @@ create_biblio_file <- function(packages, out.dir, x = bib_lines[grep("PACKAGES_HERE", bib_lines)]) # path to bib + # Use forward slashes: on Windows, tempdir() returns "C:\Users\...", + # and a substring like "\U..." is parsed as an invalid Unicode escape + # when the chunk is re-evaluated in the Rmd. bib_lines[grep("BIBPATH_HERE", bib_lines)] <- gsub( pattern = "BIBPATH_HERE", - replacement = paste0('"', dir_temp, '"'), + replacement = paste0('"', gsub("\\\\", "/", dir_temp), '"'), x = bib_lines[grep("BIBPATH_HERE", bib_lines)]) # Link citation diff --git a/tests/testthat/test-bibliography.R b/tests/testthat/test-bibliography.R new file mode 100644 index 0000000..ec52408 --- /dev/null +++ b/tests/testthat/test-bibliography.R @@ -0,0 +1,40 @@ +test_that("create_biblio_file substitutes BIBPATH with forward slashes", { + # On Windows, tempdir() returns a path with backslashes (e.g. "C:\Users\..."). + # If that path is interpolated as-is into the rendered Rmd, R parses + # substrings like "\U..." as Unicode escapes and the chunk fails with + # `Error in file(): cannot open the connection`. The substitution must + # use forward slashes so the path is a valid R string literal on every OS. + + fake_dir <- "C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\RtmpXXXX" + bib_template <- c( + "knitr::write_bib(packages, file.path(BIBPATH_HERE, 'packages.bib'))" + ) + substituted <- gsub( + pattern = "BIBPATH_HERE", + replacement = paste0('"', gsub("\\\\", "/", fake_dir), '"'), + x = bib_template + ) + + expect_false(grepl("\\\\", substituted)) + expect_true(grepl("C:/Users/RUNNER~1/AppData/Local/Temp/RtmpXXXX", substituted, fixed = TRUE)) + + expect_silent( + parse(text = substituted) + ) +}) + +test_that("create_biblio_file produces an html bibliography", { + out <- tempfile() + dir.create(out) + + suppressMessages( + create_biblio_file( + packages = c("rmarkdown", "knitr"), + out.dir = out, + to = "html", + edit = FALSE + ) + ) + + expect_true(file.exists(file.path(out, "bibliography.html"))) +})