Skip to content

[2.x] Resolve data field program ids and validate PDA and instruction links - #1289

Merged
lorisleiva merged 3 commits into
mainfrom
11-24-visitor_and_validator_follow_ups
Nov 24, 2026
Merged

lorisleiva merged 3 commits into
mainfrom
11-24-visitor_and_validator_follow_ups

Conversation

@lorisleiva

Copy link
Copy Markdown
Member

This PR ports three fixes from codama-rs to the JS visitors and validators. getResolvedInstructionInputsVisitor now depends on a PDA value's programId when it is a data field, and keeps the instruction account in the path of the optional-seed error unless the whole default was injected (codama-idl/codama-rs#199). getValidationItemsVisitor reports PDA, instruction and instruction account links that point to no node, and treats links naming a program outside the tree as external (codama-idl/codama-rs#196).

pdaLinkNode('vualt')                                                 // error: Pointing to a missing PDA named "vualt"
pdaLinkNode('ata', { program: programLinkNode('associatedToken') })  // skipped: external program

@lorisleiva

Copy link
Copy Markdown
Member Author

@trevor-cortex

@changeset-bot

changeset-bot Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 9760fe8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 17 packages
Name Type
@codama/visitors-core Minor
@codama/validators Minor
@codama/cli Patch
@codama/dynamic-codecs Patch
@codama/dynamic-parsers Patch
@codama/renderers-core Patch
@codama/upgrade Patch
@codama/visitors Minor
codama Minor
@codama/dynamic-address-resolution Patch
@codama/dynamic-client Patch
@codama/dynamic-instructions Patch
@codama/nodes-from-anchor Patch
@codama/errors Minor
@codama/fragments Minor
@codama/node-types Minor
@codama/nodes Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@trevor-cortex trevor-cortex left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

Ports three codama-rs fixes to the JS side:

  1. getResolvedInstructionInputsVisitor — getDependencies now pushes pdaValueNode.programId unconditionally (its type is AccountValueNode | DataValueNode, so the old accountValueNode-only guard silently dropped data-field program ids and the field could resolve after the PDA that reads it).
  2. getResolvedInstructionInputsVisitor — the CANNOT_USE_OPTIONAL_ACCOUNT_AS_PDA_SEED_VALUE path now keys off isNode(account.defaultValue, 'injectedValueNode') rather than reference equality, so a PDA whose seed was injected (and therefore rebuilt by ProvidedScope.resolveWithin) keeps [instruction, account, …] as its prefix.
  3. getValidationItemsVisitor — new visitPdaLink / visitInstructionLink / visitInstructionAccountLink checks, with the existing account / defined-type link checks folded into a shared getMissingLinkItems that skips links naming a program absent from linkables.

I traced the new checks through LinkableDictionary: getPath([programLinkNode]) works for the external-program probe (program name comes from the link itself, no ancestors needed), instructionAccountLinkNode without an instruction falls back to the enclosing instruction via findInstructionNodeFromPath, and [...path, node.instruction] is a legitimate path for the nested instruction link. The exhaustiveness of the injectedValueNode dichotomy also holds: resolveDefaultValue only ever yields a pdaValueNode from a pdaValueNode or an injection, never from a conditionalValueNode.

Things to be aware of

  • Behaviour change for consumers: accountLinkNode / definedTypeLinkNode pointing at a program outside the tree used to be reported and are now silent. That's the intended trade-off (documented in getMissingLinkItems's docblock — a typo in program is indistinguishable from an external program), and the changeset calls it out. Worth remembering that this also applies when validating a lone programNode: any cross-program link is now "external".
  • Validators goes minor, visitors-core patch. Since the core packages are a fixed group the whole group will bump minor anyway, so the split is cosmetic — just flagging in case you wanted both aligned.
  • One non-blocking note inline on the rebuilt-PDA error path: its tail nodes aren't tree nodes by identity. The new test deliberately only asserts kinds, so this is a conscious choice; see the comment for an alternative if path fidelity matters.
  • Pre-existing and untouched: an injectedValueNode with a fallback: pdaValueNode(...) that resolves via its fallback still reports [resolvedDefaultValue] even though that node is in the tree at […, account, injected, fallback]. Not in scope here, just noting it for whoever next touches this branch.

Tests cover the typo case, same-name-in-other-program, external program, instruction-account-without-instruction, and the "only report the missing instruction once" case. LGTM.

Comment on lines +192 to +194
const pdaPath = isNode(account.defaultValue, 'injectedValueNode')
? [resolvedDefaultValue]
: [...stack.getPath('instructionNode'), account, resolvedDefaultValue];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking. When a seed was injected, resolveWithin rebuilds the whole pdaValueNode through identityVisitor, so resolvedDefaultValue and the seed appended below are fresh objects — not the ones in the tree. The prefix is now right, but a consumer walking from account by identity (e.g. getNodePathUntilLastNode + child lookup) still won't find the tail.

If path fidelity matters, the original nodes are cheap to recover here since the offending seed is an accountValueNode and therefore untouched by resolution:

const original = account.defaultValue;
const originalSeed = (original.seeds ?? []).find(s => s.name === seed.name) ?? seed;
// path: [...stack.getPath('instructionNode'), account, original, originalSeed]

If codama-rs reports the rebuilt value and you'd rather keep parity, a one-liner in the comment saying the tail is the resolved value would save the next reader the same detour.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping parity with codama-rs, which reports the rebuilt value too, and added the one-liner to the comment.

@lorisleiva
lorisleiva merged commit f56fc28 into main Nov 24, 2026
5 checks passed
@lorisleiva
lorisleiva deleted the 11-24-visitor_and_validator_follow_ups branch November 24, 2026 23:47
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.

2 participants