ApeWorX / archive-ape-cairo

Cairo programming language compiler plugin for the Ape Framework
https://www.apeworx.io/
Apache License 2.0
8 stars 6 forks source link

fix: bug with contract type name collision when using sub-directory namespacing #6

Closed antazoey closed 2 years ago

antazoey commented 2 years ago

What I did

fixes: #5

How I did it

Use namespace in contract type name. E.g. if your project looks like this:

contracts:
  Foo.cairo
  accounts:
    Bar.cairo
  tests:
    Bar.cairo

You will get the following contract type names:

Otherwise, you would get an error Contract collision across names and that is because Bar.cairo is a file in there twice.

This is actually a really common design pattern in Cairo because of their namespacing capabilities. Noticeably, in the OZ Cairo contracts project: https://github.com/OpenZeppelin/cairo-contracts, you will see many files named library.cairo in different namespaces. This is totally a valid Cairo project structure.

How to verify it

You can now compile this repo: https://github.com/OpenZeppelin/cairo-contracts NOTE: First, you have to create an ape-config.yaml that looks like this:

contracts_folder: src

This is step 1 to getting #4 to work properly

Checklist

antazoey commented 2 years ago

Would it make sense to also reicate the namespacing? E.g. project.accounts.Bar?

I thought about that. That would be a little more work. This PR makes it so you can do project.accounts_Bar.

Let me see if we can

antazoey commented 2 years ago

See https://github.com/ApeWorX/ape/pull/698 for supporting . access. It will require the next ape release!

However, good news is that users can call the project.get_contract() method and pass in contract names like "accounts.library" and get the ContractContainer

So this PR is not dependency necessarily on the next ape release, but need to know if we like the approach in the inked Ape PR before moving forward.