|
3 | 3 | - "tests/unit/preview-subdir-knitr-cwd.test.ts" |
4 | 4 | --- |
5 | 5 |
|
6 | | -# `preview-subdir-knitr-cwd.test.ts` — forced renv activation |
| 6 | +# R unit test that changes working directory |
7 | 7 |
|
8 | | -The first test case (`preview of a knitr subdir doc from that subdir's cwd |
9 | | -renders (#14683)`) writes a `.Rprofile` into its scratch project directory |
10 | | -during `setup()`. If this test starts failing on CI with an R error like: |
| 8 | +This test runs a knitr subprocess with cwd set to a scratch directory |
| 9 | +(required to reproduce the bug). R's `.Rprofile` lookup is cwd-exact, so it |
| 10 | +won't pick up `tests/renv` activation there — on CI the R subprocess fails |
| 11 | +with `there is no package called 'rmarkdown'`. `setup()` writes a |
| 12 | +`.Rprofile` into the fixture dir to re-activate renv against the real |
| 13 | +`tests/` project. |
11 | 14 |
|
12 | | -``` |
13 | | -Error in library(rmarkdown) : there is no package called 'rmarkdown' |
14 | | -``` |
15 | | - |
16 | | -read this before touching product code (`src/execute/rmd.ts`, |
17 | | -`src/execute/engine.ts`) — it is very likely this workaround, not a |
18 | | -regression in Quarto itself. |
19 | | - |
20 | | -## Why it's there |
21 | | - |
22 | | -The test reproduces quarto-dev/quarto-cli#14683, which only manifests when |
23 | | -the process's working directory is the *document's subdirectory* (not the |
24 | | -project root, not `tests/`). The test harness enforces this with |
25 | | -`TestContext.cwd(() => e2eLabsDir)`, which does a real `Deno.chdir()` into a |
26 | | -scratch temp dir outside the repo checkout for the duration of the test. |
27 | | - |
28 | | -R's own `.Rprofile` lookup at startup is **cwd-exact** — it checks only the |
29 | | -process's own working directory, with no parent-directory search. Normal |
30 | | -smoke-all knitr renders never `chdir` away from `tests/` (they compute a |
31 | | -path *relative to* the current cwd instead — see `smoke-all.test.ts`), so |
32 | | -their R subprocess spawns with cwd = `tests/`, finds `tests/.Rprofile`, and |
33 | | -activates the `tests/renv` project automatically. This test's cwd is a |
34 | | -scratch dir with no `.Rprofile` of its own, so that activation never |
35 | | -happens. |
36 | | - |
37 | | -On CI, rmarkdown/knitr are installed **only** inside `tests/renv`'s project |
38 | | -library (see `tests/renv.lock`, `tests/configure-test-env.ps1|sh`) — not in |
39 | | -a global/system R library. Without renv activation, R falls back to |
40 | | -`R_LIBS_USER`/the base library, rmarkdown/knitr aren't there, and the |
41 | | -knitr subprocess fails before ever reaching the #14683 code path. The |
42 | | -failure is fast (well under a second) and mostly silent: `renderProject` |
43 | | -swallows the underlying rejection into a bare `Error` with no message |
44 | | -(see `src/command/render/render-files.ts`'s catch block), so the test's own |
45 | | -`result.error` diagnostic won't show the real cause either — only the raw |
46 | | -Rscript stderr does. |
47 | | - |
48 | | -This never showed up locally because a typical dev machine (e.g. an R |
49 | | -install managed by `rig`) has rmarkdown/knitr in a library that's on |
50 | | -`.libPaths()` regardless of renv activation. |
51 | | - |
52 | | -## The fix |
53 | | - |
54 | | -`setup()` writes a `.Rprofile` into the scratch project dir (`e2eProjDir` — |
55 | | -the directory `callR` actually spawns Rscript in for a non-single-file |
56 | | -project) containing: |
57 | | - |
58 | | -```r |
59 | | -Sys.setenv(RENV_PROJECT = "<absolute path to tests/>") |
60 | | -source("<absolute path to tests/>/renv/activate.R") |
61 | | -``` |
62 | | - |
63 | | -`tests/renv/activate.R` reads `RENV_PROJECT` to determine the project root |
64 | | -if it's set, falling back to `getwd()` otherwise (see the top of that file). |
65 | | -Setting it explicitly makes renv activate against the *real* `tests/renv` |
66 | | -project and library, regardless of the fixture's own cwd. The absolute path |
67 | | -is derived from `import.meta.url` (this test file's own location), not an |
68 | | -env var, so it's self-contained. |
69 | | - |
70 | | -## If this breaks again |
71 | | - |
72 | | -- Confirm the theory by reading the raw Rscript stderr in the CI log for |
73 | | - this test (search the job log for `preview-subdir-knitr-cwd`) — a missing |
74 | | - package error confirms renv never activated. |
75 | | -- Check whether `tests/renv/activate.R`'s `RENV_PROJECT` handling changed |
76 | | - (a renv version bump could alter this contract). |
77 | | -- Check whether `withinActiveRenv()` in `src/execute/rmd.ts` changed — it |
78 | | - decides the R subprocess's actual spawn cwd (`Deno.cwd()` vs |
79 | | - `projectDir`), which is where R looks for `.Rprofile` at startup. |
80 | | -- The second test in this file (`fileExecutionEngineAndTarget yields an |
81 | | - absolute target...`) is R-free and does not need this workaround — it |
82 | | - only exercises `fileExecutionEngineAndTarget`, never spawns Rscript. |
| 15 | +**Details:** `llm-docs/testing-patterns.md` → "R Tests That Change Working Directory" |
0 commit comments