Skip to content

Fix builders filling in data fields named like an account - #349

Merged
lorisleiva merged 2 commits into
mainfrom
v3-renamed-args
Nov 27, 2026
Merged

lorisleiva merged 2 commits into
mainfrom
v3-renamed-args

Conversation

@lorisleiva

@lorisleiva lorisleiva commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

This PR fixes three bugs that meet when an omitted data field is named like an account and filled in from a PDA. Asynchronous builders read a renamed input property that only the synchronous input declares, the arguments inherited the account of the same name so the default never ran, and data fields received the [address, bump] pair of the PDA rather than its address. It also applies the quality findings of the second audit: a shared scope type for generated-node imports, private helpers no longer named like exported siblings, and test helpers moved to the shared setup.

// Before
const originalArgs = { ...input };
args.vault = await findVaultPda({ programAddress });
// After
const { vault: _vault, ...originalArgs } = { ...input };
args.vault = (await findVaultPda({ programAddress }))[0];

The program address of the instructions fixture also changes, since Instructions111… contained an I, which is not a base58 character, and the new test is the first to derive a PDA from it at runtime.

This PR fixes three bugs that meet when an omitted data field is named like an account and filled in from a PDA. Asynchronous builders read a renamed input property that only the synchronous input declares, the arguments inherited the account of the same name so the default never ran, and data fields received the `[address, bump]` pair of the PDA rather than its address. It also applies the quality findings of the second audit: a shared scope type for generated-node imports, private helpers no longer named like exported siblings, and test helpers moved to the shared setup.
@lorisleiva

Copy link
Copy Markdown
Member Author

@trevor-cortex

@changeset-bot

changeset-bot Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 77d617b

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

This PR includes changesets to release 1 package
Name Type
@codama/renderers-js Major

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

Fixes the trio of bugs that surface when an omitted data field shares its name with an account and is filled in from a PDA:

  • getRenamedArgsMap now takes the builder scope (useAsync) and is computed per builder in instructionFunction.ts, so the async builder no longer maps vault: input.vaultArg from a property only the sync input declares. The page-level warning and the program plugin use the right scope too (useAsync: false for the warning, isAsync for the builder the plugin calls).
  • getArgumentsInitializationFragment destructures omitted args named like an account out of input (const { vault: _vault, ...originalArgs } = { ...input }) so args.vault === undefined actually holds and the default runs.
  • Linked PDA defaults on data fields are now indexed with [0].

Plus the audit clean-up: GeneratedNodeModuleScope shared type, getDefinedTypeNodesToExtract made private to the render-map visitor, hasInstructionData requiring customInstructionData, private helpers renamed away from their exported siblings, readonly resolved inputs, getCodamaError moved to test/_setup.ts, and a linkVault E2E fixture with a runtime test. The logic refactor in getInstructionFunctionInputFlags is behaviour-preserving as far as I can tell.

The shadowing approach is sound: renamed args are input args and shadowed args are omitted (non-input) args, so the two sets are disjoint by construction and the { ...input, vault: input.vaultArg } / { vault: _vault, ...originalArgs } paths can't collide. TS also treats _vault as used because of the rest sibling, so noUnusedLocals in the E2E tsconfig is happy.

Blocking

The [0] fix only covers the linked PDA branch. The inlined pdaNode branch a few lines above (await getProgramDerivedAddress({ ... })) still assigns the [address, bump] pair to data fields, so an omitted data field defaulting to an inline PDA keeps the exact bug this PR describes. See the inline comment.

Notes for reviewers

  • The E2E program address changed from Instructions1111… to 86ZVX2…. I assume that's because I isn't in the base58 alphabet and the new runtime test is the first thing to actually derive a PDA against it — worth a line in the PR description so it doesn't look like an unrelated churn.
  • The changeset is major; with the repo on the 2.0.0-rc train that matches the pre-mode process, even though the [0] change is arguably a bug fix (the old output couldn't have encoded).
  • CLAUDE.md still lists getRenamedArgsMap conceptually under instructionInputs.ts ("the only module that knows how the IDL stores them") — now that it lives in async.ts, the async.ts bullet could mention it. Non-blocking.

Comment on lines +239 to +241
// Account values accept the derived address with its bump, whilst data fields hold the address.
const pda = fragment`await ${pdaFunction}(${mergeFragments(pdaArgs, c => c.join(', '))})`;
return defaultFragment(input.kind === 'account' ? pda : fragment`(${pda})[0]`);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This only covers linked PDAs. The inlined pdaNode branch above (return defaultFragment(fragment\await ${getProgramDerivedAddress}({ ... })`), ~L210) still assigns the [address, bump]tuple wheninput.kind === 'data', so an omitted data field defaulting to an inline pdaValueNode({ pda: pdaNode(...) })` keeps the bug this PR fixes.

Suggest lifting the kind check into a small helper used by both branches, e.g.

const pdaAddress = (pda: Fragment) => (input.kind === 'account' ? pda : fragment`(${pda})[0]`);

and adding a unit test for the inline case alongside the linked one (it renders instruction arguments with linked PDA default values…).

Fixes inlined PDA defaults of data fields the same way as linked ones, with a test, and documents where arguments are renamed.
@lorisleiva

Copy link
Copy Markdown
Member Author

Inlined PDA defaults of data fields now hold the address too, through one pdaDefault helper shared by both branches, with a unit test. The async.ts bullet of the agent docs mentions getRenamedArgsMap, and the description explains the fixture address change.

@lorisleiva

Copy link
Copy Markdown
Member Author

@trevor-cortex

@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.

Re-review

The blocking point from the previous pass is resolved: instructionInputDefault.ts now wraps both the inlined getProgramDerivedAddress branch and the linked findXPda branch through a shared pdaDefault helper that indexes [0] for data fields and leaves account values untouched, and there's a dedicated unit test (it renders instruction arguments with inlined PDA default values as addresses) covering the inline case. The non-blocking notes are also addressed: CLAUDE.md / AGENTS.md mention getRenamedArgsMap under async.ts, and the PR description explains the base58 fixture address change.

Re-checked the rest of the diff against the current head — instructionFunction.ts, async.ts, programPlugin.ts, instructionPage.ts, the render-map visitor, the E2E fixture and runtime test are unchanged from my first pass and still look right. Nothing further from me.

Notes for reviewers

  • The new const pdaDefault = … sits directly in the case 'pdaValueNode': body without a block, alongside the existing let pdaProgramValue. Lint evidently doesn't enforce no-case-declarations here, so it's consistent with the surrounding code — just flagging in case anyone wonders.
  • The [0] change is shipped as major to fit the 2.0.0-rc pre-mode train; the generated output for affected data fields previously couldn't have type-checked, so no consumer should be relying on the old shape.

@lorisleiva
lorisleiva merged commit 0244cf4 into main Nov 27, 2026
6 checks passed
@lorisleiva
lorisleiva deleted the v3-renamed-args branch November 27, 2026 07:51
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