latticexyz / mud

MUD is a framework for building autonomous worlds
https://mud.dev
MIT License
712 stars 175 forks source link

improve error for structs, user types defined in their own file #762

Open yonadaaa opened 1 year ago

yonadaaa commented 1 year ago

I want to define a struct and a function that returns that datatype in the same file like so:

struct Record {
  bytes32 table;
  bytes32[] key;
  bytes value;
}

contract SyncSystem is System {
  function sync(bytes32[] memory tableIds) public view returns (Record[][] memory records) {
...
}

However, codegen fails and seems to expect this struct to be imported, with:

MUDError: Function "sync" in contract "SyncSystem": Symbol "Record" has no explicit import

This does not occur if I define the struct in another local file.

alvrs commented 1 year ago

This is expected, see #677 and #698 for context. Unclear if we can work around this, since we can't import from the system file in the system interface. (I guess one possible workaround might be to let codegen duplicate the struct definitions into the interface files, but that seems not very clean and not sure if a better solution than just asking consumers to define structs in separate files, like a local Types.sol)

dk1a commented 1 year ago

We can probably log a better explanation, but yeah there isn't really a workaround. Duplicating the struct would be horrible devex for calling the system from World, unlike typescript solidity won't consider those structs interchangeable

yonadaaa commented 1 year ago

asking consumers to define structs in separate files, like a local Types.sol

This isn't a bad idea! I wasn't sure where to put the struct instead.

We can probably log a better explanation

Yup, if the error was better I would have moved on and put it in another file, but I thought it was a bug.

This really should "just work" but as long as we document it, I'm happy to close this.