Skip to content

Commit 7f84ed6

Browse files
authored
Correctly use same R version when calling quarto CLI from R (#250)
* move import where it is used * processx can return different error than the one from `error_on_status` * Set `QUARTO_R` for ealier versions of Quarto to overcome the R_HOME detection problem Fixed in 1.8 at quarto-dev/quarto-cli#12887 * missing test_path in tests * Add NEWS * bump version
1 parent 0b82db3 commit 7f84ed6

File tree

7 files changed

+43
-27
lines changed

7 files changed

+43
-27
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: quarto
22
Title: R Interface to 'Quarto' Markdown Publishing System
3-
Version: 1.4.4.9010
3+
Version: 1.4.4.9011
44
Authors@R: c(
55
person("JJ", "Allaire", , "[email protected]", role = "aut",
66
comment = c(ORCID = "0000-0003-0174-9868")),

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# quarto (development version)
22

3+
- Quarto CLI will now correctly use the same R version than the one used to run functions in this package (#204).
4+
35
- Add `quarto_available()` function to check if Quarto CLI is found (thanks, @hadley, #187).
46

57
- `quarto_render()` now correctly set `as_job` when not inside RStudio IDE and required **rstudioapi** functions are not available (#203).

R/quarto.R

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ quarto_run <- function(
128128
...,
129129
.call = rlang::caller_env()
130130
) {
131+
# This is required due to a bug in QUARTO CLI, fixed only in 1.8+
132+
# https://github.com/quarto-dev/quarto-cli/pull/12887
133+
custom_env <- NULL
134+
if (!quarto_available(min = "1.8.12")) {
135+
custom_env <- c("current", QUARTO_R = R.home("bin"))
136+
}
131137
res <- tryCatch(
132138
{
133139
processx::run(
@@ -136,31 +142,40 @@ quarto_run <- function(
136142
echo = echo,
137143
error_on_status = TRUE,
138144
echo_cmd = echo_cmd,
145+
env = custom_env,
139146
...
140147
)
141148
},
142149
error = function(e) {
143-
msg <- c(x = "Error running quarto cli.")
144-
# if there is an error message from quarto CLI, add it to the message
145-
if (e$stderr != "") {
146-
quarto_error_msg <- xfun::split_lines(e$stderr)
147-
names(quarto_error_msg) <- rep(" ", length(quarto_error_msg))
148-
msg <- c(
149-
msg,
150-
" " = paste0(rep("-", nchar(msg)), collapse = ""),
151-
quarto_error_msg
150+
if (!inherits(e, "system_command_status_error")) {
151+
cli::cli_abort(
152+
c("!" = "Error running quarto CLI from R."),
153+
call = .call,
154+
parent = e
152155
)
153-
}
156+
} else {
157+
msg <- c(x = "Error returned by quarto CLI.")
158+
# if there is an error message from quarto CLI, add it to the message
159+
if (e$stderr != "") {
160+
quarto_error_msg <- xfun::split_lines(e$stderr)
161+
names(quarto_error_msg) <- rep(" ", length(quarto_error_msg))
162+
msg <- c(
163+
msg,
164+
" " = paste0(rep("-", nchar(msg)), collapse = ""),
165+
quarto_error_msg
166+
)
167+
}
154168

155-
# if `--quiet` has been set, quarto CLI won't report any error (e$stderr will be empty)
156-
# So remind user to run without `--quiet` to see the full error message
157-
if (cli_arg_quiet() %in% args)
158-
msg <- c(
159-
msg,
160-
"i" = "Rerun with `quiet = FALSE` to see the full error message."
161-
)
169+
# if `--quiet` has been set, quarto CLI won't report any error (e$stderr will be empty)
170+
# So remind user to run without `--quiet` to see the full error message
171+
if (cli_arg_quiet() %in% args)
172+
msg <- c(
173+
msg,
174+
"i" = "Rerun with `quiet = FALSE` to see the full error message."
175+
)
162176

163-
cli::cli_abort(msg, call = .call, parent = e)
177+
cli::cli_abort(msg, call = .call, parent = e)
178+
}
164179
}
165180
)
166181
invisible(res)

R/render.R

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
#' `quarto.quiet` \R option or `QUARTO_R_QUIET` environment variable can be used to globally override a function call
4444
#' (This can be useful to debug tool that calls `quarto_*` functions directly).
4545
#'
46-
#' On Github Actions, it will always be `quiet=FALSE`.
46+
#' On Github Actions, it will always be `quiet = FALSE`.
4747
#' @param profile [Quarto project
4848
#' profile(s)](https://quarto.org/docs/projects/profiles.html) to use. Either
4949
#' a character vector of profile names or `NULL` to use the default profile.
@@ -57,8 +57,6 @@
5757
#' background jobs. Use the `quarto.render_as_job` \R option to control
5858
#' the default globally.
5959
#'
60-
#' @importFrom rmarkdown relative_to
61-
#'
6260
#' @examples
6361
#' \dontrun{
6462
#' # Render R Markdown

R/utils.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
#' @importFrom rmarkdown relative_to
12
relative_to_wd <- function(path) {
2-
relative_to(getwd(), path)
3+
rmarkdown::relative_to(getwd(), path)
34
}
45

56
#' @importFrom yaml write_yaml

tests/testthat/_snaps/quarto.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
quarto_run(c("rend", "--quiet"))
55
Condition
66
Error:
7-
x Error running quarto cli.
7+
x Error returned by quarto CLI.
88
i Rerun with `quiet = FALSE` to see the full error message.
99
Caused by error:
1010
! System command 'quarto' failed
@@ -15,8 +15,8 @@
1515
quarto_inspect()
1616
Condition
1717
Error in `quarto_inspect()`:
18-
x Error running quarto cli.
19-
-------------------------
18+
x Error returned by quarto CLI.
19+
-----------------------------
2020
ERROR: Book chapter 'intro.qmd' not found
2121
2222
Stack trace:

tests/testthat/test-render.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ test_that("An error is reported when Quarto is not installed", {
66

77
test_that("R Markdown documents can be rendered", {
88
skip_if_no_quarto()
9-
quarto_render("test.Rmd", quiet = TRUE)
9+
quarto_render(test_path("test.Rmd"), quiet = TRUE)
1010
expect_true(file.exists("test.html"))
1111
unlink("test.html")
1212
})

0 commit comments

Comments
 (0)