Skip to content

bug(core): --fold never threads lexicon intrinsics through, so intrinsic tagged templates never fold in production #1039

Closed

Description

@lex00

Found by the #1038 docs audit. It is a code gap, not a docs one, and it invalidates a premise the fold epic (#1019) shipped on: intrinsic-heavy authoring (AWS Sub, the dominant real-world pattern) never folds under --fold today.

Background for a cold start

chant build --fold (#1022) statically folds each source file's AST instead of importing and running it, with per-file run-fallback for anything outside the fold subset. The folder itself lives in packages/core/src/fold/fold.ts. It CAN fold a registered intrinsic tagged template — fold.ts:254 checks intrinsics.some((i) => i.isTag === true && i.name === tagName) and reduces Sub`...`to its intrinsic node form.IntrinsicDefis declared inpackages/core/src/lexicon.ts:105 (isTag?: boolean`).

Gap 1 — the CLI fold path hardcodes intrinsics to []

The call chain is: packages/core/src/cli/commands/build.ts → build() in packages/core/src/build.ts:472 (discover(path, { fold: options?.fold })) → discover() in packages/core/src/discovery/index.ts:73 → tryFoldFile(file) at index.ts:98 → foldResource/fold in packages/core/src/discovery/fold-import.ts.

Nothing on that chain accepts or threads intrinsics. The terminal calls are hardcoded:

  • fold-import.ts:471 — fold(argNode, ctx.consts, [])
  • fold-import.ts:510 — foldResource(node, ctx.consts, [])
  • tryFoldFile(file) at fold-import.ts:577 takes only the file path.

The seam already exists at the top: cli/commands/build.ts has options.plugins (each plugin exposes intrinsics(): IntrinsicDef[]). A grep confirms intrinsics() is consumed nowhere in core src today outside docs codegen — only unit tests exercise the param, with hand-fabricated defs (fold.test.ts).

Gap 2 — isTag registration is wrong in at least two lexicons

isTag predates the epic (it drove the docs "Tag?" column) and was never audited for fold:

  • lexicons/aws/src/plugin.ts:57 registers Sub with no isTag — AWS Sub is authored as a tagged template and must have isTag: true. Even with Gap 1 fixed, AWS folds nothing until this is set.
  • lexicons/gitlab/src/plugin.ts:56 has isTag: true on reference(), which is invoked as a plain call, not a tagged template — the inverse error. (It conflates isTag with YAML !reference rendering.)
  • docker (plugin.ts:48) and github (plugin.ts:71) have explicit isTag: false; verify those are correct while auditing.

Audit all 11 lexicons: isTag: true iff the intrinsic is authored as a JS tagged template.

Empirically confirmed

chant build <dir> --fold on lexicon examples:

[fold:run] resource-basic.ts — "appBucket" not foldable: unregistered tagged template intrinsic: Sub`...`
[fold:run] main.ts — not foldable: function call as a value is not foldable: Concat(...)   (azure)

0 of ~20 AWS, 0 of ~9 Azure, 0 of ~8 GCP examples fold. The 14/98 corpus entries that fold are intrinsic-free.

Fix

  1. Add an intrinsics?: IntrinsicDef[] option through the chain: build() options → DiscoveryOptions → tryFoldFile(file, intrinsics) → replace both hardcoded [] in fold-import.ts. Populate it in cli/commands/build.ts from options.plugins.flatMap(p => p.intrinsics?.() ?? []) (guard: intrinsics may be optional on the plugin interface — check).
  2. Fix isTag per Gap 2 across all lexicons. If isTag is genuinely doing double duty for YAML tag rendering somewhere (gitlab), decouple: fold keys off "authored as a tagged template" only. Check how the serializer/docs codegen consume isTag before changing gitlab's value — if the YAML !reference rendering depends on it, introduce a separate flag rather than breaking rendering.
  3. Tests: a fold-import test where a resource using a registered tagged-template intrinsic folds end-to-end through tryFoldFile with real plugin defs (not fabricated ones); an assertion that an intrinsic-using AWS example moves from run-fallback to fold with zero drift versus the run path.

Scope guard

Azure-style intrinsics (Concat(...), Reference(...)) are plain calls, not tagged templates. They stay unfoldable after this fix. That is expected and out of scope — do not try to fold plain-call intrinsics here.

Acceptance

  • chant build --fold on an AWS example using Sub folds (logs [fold:fold], not [fold:run]), and folded output is identical to run output.
  • Both hardcoded [] in fold-import.ts are gone; intrinsics flow from options.plugins.
  • isTag audited across all 11 lexicons; AWS Sub is isTag: true; gitlab reference() no longer claims tagged-template status (without breaking its YAML rendering).
  • just build, just test, just lint green. Any touched lexicon also passes npm run build -w @intentius/chant-lexicon-<name> (lexicon tsc is CI-gated separately from core; aws is vitest-only).

Follow-up to #1019, surfaced by #1038.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions