Skip to content

Commit 8325ac0

Browse files
committed
Split the R/renv-activation gotcha across rules, llm-docs, and code comment
Per repo convention, .claude/rules/testing/ files are terse pointers and llm-docs/testing-patterns.md holds the actual explanation — llm-docs explicitly excludes issue-specific notes. Move the detailed mechanism there as a generalized "R Tests That Change Working Directory" entry (no issue number, survives independently of #14683), trim the rule file back to a short pointer, and trim the test's own setup() comment to a one-line pointer plus what's specific to this fixture.
1 parent 0eb7093 commit 8325ac0

3 files changed

Lines changed: 41 additions & 83 deletions

File tree

‎.claude/rules/testing/unit-test-with-r.md‎

Lines changed: 8 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -3,80 +3,13 @@ paths:
33
- "tests/unit/preview-subdir-knitr-cwd.test.ts"
44
---
55

6-
# `preview-subdir-knitr-cwd.test.ts` — forced renv activation
6+
# R unit test that changes working directory
77

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.
1114

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"

‎llm-docs/testing-patterns.md‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,36 @@ Rscript -e "renv::install(); renv::snapshot()"
397397

398398
**Note:** While Quarto supports local Project.toml files in document directories for production use, the quarto-cli test infrastructure specifically does NOT support this pattern. All test dependencies must be in the main `tests/` environment.
399399

400+
### R Tests That Change Working Directory
401+
402+
R resolves `.Rprofile` from the **exact** process cwd (no parent-directory
403+
search). On CI, rmarkdown/knitr live only in `tests/renv`'s project library,
404+
activated when cwd is `tests/` (via `tests/.Rprofile` sourcing
405+
`renv/activate.R`). Most knitr tests never leave `tests/` — they pass paths
406+
relative to the current cwd instead of changing directories — so activation
407+
happens automatically.
408+
409+
A test that must run with cwd set elsewhere (a scratch temp dir, via
410+
`TestContext.cwd()` — see "Working-Directory-Sensitive Tests" above) loses
411+
that activation: the R subprocess starts outside `tests/`, renv never
412+
activates, and package loads fail with `there is no package called
413+
'rmarkdown'`. This is CI-only — a developer machine with rmarkdown on the
414+
default `.libPaths()` masks it entirely. The render pipeline also tends to
415+
swallow the underlying subprocess error, so the failure can be silent beyond
416+
the bare package-load message.
417+
418+
**Fix:** in the fixture's cwd, write a `.Rprofile` that re-points renv at the
419+
real project, regardless of where the test's cwd actually is:
420+
421+
```r
422+
Sys.setenv(RENV_PROJECT = "<absolute path to tests/>")
423+
source("<absolute path to tests/>/renv/activate.R")
424+
```
425+
426+
`renv/activate.R` reads `RENV_PROJECT` to determine the project root if set,
427+
falling back to `getwd()` otherwise — setting it explicitly decouples renv
428+
activation from the test's cwd.
429+
400430
## Best Practices
401431

402432
1. **Always clean up**: Use teardown to remove generated files

‎tests/unit/preview-subdir-knitr-cwd.test.ts‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,9 @@ unitTest(
157157
// the original cwd afterward.
158158
cwd: () => e2eLabsDir,
159159
setup: () => {
160-
// Force renv activation for the fixture project, regardless of cwd.
161-
// R's own .Rprofile lookup is cwd-exact (no parent-directory search),
162-
// and this fixture's cwd (e2eLabsDir, required to reproduce #14683) is
163-
// outside tests/, so it would never find tests/.Rprofile on its own.
164-
// Without this, CI environments where rmarkdown/knitr are installed
165-
// only in tests/renv's project library (not a global R library) fail
166-
// with "there is no package called 'rmarkdown'" before ever reaching
167-
// the #14683 code path. See tests/unit/CLAUDE.md for the full story.
160+
// Re-activate renv against the real tests/ project, regardless of
161+
// this fixture's cwd — see llm-docs/testing-patterns.md → "R Tests
162+
// That Change Working Directory" for why this is needed.
168163
const testsDir = dirname(dirname(fromFileUrl(import.meta.url)))
169164
.replaceAll("\\", "/");
170165
Deno.writeTextFileSync(

0 commit comments

Comments
 (0)