codama-idl / codama

Generate clients, CLIs, documentation and more from your Solana programs
MIT License
73 stars 16 forks source link

Name Conflict with Anchor V01 IDL Types #36

Closed kespinola closed 4 months ago

kespinola commented 4 months ago

Issue

Generated types have name conflicts with accounts.

Config

import path from "path";
import { fileURLToPath } from "url";
import {
  renderRustVisitor,
  renderJavaScriptUmiVisitor,
} from "@kinobi-so/renderers";
import { rootNodeFromAnchorWithoutDefaultVisitor } from "@kinobi-so/nodes-from-anchor";
import { readJson } from "@kinobi-so/renderers-core";
import { visit } from "@kinobi-so/visitors-core";
import fs from "fs/promises";

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const clientDir = path.join(__dirname, "clients");
const idlDir = path.join(__dirname, "target", "idl");

const idlFiles = await fs.readdir(idlDir);

for (const idlFile of idlFiles) {
  const idlPath = path.join(idlDir, idlFile);
  const idl = readJson(idlPath);

  const node = rootNodeFromAnchorWithoutDefaultVisitor(idl);

  const sdkName = idl.metadata.name;

  await visit(
    node,
    renderJavaScriptUmiVisitor(
      path.join(clientDir, "js", sdkName, "src", "generated")
    )
  );

  await visit(
    node,
    renderRustVisitor(
      path.join(clientDir, "rust", sdkName, "src", "generated"),
      { format: true }
    )
  );

Logs

src/generated/index.ts:14:1 - error TS2308: Module './accounts' has already exported a member named 'ApproveAccount'. Consider explicitly re-exporting to resolve the ambiguity.

14 export * from './types';
   ~~~~~~~~~~~~~~~~~~~~~~~~

src/generated/index.ts:14:1 - error TS2308: Module './accounts' has already exported a member named 'ApproveAccountArgs'. Consider explicitly re-exporting to resolve the ambiguity.

14 export * from './types';
   ~~~~~~~~~~~~~~~~~~~~~~~~

src/generated/index.ts:14:1 - error TS2308: Module './accounts' has already exported a member named 'Manager'. Consider explicitly re-exporting to resolve the ambiguity.

14 export * from './types';
   ~~~~~~~~~~~~~~~~~~~~~~~~

src/generated/index.ts:14:1 - error TS2308: Module './accounts' has already exported a member named 'ManagerArgs'. Consider explicitly re-exporting to resolve the ambiguity.

14 export * from './types';
   ~~~~~~~~~~~~~~~~~~~~~~~~

src/generated/index.ts:14:1 - error TS2308: Module './accounts' has already exported a member named 'TokenGroup'. Consider explicitly re-exporting to resolve the ambiguity.

14 export * from './types';
   ~~~~~~~~~~~~~~~~~~~~~~~~

src/generated/index.ts:14:1 - error TS2308: Module './accounts' has already exported a member named 'TokenGroupArgs'. Consider explicitly re-exporting to resolve the ambiguity.

14 export * from './types';
   ~~~~~~~~~~~~~~~~~~~~~~~~
  context: {
    __code: 1200008,
    formattedHistogram: 'errors: 4',
    validationItems: [
      {
        level: 'error',
        message: 'The "approveAccount" defined type exports a "ApproveAccount" type that conflicts with the "ApproveAccount" type exported by the "approveAccount" account.\n' +
          '|> Conflicting stack: [rootNode] > [programNode]wenNewStandard.',
        node: {
          kind: 'definedTypeNode',
          name: 'approveAccount',
          docs: [],
          type: { kind: 'structTypeNode', fields: [Array] }
        },
        stack: [
          {
            kind: 'rootNode',
            standard: 'kinobi',
            version: '0.20.2',
            program: [Object],
            additionalPrograms: []
          },
          {
            kind: 'programNode',
            name: 'wenNewStandard',
            publicKey: 'wns1gDLt8fgLcGhWi5MqAqgXpwEP1JftKE9eZnXS1HM',
            version: '0.3.2-alpha',
            origin: 'anchor',
            docs: [],
            accounts: [Array],
            instructions: [Array],
            definedTypes: [Array],
            pdas: [Array],
            errors: [Array]
          }
        ]
      },
kespinola commented 4 months ago

I believe the issues is here where the accountNode is referencing the type to create its representation since the type still remains it is also registered as a type with all the same functions and types minus the discriminator available on the account.

https://github.com/kinobi-so/kinobi/blob/main/packages/nodes-from-anchor/src/v01/AccountNode.ts#L18

What I'm going to do is purge the type before generating the definedTypes.

lorisleiva commented 4 months ago

Well spotted, thank you! 🙏