|
| 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 | |
0 commit comments