Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lang: fix clobbered module names on IDL parsing (ready for review) #1343

Merged
merged 1 commit into from Feb 21, 2022

Conversation

gdoteof
Copy link
Contributor

@gdoteof gdoteof commented Jan 20, 2022

When parsing the crate to generate the IDL; the parser naively assumes that all files in the crate have unique names (sans the path).

I found this when I had something like e.g.:

.
├── instructions
│   ├── foo.rs
│   └── mod.rs
├── lib.rs
└── state
    ├── foo.rs
    └── mod.rs

The parser is maintaining a key-value store from module-names to parsed modules. The name is being generated from the filename and ignores the path; so multiple files with the same name in the same crate get over written with only the last one parsed remaining in the data structure.

This PR simply adds the path to the key; the key itself appears to be arbitrary and never consumed and is only to avoid duplication.

Here is an example repo which illustrates this problem:

https://github.com/gdoteof/anchor-idl-parse-bug/tree/main/programs/idl_parse_bug/src

This is the IDL generated after the changes in this pr:

export type IdlParseBug = {
  "version": "0.1.0",
  "name": "idl_parse_bug",
  "instructions": [
    {
      "name": "foo",
      "accounts": [
        {
          "name": "fooAccount",
          "isMut": true,
          "isSigner": true
        },
        {
          "name": "authority",
          "isMut": false,
          "isSigner": true
        },
        {
          "name": "systemProgram",
          "isMut": true,
          "isSigner": false
        }
      ],
      "args": []
    }
  ],
  "accounts": [
    {
      "name": "fooAccount",
      "type": {
        "kind": "struct",
        "fields": [
          {
            "name": "mine",
            "type": {
              "defined": "MyType"
            }
          }
        ]
      }
    }
  ],
  "types": [
    {
      "name": "MyType",
      "type": {
        "kind": "enum",
        "variants": [
          {
            "name": "Bar"
          },
          {
            "name": "Baz"
          }
        ]
      }
    }
  ]
};

export const IDL: IdlParseBug = {
  "version": "0.1.0",
  "name": "idl_parse_bug",
  "instructions": [
    {
      "name": "foo",
      "accounts": [
        {
          "name": "fooAccount",
          "isMut": true,
          "isSigner": true
        },
        {
          "name": "authority",
          "isMut": false,
          "isSigner": true
        },
        {
          "name": "systemProgram",
          "isMut": true,
          "isSigner": false
        }
      ],
      "args": []
    }
  ],
  "accounts": [
    {
      "name": "fooAccount",
      "type": {
        "kind": "struct",
        "fields": [
          {
            "name": "mine",
            "type": {
              "defined": "MyType"
            }
          }
        ]
      }
    }
  ],
  "types": [
    {
      "name": "MyType",
      "type": {
        "kind": "enum",
        "variants": [
          {
            "name": "Bar"
          },
          {
            "name": "Baz"
          }
        ]
      }
    }
  ]
};

@gdoteof gdoteof changed the title fix clobbered module names on IDL parsing lang: fix clobbered module names on IDL parsing Jan 21, 2022
@gdoteof gdoteof changed the title lang: fix clobbered module names on IDL parsing fix clobbered module names on IDL parsing Jan 21, 2022
@gdoteof gdoteof changed the title fix clobbered module names on IDL parsing lang: fix clobbered module names on IDL parsing Jan 21, 2022
@gdoteof gdoteof changed the title lang: fix clobbered module names on IDL parsing lang: fix clobbered module names on IDL parsing (ready for review) Jan 23, 2022
@armaniferrante armaniferrante merged commit 905d26b into coral-xyz:master Feb 21, 2022
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.

None yet

2 participants