Fix Windows libPaths propagation via Rscript command line - #303
Fix Windows libPaths propagation via Rscript command line#303shikokuchuo wants to merge 1 commit into
Conversation
On Windows, R_LIBS set on the quarto.exe process is not inherited by the Rscript process that Quarto CLI spawns for the knitr engine, so session .libPaths() never reach rendered documents (quarto-dev#217). Pass the lib paths through the Rscript command line instead, via QUARTO_KNITR_RSCRIPT_ARGS (Quarto >= 1.7): a new inst/rmd-init.R becomes the Rscript entry point, restores .libPaths() from a hex-encoded trailing argument, then sources Quarto's rmd.R. Hex encoding keeps the payload comma-free (Quarto splits the env var on commas) and pure ASCII (immune to Windows quoting and non-ASCII path mangling). R_LIBS is still set on all platforms; older Quarto versions ignore the unknown env var, so behavior there is unchanged. The previously skipped test now runs on Windows.
cderv
left a comment
There was a problem hiding this comment.
wow thank you !
but on Windows the variable is not inherited across the second hop when quarto.exe (Deno) spawns Rscript. Environment variables are lost on that hop,
Is this is Deno or Quarto issue ? I want to understand why this happenned only in R CMD CHECK, and could it be a problem for other env var in that case ?
I really want to understand all this before merging so I'll spend some time on this.
|
This is an The only reference in R is a comment in base R's documentation for
It's a bit oblique, but 'accept on the command line' is exactly what we're doing here. |
I was also looking into this at the time. Something in how R is called during R CMD build on windows. I shared in #217 several r-source links where they set R_LIBS using env var and then they use And I believe this is where it could not be working...
but they don't use I would need to retry locally and verify if I can reproduce using baseR or if this is something only on CI. You had the problem different than #217 ? or you are trying to solve 217 ? |
|
I ran into #217 some months ago, which motivated the fix. I'm using this |
Problem
On Windows,
.libPaths()from the calling R session never reach the R process that executes knitr chunks, soquarto_render()fails to find packages in non-default libraries (e.g. R CMD build's temp library during vignette rebuilding). Fixes #217.Root cause
The process chain is
R session → quarto.exe → Rscript rmd.R.quarto_run()passesR_LIBSas an environment variable (via processx), which works on Unix, but on Windows the variable is not inherited across the second hop when quarto.exe (Deno) spawns Rscript. Environment variables are lost on that hop, but command-line arguments are passed reliably.Fix
Use
QUARTO_KNITR_RSCRIPT_ARGS(Quarto ≥ 1.7), which quarto.exe reads from its own environment (first hop works) and inserts into the Rscript command line:inst/rmd-init.Rbecomes the Rscript entry point. It restores.libPaths()from a hex-encoded trailing argument read viacommandArgs(trailingOnly = TRUE), thensource()s Quarto's realrmd.R(appended to the command line by Quarto CLI), so rendering proceeds exactly as before.,— and pure ASCII, immune to Windows quoting issues and non-ASCII path mangling.R_LIBSis still set on all platforms (unchanged Unix mechanism); both channels share the same opt-out (options(quarto.use_libpaths = FALSE)/libpaths = NULL).Note
Rscript -e '...' script.Rruns only the expression (the script never runs), which is why the init-script + trailing-args pattern is used rather than-einjection.Supersedes the
R_ENVIRON_USERapproach in #298, which relies on the same broken env-var channel.Tests
skip_on_os("windows")from"quarto sees same libpaths as main process"— it should now pass on Windows CI.tests/testthat/test-libpaths.R: hex round-trip (spaces, non-ASCII),QUARTO_KNITR_RSCRIPT_ARGSconstruction and user-value preservation, and an end-to-endRscript inst/rmd-init.R <hex> probe.Rrun..libPaths()output.