# Motoko - Multiple Canister Imports - Compile Error

**URL:** <https://forum.dfinity.org/t/motoko-multiple-canister-imports-compile-error/10491>\
**Category:** Language Support\
**Tags:** Motoko\
**Created:** [January 26, 2022, 3:12am UTC](https://forum.dfinity.org/t/motoko-multiple-canister-imports-compile-error/10491 "2022-01-26T03:12:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Motokoder](https://avatars.discourse-cdn.com/v4/letter/m/e47c2d/32.png) [@Motokoder](https://forum.dfinity.org/u/Motokoder)\
**Post date:** [January 26, 2022, 3:12am UTC](https://forum.dfinity.org/t/motoko-multiple-canister-imports-compile-error/10491/1 "2022-01-26T03:12:14Z")

</div>

I am running into an issue with the Motoko compiler (dfx 0.8.4) when importing two canisters and calling functions on each.

Canister A imports canisters B and C.

```auto
import B "canister:B”;
import C "canister:C”;

```

When calling a function of B, the compiler incorrectly expects a different return type that only exists in a function of C.

When inspecting B.did and C.did, the return type of the function I am calling in B has the same autogenerated type name (Result\_2) as the return type in C that the compiler is expecting.

B.did

```auto
type Result_2 = 
 variant {
   err: text;
   ok: text;
 };

```

C.did

```auto
type Result_2 = 
 variant {
   err: text;
   ok: opt vec SomeType;
 };

```

Error:

```auto
Stderr:
/src/Post/main.mo:196.59-196.146: type error [M0096], expression of type
  Result_2 = {#err : Text; #ok : ?[SomeType]}
cannot produce expected type
  {#err : Text; #ok : Text}

```

So, with multiple canister imports, it appears that the compiler is expecting the incorrect return types of functions when they have the same autogenerate type names in the did files.

I should also mention that I started using Text as the error type because I ran into the same issue with multiple variant types for defining errors.

This is currently a roadblock and the only solution I know of is to remove one of the canister imports. If anyone knows of another solution, please advise me. Thank you!

---

<div class="post-metadata">

**Author:** ![Motokoder](https://avatars.discourse-cdn.com/v4/letter/m/e47c2d/32.png) [@Motokoder](https://forum.dfinity.org/u/Motokoder)\
**Post date:** [January 26, 2022, 3:21am UTC](https://forum.dfinity.org/t/motoko-multiple-canister-imports-compile-error/10491/2 "2022-01-26T03:21:47Z")

</div>

After writing this, I realized that the autogenerated type name is causing the collision with the compiler, so I tried creating a custom type for the function return types to remove the naming collision, and that worked!

Example:

```auto
type MyCustomResult = Result.Result<Text, Text>;

public func SomeFunction() : async MyCustomResult {...}

```

The did file now uses the custom type name instead of the autogenerated name:

```auto
type MyCustomResult = 
 variant {
   err: text;
   ok: text;
 };

```

So, the compiler is running into type naming collisions across multiple canisters due to the autogenerated type names in the did files. By defining custom types for function results, these naming collisions can be avoided.

---

<div class="post-metadata">

**Author:** ![cryptoschindler](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/cryptoschindler/32/26_2.png) [@cryptoschindler](https://forum.dfinity.org/u/cryptoschindler)\
**Post date:** [February 5, 2022, 7:13am UTC](https://forum.dfinity.org/t/motoko-multiple-canister-imports-compile-error/10491/3 "2022-02-05T07:13:00Z")

</div>

This might be interesting for @chenyan

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [February 5, 2022, 5:27pm UTC](https://forum.dfinity.org/t/motoko-multiple-canister-imports-compile-error/10491/4 "2022-02-05T17:27:00Z")

</div>

Thanks for the report. There is no namespace in candid imports, which will certainly cause problems with multiple imports. I will file a bug: [https://github.com/dfinity/motoko/issues/3103](https://github.com/dfinity/motoko/issues/3103).

The current workaround is indeed to write custom type definitions to avoid the collision. A slightly more automatic approach is to use `didc` to generate the type bindings with `didc bind a.did -t mo`. Then you can `import A "./a"; import B "./b"` in Motoko and use `A.Result_2` and `B.Result_2` to avoid name collisions.
