Cannot generate a contract instance when there are enums with the same name in the project

I wrote a smart contract in sway with the following dependencies:

[dependencies]
nft = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.8.1" }
ownership = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.8.1" }

Both of the libraries have an enum with the same name - AccessError:

After building the contract I get an ABI json file with both of these enums listed:

"types": [
    ...
    {
      "typeId": 3,
      "type": "enum AccessError",
      "components": [
        {
          "name": "OwnerDoesNotExist",
          "type": 0,
          "typeArguments": null
        },
        {
          "name": "SenderNotOwner",
          "type": 0,
          "typeArguments": null
        },
        {
          "name": "SenderNotOwnerOrApproved",
          "type": 0,
          "typeArguments": null
        }
      ],
      "typeParameters": null
    },
    {
      "typeId": 4,
      "type": "enum AccessError",
      "components": [
        {
          "name": "CannotReinitialized",
          "type": 0,
          "typeArguments": null
        },
        {
          "name": "NotOwner",
          "type": 0,
          "typeArguments": null
        }
      ],
      "typeParameters": null
    },
   ...

And when I try to use this ABI in my Rust code

abigen!(Contract(
    name = "ContractInstance",
    abi = "../contract/out/debug/contract-abi.json"
));

, I get the following error:

$ cargo run

   Compiling script v0.1.0 (./script)
error[E0428]: the name `AccessError` is defined multiple times
 --> src/model.rs:3:1
  |
3 | / abigen!(Contract(
4 | |     name = "ContractInstance",
5 | |     abi = "../contract/out/debug/contract-abi.json"
6 | | ));
  | |__^ `AccessError` redefined here
  |
  = note: `AccessError` must be defined only once in the type namespace of this module
  = note: this error originates in the macro `abigen` (in Nightly builds, run with -Z macro-backtrace for more info)

The problem is gone after renaming one of the AccesError occurrences manually in the ABI json file, but I think that this behavior should be fixed.

fuelup show
active toolchain
-----------------
beta-3-aarch64-apple-darwin (default)
  forc : 0.37.3
    - forc-client
      - forc-deploy : 0.37.3
      - forc-run : 0.37.3
    - forc-doc : 0.37.3
    - forc-explore : 0.28.1
    - forc-fmt : 0.37.3
    - forc-index : 0.11.2
    - forc-lsp : 0.37.3
    - forc-tx : 0.37.3
    - forc-wallet : 0.2.2
  fuel-core : 0.17.11
  fuel-indexer : 0.11.2
1 Like

Hey,
First of all thanks for including your toolchain info in your post that’s very helpful.
It looks like your issue can be solved by calling forc build --json-abi-with-callpaths inside your Sway project. If it still doesn’t work let us know in this thread.
This is documented here, and will be deprecated as soon as the forc tool uses this option by default, which should in the near future.

1 Like

Hey, thanks! That solved the problem in rust for me. However, I cannot use the same generated code with typescript now, because ts typegen generates export statements like this one:

export type nft::nft_core::errors::AccessErrorInput = Enum<{ OwnerDoesNotExist: [], SenderNotOwner: [], SenderNotOwnerOrApproved: [] }>;

and these statements are illegal in typescript (because of :: I think).

That’s not a blocker for me, I’ll generate the ABI without --json-abi-with-callpaths flag for typescript, but I think there should be a unified way for both SDKs