Skip to content

fix(core): stackOutput derives lexicon from a directly-referenced Declarable (Ref(param)) (#1152) - #1164

Merged
lex00 merged 1 commit into
mainfrom
fix/1152-stackoutput-param-intrinsics
Jul 28, 2026
Merged

lex00 merged 1 commit into
mainfrom
fix/1152-stackoutput-param-intrinsics

Conversation

@lex00

@lex00 lex00 commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Fixes #1152.

What was actually wrong

The issue's original report claimed stackOutput(Ref(param), { exportName }) and stackOutput(Sub\${Ref(param)}`, ...) "throw the not-an-intrinsic guard error." That claim doesn't hold — the issue's own follow-up comment retracts it after a cleaner bisect (stackOutput(Ref(param))"works fine"; the real crash it found wasstackOutput(role.RoleName)`-style non-attribute property access, re-scoping the issue to two smaller, unrelated asks).

Reproduced from scratch to confirm which claim was right before touching anything: Ref(param) already carries RefIntrinsic's INTRINSIC_MARKER, so stackOutput()'s input-validation guard (isIntrinsic(ref)) already accepts it — no throw, confirmed empirically. But it isn't harmless either: it silently derives lexicon: "unknown". partitionByLexicon (packages/core/src/build.ts) buckets an "unknown"-lexicon StackOutput where no serializer ever looks, so the output silently vanishes from the template — no error, exit 0. That's the exact shape of the workaround described in the original report ("post-build template injection in the deploy script").

Root cause: stackOutput()'s anchor search (firstAttrRef) only ever looked for a nested AttrRef to borrow a wrapping intrinsic's lexicon from. Ref(param)/Ref(resource) wrap the referenced entity itself, not one of its attributes — and a CloudFormation Parameter carries no attributes at all (real CFN parameters have no Fn::GetAtt-returnable attributes), so Ref(param) can never contain an AttrRef for an AttrRef-only search to find, no matter how deep it looks.

The fix

packages/core/src/stack-output.ts: generalized the anchor search (renamed firstAttrRef → findAnchor) to recognize two shapes, not one — a nested AttrRef (borrow its parent's .lexicon, unchanged) or a directly-referenced Declarable (use its own .lexicon, new). The Declarable check uses isDeclarable (Symbol.for marker), which is already dual-copy-safe the same way #1137 made the AttrRef checks here dual-copy-safe.

This is a pure widening — every existing anchor-derivation path (bare AttrRef, AttrRef nested in an intrinsic, foreign-copy AttrRef via #1137) is unchanged and still covered by the pre-existing test suite (all 13 pre-existing stack-output.test.ts tests pass unmodified). Ref(resource) (as opposed to Ref(parameter)) already derived the correct lexicon before this fix too, just via an indirect route (recursing into the referenced resource's own AttrRef properties) — this fix reaches the same entity directly instead, with no change in the final derived value.

Why this is the right semantics (not a thrown error)

The task brief asked me to decide, from chant graph --stacks's consumer (computeStackGraph in build.ts), whether a parameter-valued output is legitimate or needs a loud rejection. A Parameter is a real, valid, lexicon-scoped entity — Ref(param) is exactly how CloudFormation asks for its resolved value at deploy time (the consuming stack receives whatever value the parameter takes). There's nothing invalid about the shape; the bug was purely in routing the output to the right lexicon partition once produced. Rejecting it loudly would be wrong — silently losing it was the actual defect, and deriving the correct lexicon fixes that outright rather than trading a silent failure for a noisy one.

Fold-path parity (#1091/#1123)

stackOutput is a registered foldable authoring helper (fold/foldable-helpers.ts) with requireLiveRefs already enforced at its call site in fold-import.ts. This fix lives entirely in stack-output.ts's own logic, which both the fold and run paths call identically — no fold-specific branching needed. Verified directly: lexicons/aws/examples/stack-outputs extended with a Parameter + stackOutput(Ref(parameter), { exportName }) output (the shape #1123 built this example for), and chant build --fold vs. plain run produce byte-identical output, both correctly including the new EnvironmentName output (missing before the fix, on both paths identically — confirming this was never fold-specific).

Added 3 new unit tests to stack-output.test.ts for the new Declarable-anchor path (bare, nested one level deeper via a Sub-style wrapper, and options.lexicon still overriding a Declarable-derived anchor) — 16/16 passing, all 13 pre-existing tests untouched and passing.

Note on the issue's re-scoped asks

The issue's follow-up comment re-scopes it to two different, smaller DX asks that this PR does not address (they're a separate concern from the anchor-derivation bug this PR fixes):

  1. Naming the offending output in stackOutput()'s guard-error message (today only the file is reported).
  2. Property access for a non-GetAtt property on a generated resource (e.g. role.RoleName) returning something outputtable instead of undefined.

I looked at (2) enough to flag a real hazard worth knowing about before anyone picks it up: the generated resource classes (createResource in packages/core/src/runtime.ts) are walked, JSON.stringify'd, and duck-typed (.then, .toJSON, etc.) pervasively across discovery/fold/serializers — a Proxy that throws on any unset property (the literal reading of "throw a named error at access time") would very likely break JSON.stringify itself (it always probes .toJSON defensively) and other generic duck-typing across the codebase. The safer half of the issue's own either/or (return the declared prop value when set, leave it undefined otherwise) avoids that blast radius, but is a distinct piece of work from this PR. Recommend tracking those two asks separately rather than folding them in here.

Gates

just build            npx tsc --noEmit -p packages/core/tsconfig.json   (clean)
just lint              npx eslint packages/                              (clean)
npm run build -w @intentius/chant-lexicon-aws                            (clean)
just test              776 files passed | 1 skipped (777)
                        9101 tests passed | 5 skipped (9106)
                        baseline (origin/main): 776/777 files, 9098/9106 tests — +3 tests, 0 files, from this PR's new unit tests
just check-lexicons     2 known, tracked tier-1 failures (github #1069) — unchanged from main; no untracked failures

just fold-differential — measured on origin/main first, then after:

before (origin/main)   corpus: 101/101   fold: 55   run-fallback: 46   drift: 0   isolated-retry: 0
after (this branch)    corpus: 101/101   fold: 55   run-fallback: 46   drift: 0   isolated-retry: 0

Identical — no corpus entry changed classification (stack-outputs was already fold and still is), and no count moved since this PR enriches an existing corpus entry rather than adding a new one.

just sandbox-differential — same before/after measurement:

before (origin/main)   corpus: 101/101   identical: 101   excluded: 0   drift: 0   --fold: 55   --sandbox: 55   demoted entries: 0   demoted files: 3
after (this branch)    corpus: 101/101   identical: 101   excluded: 0   drift: 0   --fold: 55   --sandbox: 55   demoted entries: 0   demoted files: 3

Identical, both runs.

Test plan

  • just build / just lint clean
  • npm run build -w @intentius/chant-lexicon-aws clean
  • just test — +3 tests, 0 regressions
  • just check-lexicons — no regression (2 known tracked failures, unchanged)
  • just fold-differential — zero drift, corpus counts unchanged from main
  • just sandbox-differential — zero drift, corpus counts unchanged from main
  • Manually reproduced the pre-fix silent-drop behavior on both fold and run paths before writing the fix, and confirmed fold/run stay byte-identical after it

…larable (Ref(param)) (#1152)

`stackOutput()`'s anchor search only ever looked for a nested AttrRef to
borrow a wrapping intrinsic's lexicon from. `Ref(param)`/`Ref(resource)`
wrap the referenced entity itself, not one of its attributes — and a
CloudFormation Parameter carries no attributes at all, so `Ref(param)`
could never contain an AttrRef for the old search to find. The anchor
silently came back empty, `lexicon` fell back to "unknown", and
`partitionByLexicon` bucketed the output where no serializer ever looks
— dropped from the template with no error, matching the post-build
template-injection workaround described in the issue.

`findAnchor` now also recognizes a directly-referenced Declarable (via
`isDeclarable`'s dual-copy-safe marker) and derives the lexicon from its
own `.lexicon` field. Confirmed the specific "throws" framing in the
issue's original report no longer applies (RefIntrinsic already carries
the Intrinsic marker, so the input-validation guard already accepted
it) — the real, still-live bug was this silent misrouting, not a thrown
error.

Extends lexicons/aws/examples/stack-outputs (built for this class of
case in #1123) with a Parameter + stackOutput(Ref(parameter)) output,
fold and run confirmed byte-identical.
@lex00
lex00 merged commit a25a6e3 into main Jul 28, 2026
5 checks passed
@lex00
lex00 deleted the fix/1152-stackoutput-param-intrinsics branch August 3, 2026 19:21
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.

stackOutput rejects lexicon-built intrinsics wrapping parameters (Ref(param), Sub with Ref(param))

1 participant