Skip to content

Allow arrays and booleans for knitr cache-globals cell option - #14736

Merged
cderv merged 2 commits into
quarto-dev:mainfrom
cderv:claude/quarto-knitr-vars-triage-bnbbw2
Jul 28, 2026
Merged

Allow arrays and booleans for knitr cache-globals cell option#14736
cderv merged 2 commits into
quarto-dev:mainfrom
cderv:claude/quarto-knitr-vars-triage-bnbbw2

Conversation

@cderv

@cderv cderv commented Jul 28, 2026

Copy link
Copy Markdown
Member

Description

Fixes #14735

cache-globals was declared as a bare string in src/resources/schema/cell-cache.yml, while the analogous cache-vars uses maybeArrayOf: string. Since maybeArrayOf: string desugars to anyOf(string, arrayOf(string)) and plain string does not, a YAML array was rejected during cell metadata validation — which runs before the engine is invoked, so it fails even with no R installed:

ERROR: Validation of YAML cell metadata failed.
Field "cache-globals" has value
- var_1
  - var_2
The value must instead be a string.

The same declaration also rejected cache-globals: false, a value the option's own long description documents ("In addition, cache-globals: false means detecting all variables in a code chunk"). The schema therefore contradicted its own prose, and users hit a validation error that disagreed with the documentation.

Both forms are valid for knitr. cache_globals() in knitr/R/block.R takes a character vector directly, treats FALSE as "find all symbols", and otherwise auto-detects; knitr declares the option as list in opts_chunk_attr. Per knitr's NEWS.md, vectors are supported since 1.17 and booleans since 1.34, so this is safe for any knitr Quarto supports.

The fix widens the schema using the same anyOf + maybeArrayOf shape that dependson uses further down the same file:

- name: cache-globals
  schema:
    anyOf:
      - maybeArrayOf: string
      - boolean

Editor tooling artifacts are regenerated via dev-call build-artifacts.

Test

tests/unit/schema-validation/cell-options-knitr-cache-globals.test.ts covers the array, boolean and single-string forms through parseAndValidateCellOptions, the same function reached during render from validate-document.ts via partitionCellOptionsMapped. It needs no R toolchain, since the defect was in TypeScript-side validation that runs before any engine. Verified to fail against the pre-fix generated artifact and to pass after (3 passed, ~0.4s).

It initializes via setInitializer(initYamlIntelligenceResourcesFromFilesystem) rather than fullInit(). fullInit() also runs ensureSchemaResources(), which overlays the source schema yml on top of the generated artifact, so a test using it would pass even when dev-call build-artifacts had not been re-run.

Also verified manually against knitr 1.45 and 1.51, though not kept as a test: the array reaches knitr as a character vector and false as logical FALSE, and with cache: true the cache database records the manually supplied globals for autodep.

smoke/yaml-intelligence/yaml-intelligence-code-cell-options.test.ts still passes (6 passed).

Checklist

I have (if applicable):

  • referenced the GitHub issue this PR closes
  • updated the appropriate changelog in the PR
  • ensured the present test suite passes
  • added new tests
  • created a separate documentation PR in Quarto's website repo and linked it to this PR

The option description in cell-cache.yml already documented the boolean form, so no wording change was needed — the generated docs were correct prose sitting on a schema that disagreed with them.

AI-assisted PR
  • AI tool used: Claude Code
  • Codebase grounding: local clone of quarto-cli; local clone of yihui/knitr for R/block.R, R/defaults.R and NEWS.md
  • Human review: I have reviewed, tested, and verified the AI-generated content before submitting.

cderv and others added 2 commits July 28, 2026 10:30
cache-globals was declared as a bare `string`, while the analogous cache-vars
uses `maybeArrayOf: string`. Since `maybeArrayOf: string` desugars to
`anyOf(string, arrayOf(string))` and plain `string` does not, a YAML array
failed cell metadata validation -- which runs before the engine, so it failed
even with no R installed.

The same declaration rejected `cache-globals: false`, a value the option's own
long description documents, so the schema contradicted its own prose.

knitr's cache_globals() takes a character vector directly, treats FALSE as
"find all symbols", and otherwise auto-detects; it declares the option as a
list type in opts_chunk_attr. Per knitr's NEWS.md, vectors are supported since
1.17 and booleans since 1.34.

Widen the schema to `anyOf(maybeArrayOf: string, boolean)`, matching dependson
in the same file, and regenerate the editor tooling artifacts.

Fixes quarto-dev#14735

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Covers the array, boolean and single-string forms through
parseAndValidateCellOptions, the function render itself calls. Runs without an
R toolchain, since the defect was in TypeScript-side schema validation that
happens before any engine runs.

Initializes via setInitializer(initYamlIntelligenceResourcesFromFilesystem)
rather than fullInit(): fullInit() also runs ensureSchemaResources(), which
overlays the source schema yml on top of the generated artifact, so a test
using it would pass even when dev-call build-artifacts had not been re-run.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@posit-snyk-bot

posit-snyk-bot commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@cderv
cderv merged commit 8b287b6 into quarto-dev:main Jul 28, 2026
51 checks passed
@cderv
cderv deleted the claude/quarto-knitr-vars-triage-bnbbw2 branch July 28, 2026 12:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

YAML arrays are rejected for cache-globals but accepted for cache-vars

2 participants