Skip to content

Commit d0c3e16

Browse files
committed
Force renv activation in the #14683 e2e fixture to fix CI
The diagnostic probe (previous commit) confirmed the CI failure: R's .Rprofile lookup is cwd-exact with no parent-directory search, and the regression's own repro shape requires the process cwd to be a scratch directory outside the repo (the doc's subdirectory). That cwd never finds tests/.Rprofile, so renv never activates, and on CI — where rmarkdown/knitr live only in tests/renv's project library — R fails with "there is no package called 'rmarkdown'" before ever reaching the #14683 code path. It never showed up locally because a typical dev R install has these packages in a library that's on .libPaths() regardless of renv activation. setup() now writes a .Rprofile into the fixture project directory that sets RENV_PROJECT and sources the real tests/renv/activate.R, so renv activates against the actual project/library regardless of the fixture's own cwd. Removes the now-unneeded diagnostic probe. Adds tests/unit/CLAUDE.md documenting the mechanism for future debugging, since the failure mode (fast, silent, error message swallowed by renderProject's error wrapping) is not obvious from the test output alone.
1 parent e119044 commit d0c3e16

2 files changed

Lines changed: 101 additions & 32 deletions

File tree

‎tests/unit/CLAUDE.md‎

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

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

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@
2929

3030
import { unitTest } from "../test.ts";
3131
import { assert } from "testing/asserts";
32-
import { dirname, isAbsolute, join } from "../../src/deno_ral/path.ts";
32+
import {
33+
dirname,
34+
fromFileUrl,
35+
isAbsolute,
36+
join,
37+
} from "../../src/deno_ral/path.ts";
3338
import { existsSync } from "../../src/deno_ral/fs.ts";
3439
import { which } from "../../src/core/path.ts";
35-
import { rBinaryPath } from "../../src/core/resources.ts";
3640
import { projectContext } from "../../src/project/project-context.ts";
3741
import { singleFileProjectContext } from "../../src/project/types/single-file/single-file.ts";
3842
import { notebookContext } from "../../src/render/notebook/notebook-context.ts";
@@ -67,36 +71,6 @@ unitTest(
6771
(await singleFileProjectContext(file, nbContext));
6872

6973
try {
70-
// Temporary diagnostic (#14683 CI investigation): prove whether R can
71-
// load rmarkdown/knitr from callR's actual spawn cwd (the project dir)
72-
// on CI before blaming the render pipeline for the missing output.
73-
{
74-
// Pass a script file (not inline -e code) — an -e argument containing
75-
// double quotes gets re-tokenized by cmd.exe when Rscript resolves to
76-
// a .bat shim (e.g. rig-managed installs), corrupting the call.
77-
// Production callR never uses -e for the same reason (rmd.R is a file).
78-
const rscript = await rBinaryPath("Rscript");
79-
const probeScript = join(e2eBase, "probe.R");
80-
Deno.writeTextFileSync(
81-
probeScript,
82-
'cat("LIBPATHS:", paste(.libPaths(), collapse="|"), "\\n")\n' +
83-
"library(rmarkdown)\nlibrary(knitr)\ncat(\"PKG_OK\\n\")\n",
84-
);
85-
const probe = new Deno.Command(rscript, {
86-
args: [probeScript],
87-
cwd: e2eProjDir,
88-
stdout: "piped",
89-
stderr: "piped",
90-
});
91-
const probeResult = await probe.output();
92-
assert(
93-
probeResult.success,
94-
`R probe failed (code ${probeResult.code}) rscript=${rscript} in cwd=${e2eProjDir}\n` +
95-
`STDOUT:\n${new TextDecoder().decode(probeResult.stdout)}\n` +
96-
`STDERR:\n${new TextDecoder().decode(probeResult.stderr)}`,
97-
);
98-
}
99-
10074
// Mimic preview cmd.ts: resolve the preview format first, using the
10175
// bare filename. This seeds the shared context's fileInformationCache.
10276
const services = renderServices(nbContext);
@@ -183,6 +157,22 @@ unitTest(
183157
// the original cwd afterward.
184158
cwd: () => e2eLabsDir,
185159
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.
168+
const testsDir = dirname(dirname(fromFileUrl(import.meta.url)))
169+
.replaceAll("\\", "/");
170+
Deno.writeTextFileSync(
171+
join(e2eProjDir, ".Rprofile"),
172+
`Sys.setenv(RENV_PROJECT = "${testsDir}")\n` +
173+
`source("${testsDir}/renv/activate.R")\n`,
174+
);
175+
186176
Deno.writeTextFileSync(
187177
join(e2eProjDir, "_quarto.yml"),
188178
'project:\n type: website\n\nwebsite:\n title: "testsite"\n',

0 commit comments

Comments
 (0)