Skip to content

Commit e1f98e2

Browse files
authored
fix(preview): restore --output-dir for single-file preview without _quarto.yml (#14491)
When `quarto preview file.qmd --output-dir <dir>` runs in a directory without _quarto.yml, preview pre-creates a single-file ProjectContext and passes it to render() as pContext. The synthetic-project trigger in render-shared only fired when context was null, bypassing it for the preview path and surfacing the project-only-flag check. Extend the trigger to also fire when pContext is a single-file context. Discarded single-file context cleans up via globalTempContext. Adds a unit test that exercises the exact preview pattern, and an llm-docs note describing the synthetic-project mechanism + history. Fixes #14489. Preview side of posit-dev/positron#13370.
1 parent cd29c5e commit e1f98e2

4 files changed

Lines changed: 229 additions & 4 deletions

File tree

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
---
2+
main_commit: e768e5c2d
3+
analyzed_date: 2026-05-06
4+
key_files:
5+
- src/command/render/render-shared.ts
6+
- src/project/project-context.ts
7+
- src/project/types/single-file/single-file.ts
8+
- src/project/types.ts
9+
- src/command/preview/preview.ts
10+
- src/command/preview/preview-shiny.ts
11+
---
12+
13+
# Synthetic Project Context (`--output-dir` without `_quarto.yml`)
14+
15+
How `quarto render foo.qmd --output-dir baz/` and `quarto preview foo.qmd
16+
--output-dir baz/` work in a directory without `_quarto.yml`.
17+
18+
## What it is
19+
20+
`projectContextForDirectory()` (in `src/project/project-context.ts`) wraps
21+
`projectContext(path, ctx, opts, /*force*/ true)`. When `force` is true,
22+
`projectContext` walks the directory and produces a `ProjectContext` even
23+
when no `_quarto.yml` is present — the resulting context has
24+
`config: { project: {} }`, `files.input` populated, and `isSingleFile: false`.
25+
26+
This is the **synthetic project** mechanism. It exists to allow the
27+
project-only flag `--output-dir` to function on free-floating files.
28+
29+
## Why two single-file mechanisms exist
30+
31+
`singleFileProjectContext()` (in `src/project/types/single-file/single-file.ts`)
32+
also produces a `ProjectContext` for a single file. They are not duplicates:
33+
34+
| Mechanism | Purpose | `config` | `files.input` | `isSingleFile` |
35+
|-----------|---------|----------|---------------|----------------|
36+
| `singleFileProjectContext` | Default fallback for single-file renders, extension support | `{ project: {} }` (1.9+) | `[]` | `true` |
37+
| `projectContextForDirectory` | Synthetic project for `--output-dir` without `_quarto.yml` | `{ project: {} }` (walked) | `[walked .qmd files]` | `false` |
38+
39+
The shapes diverged for historical reasons:
40+
41+
- `singleFileProjectContext` got `config` populated in 1.9 (Gordon, commit
42+
`1f94440b8`) so extensions can contribute metadata to single-file renders.
43+
This was for **extensions**, not `--output-dir`.
44+
- `projectContextForDirectory` predates that (1.4, JJ, commit `71b4df65f`)
45+
and is purpose-built for the `--output-dir` use case.
46+
47+
## When the synthetic project is used in `render-shared.ts`
48+
49+
`render()` chooses between mechanisms in this order:
50+
51+
```typescript
52+
let context = pContext || (await projectContext(path, nbContext, options));
53+
54+
// Promote to synthetic project when --output-dir is used and we don't
55+
// have a real project (or the caller pre-passed only a single-file context).
56+
if (options.flags?.outputDir && (!context || context.isSingleFile)) {
57+
context = await projectContextForDirectory(path, nbContext, options);
58+
options.forceClean = options.flags.clean !== false;
59+
}
60+
61+
// Otherwise, fall back to single-file context.
62+
if (!context) {
63+
context = await singleFileProjectContext(path, nbContext, options);
64+
}
65+
```
66+
67+
The `context.isSingleFile` arm covers the preview path (`preview.ts`
68+
pre-creates a `singleFileProjectContext` and passes it as `pContext`).
69+
Without the `isSingleFile` arm, the synthetic-project trigger is skipped
70+
and the path falls through to `validateDocumentRenderFlags`, which throws
71+
`The --output-dir flag can only be used when rendering projects.` —
72+
regression behind `quarto-dev/quarto-cli#14489`.
73+
74+
## `forceClean` and `.quarto` cleanup
75+
76+
When the synthetic project is created, `options.forceClean` is set:
77+
78+
```typescript
79+
options.forceClean = options.flags.clean !== false;
80+
```
81+
82+
This signals `project.ts` cleanup logic to remove the temporary `.quarto`
83+
scratch directory at the end of the render (#9745, #13625). On Windows,
84+
file handles must close before the directory is removed; the project
85+
context's `cleanup()` is called first.
86+
87+
For the preview path, `forceClean=true` is set on every re-render's
88+
`RenderOptions`. This means `.quarto` is recreated and cleaned each
89+
re-render. Sub-optimal for perf but correct behaviorally.
90+
91+
## `validateDocumentRenderFlags` — the original gate
92+
93+
```typescript
94+
function validateDocumentRenderFlags(flags?: RenderFlags) {
95+
if (flags) {
96+
const projectOnly = {
97+
"--output-dir": flags.outputDir,
98+
"--site-url": flags.siteUrl,
99+
};
100+
for (const arg of Object.keys(projectOnly)) {
101+
if (projectOnly[arg]) {
102+
throw new Error(
103+
`The ${arg} flag can only be used when rendering projects.`,
104+
);
105+
}
106+
}
107+
}
108+
}
109+
```
110+
111+
Originally `--output-dir` was strictly project-only and this was the
112+
gate. The synthetic-project mechanism is the deliberate back-door for
113+
`--output-dir`; `--site-url` has no back-door so the gate still applies.
114+
115+
## Key history
116+
117+
| Date | Commit | Change |
118+
|------|--------|--------|
119+
| 2023-11 (1.4) | `71b4df65f` | JJ: support `--output-dir` for single-file renders via synthetic project |
120+
| 2025-08 (1.9) | `1f94440b8` | Gordon: `singleFileProjectContext` gets `config = { project: {} }` for extensions (unrelated to `--output-dir`) |
121+
| 2025-09 (1.9) | `017349c6f` | Gordon: extensible engine architecture foundation |
122+
| 2025-12 (1.9) | `77633b39a` | Carlos: preview pre-creates project context — introduced the regression |
123+
| 2026-01 (1.9.37) | `c3384a5ff` | Christophe: restore synthetic-project creation for the `pContext === null` path (only partially fixed the regression) |
124+
| 2026-05 (1.9.x) | this PR | extend synthetic-project trigger to `pContext.isSingleFile` (closes the regression for preview) |
125+
126+
## Related test surface
127+
128+
- `tests/smoke/render/render-output-dir.test.ts` — covers the render
129+
path (`pContext` null + `--output-dir`).
130+
- `tests/unit/render-shared-output-dir.test.ts` — covers the preview
131+
pattern (`pContext = singleFileProjectContext`, `--output-dir`).
132+
- Manual fixtures: `tests/docs/manual/preview/` (matrix entries
133+
describing reproductions).
134+
135+
## Key files
136+
137+
| File | Purpose |
138+
|------|---------|
139+
| `src/command/render/render-shared.ts` | `render()` — chooses synthetic vs single-file vs full project |
140+
| `src/project/project-context.ts` | `projectContext(force=true)` walks dir → synthetic project; `projectContextForDirectory()` is the wrapper |
141+
| `src/project/types/single-file/single-file.ts` | `singleFileProjectContext()` — minimal context, `isSingleFile: true` |
142+
| `src/project/types.ts` | `ProjectContext.isSingleFile` discriminator |
143+
| `src/command/preview/preview.ts` | Pre-creates project context, passes as `pContext` to `render()` |
144+
| `src/command/preview/preview-shiny.ts` | Shiny preview path; also goes through `renderForPreview` so the same `render()` flow applies |

‎news/changelog-1.10.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All changes included in 1.10:
55
- ([#14267](https://github.com/quarto-dev/quarto-cli/issues/14267)): Fix Windows paths with accented characters (e.g., `C:\Users\Sébastien\`) breaking dart-sass compilation.
66
- ([#14281](https://github.com/quarto-dev/quarto-cli/issues/14281)): Fix transient `.quarto_ipynb` files accumulating during `quarto preview` with Jupyter engine.
77
- ([#14298](https://github.com/quarto-dev/quarto-cli/issues/14298)): Fix `quarto preview` browse URL including output filename (e.g., `hello.html`) for single-file documents, breaking Posit Workbench proxied server access.
8+
- ([#14489](https://github.com/quarto-dev/quarto-cli/issues/14489)): Restore `--output-dir` support for `quarto preview` of single files when no `_quarto.yml` is present (e.g. R-package workspaces). Regression introduced in v1.9.18.
89
- ([rstudio/rstudio#17333](https://github.com/rstudio/rstudio/issues/17333)): Fix `quarto inspect` on standalone files emitting project metadata that breaks RStudio's publishing wizard.
910

1011
## Accessibility

‎src/command/render/render-shared.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,15 @@ export async function render(
4949
// determine target context/files
5050
let context = pContext || (await projectContext(path, nbContext, options));
5151

52-
// Create a synthetic project when --output-dir is used without a project file
53-
// This creates a temporary .quarto directory to manage the render, which must
54-
// be fully cleaned up afterward to avoid leaving debris (see #9745)
55-
if (!context && options.flags?.outputDir) {
52+
// Create a synthetic project when --output-dir is used without a real project.
53+
// Triggers in two situations:
54+
// 1. No context yet (render path: no _quarto.yml found above)
55+
// 2. pContext is a single-file context (preview pre-creates one in
56+
// preview.ts before calling render(), which would otherwise bypass
57+
// synthetic-project creation — regression behind #14489)
58+
// The synthetic project provides a temporary .quarto directory so output-dir
59+
// handling works the same as for real projects (see #9745, #13625).
60+
if (options.flags?.outputDir && (!context || context.isSingleFile)) {
5661
context = await projectContextForDirectory(path, nbContext, options);
5762

5863
// forceClean signals this is a synthetic project that needs full cleanup
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* render-shared-output-dir.test.ts
3+
*
4+
* Regression test for posit-dev/positron#13370 / quarto-dev/quarto-cli#14489.
5+
*
6+
* When `quarto preview file.qmd --output-dir <dir>` runs in a directory
7+
* without _quarto.yml, preview pre-creates a singleFileProjectContext and
8+
* passes it to render() as pContext. The synthetic-project trigger in
9+
* render-shared.ts only fired when pContext was null, so the path fell
10+
* through to validateDocumentRenderFlags() and threw.
11+
*
12+
* This test mimics that exact pattern and asserts no throw + output
13+
* landed in --output-dir.
14+
*
15+
* Copyright (C) 2026 Posit Software, PBC
16+
*/
17+
18+
import { unitTest } from "../test.ts";
19+
import { assert } from "testing/asserts";
20+
import { join } from "../../src/deno_ral/path.ts";
21+
import { existsSync } from "../../src/deno_ral/fs.ts";
22+
import { render } from "../../src/command/render/render-shared.ts";
23+
import { singleFileProjectContext } from "../../src/project/types/single-file/single-file.ts";
24+
import { notebookContext } from "../../src/render/notebook/notebook-context.ts";
25+
import { renderServices } from "../../src/command/render/render-services.ts";
26+
import { initYamlIntelligenceResourcesFromFilesystem } from "../../src/core/schema/utils.ts";
27+
28+
unitTest(
29+
"render() with --output-dir succeeds when caller pre-passes a single-file context (#14489)",
30+
async () => {
31+
await initYamlIntelligenceResourcesFromFilesystem();
32+
33+
const tempBase = Deno.makeTempDirSync({ prefix: "quarto_test_outdir_" });
34+
const inputDir = join(tempBase, "src");
35+
const outputDir = join(tempBase, "out");
36+
Deno.mkdirSync(inputDir);
37+
Deno.mkdirSync(outputDir);
38+
39+
const inputFile = join(inputDir, "test.qmd");
40+
Deno.writeTextFileSync(
41+
inputFile,
42+
"---\ntitle: Test\nformat: html\n---\n\nHello world.\n",
43+
);
44+
45+
const nbCtx = notebookContext();
46+
const services = renderServices(nbCtx);
47+
48+
const renderOptions = {
49+
services,
50+
flags: { outputDir },
51+
pandocArgs: [],
52+
};
53+
54+
// Mimic preview's pattern: pre-create singleFileProjectContext, pass as pContext.
55+
const project = await singleFileProjectContext(inputFile, nbCtx, renderOptions);
56+
57+
try {
58+
// Before the fix this throws "The --output-dir flag can only be used
59+
// when rendering projects."
60+
await render(inputFile, renderOptions, project);
61+
62+
assert(
63+
existsSync(join(outputDir, "test.html")),
64+
`Expected output HTML at ${join(outputDir, "test.html")} after render with --output-dir`,
65+
);
66+
} finally {
67+
services.cleanup?.();
68+
try {
69+
Deno.removeSync(tempBase, { recursive: true });
70+
} catch {
71+
// best-effort cleanup
72+
}
73+
}
74+
},
75+
);

0 commit comments

Comments
 (0)