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

Option to specify location of '.cache' folder [APE-703] #1335

Closed
0rtis opened this issue Mar 9, 2023 · 23 comments
Closed

Option to specify location of '.cache' folder [APE-703] #1335

0rtis opened this issue Mar 9, 2023 · 23 comments
Assignees
Labels
category: feature New feature or request

Comments

@0rtis
Copy link

0rtis commented Mar 9, 2023

Overview

Add an option to specify th location of the .cache folder. At the moment, it is located at the root of the contracts folder. This can cause issue with other framework (ex: Hardhat will parse all contracts in the contracts folder, including .cache resulting in very long processing time)

Specification

Ideally, it would be an option on the ape config file like cache_folder: mycache (similar to contracts_folder).

@0rtis 0rtis added the category: feature New feature or request label Mar 9, 2023
@NotPeopling2day NotPeopling2day changed the title Option to specify location of '.cache' folder Option to specify location of '.cache' folder [APE-703] Mar 9, 2023
@antazoey
Copy link
Member

antazoey commented Mar 14, 2023

This may also affect compiler plugins like ape-solidity. We would need to make sure they work when the .cache folder is outside of the contracts/ folder may require providing multiple base-paths.

@mikeshultz mikeshultz self-assigned this Jan 28, 2024
@mikeshultz
Copy link
Member

So what all ends up in the .cache folder? Is it just ethpm/dependency stuff or is there other uses?

@antazoey
Copy link
Member

So what all ends up in the .cache folder? Is it just ethpm/dependency stuff or is there other uses?

Primarily it gets used by ape-solidity. Your dependency source files get plopped there from the cache and then ape-soldity uses remappings to refer to the files there from your import statements. That is why it defaults to the contracts/ folder - it works best for importing

@mikeshultz
Copy link
Member

So what all ends up in the .cache folder? Is it just ethpm/dependency stuff or is there other uses?

Primarily it gets used by ape-solidity. Your dependency source files get plopped there from the cache and then ape-soldity uses remappings to refer to the files there from your import statements. That is why it defaults to the contracts/ folder - it works best for importing

Wondering if solc going to have a problem with this. I vaguely recall some sticking points that might make this challenging. For instance, directory paths affecting some kind of source hashing (libraries maybe?) or contract validation (memory is foggy). Also, I didn't think solc would compile source files outside of the contracts path.

Maybe there's some workarounds. Just need to wire it up and test it, I guess.

@antazoey
Copy link
Member

So what all ends up in the .cache folder? Is it just ethpm/dependency stuff or is there other uses?

Primarily it gets used by ape-solidity. Your dependency source files get plopped there from the cache and then ape-soldity uses remappings to refer to the files there from your import statements. That is why it defaults to the contracts/ folder - it works best for importing

Wondering if solc going to have a problem with this. I vaguely recall some sticking points that might make this challenging. For instance, directory paths affecting some kind of source hashing (libraries maybe?) or contract validation (memory is foggy). Also, I didn't think solc would compile source files outside of the contracts path.

Maybe there's some workarounds. Just need to wire it up and test it, I guess.

Alternative idea: is there a way to tell Hardhat to ignore these files via CLI or config file?

@mikeshultz
Copy link
Member

Background on the paths restrictions. I haven't dug into ape-solidity's compiler code yet so maybe if it's building JSON bundles or something it doesn't matter. https://docs.soliditylang.org/en/latest/path-resolution.html#allowed-paths

The hashing thing I mentioned, I think it had to do with solc producing different bytecode if the directory structure of contract imports were changed. That memory is super old and may not be relevant anymore. I also can't seem to find any modern reference to the issue, so may also not be relevant.

I think we just need to try this out and see what happens.

@antazoey
Copy link
Member

I think we just need to try this out and see what happens.

Ya, definitely need some research and answers to questions.
But it seems like the real issue is that Ape isn't playing nice with other frameworks?

Does HH or Foundry cache dependency-source files for importing somewhere similarly?

@mikeshultz
Copy link
Member

HH stores dep contracts in the project root's node_modules/ dir.

Looks like foundry stores deps in a lib/ dir in the root, also separate from user sources. They also seem to require some remappings for OZ internal/clean imports.

@antazoey
Copy link
Member

Solc has a --include-path CLI option. Perhaps we just need to make sure the new .cache directory is included there when compiling

@fubuloubu
Copy link
Member

@Ninjagod1251 can share some of our preliminary findings with the both of you based on our conversations with the HH team

@mikeshultz
Copy link
Member

Found .cache referenced in ape-solidity and ape-cairo. ape-vyper does not appear to leverage this. Any others I might be missing?

@antazoey
Copy link
Member

Found .cache referenced in ape-solidity and ape-cairo. ape-vyper does not appear to leverage this. Any others I might be missing?

  • Don't worry about ape-cairo, is no longer really works. You can think of it as theoretical though.
  • ape-vyper does not use it - i think it is does differently, like linking the compiled JSONs directly

ape-solidity for sure does use it.

@fubuloubu
Copy link
Member

ape-vyper does not appear to leverage this. Any others I might be missing?

It might in the future as vyper is adding module libraries

@mikeshultz
Copy link
Member

Frustrating experimentation. The couple of potentially useful compiler options are only available for newer versions, and trying to conditionally support project-level features depending on compiler version probably isn't really going to work (e.g. --no-cbor-metadata and --import-paths).

The hashing thing I mentioned, I think it had to do with solc producing different bytecode if the directory structure of contract imports were changed. That memory is super old and may not be relevant anymore. I also can't seem to find any modern reference to the issue, so may also not be relevant.

Changing the paths of files, changes the metadata (and JSON inputs) which changes the "garbage" appended to the contracts' bytecode. More background on this: https://docs.soliditylang.org/en/latest/metadata.html#encoding-of-the-metadata-hash-in-the-bytecode

It's kind of a cool feature, but super frustrating and I'm not aware of anyone that actually leverages it (though seems like it could be a cool Ape feature).

So, moving the location of the .cache directory changes the bytecode output, which may be unexpected to users. Maybe this isn't a big deal, since it's a setting in project-level config, but it may be pretty confusing and is likely to break things like contract verification if it ever changes during the life of the project.

I think that's probably fine and we just document it and let the users adjust their project structure as they see fit. If they manually had deps in their source tree and moved them they'd see the same behavior.


Long term thinking, maybe we could maybe create a temporary package structure that we build from on the fly every time we compile. This lets us assemble dependencies in a relative path however we see fit. This would likely increases complexity, and have a performance impact but having reproducible build steps is an interesting idea.

@antazoey
Copy link
Member

antazoey commented Feb 2, 2024

Long term thinking, maybe we could maybe create a temporary package structure that we build from on the fly every time we compile.

I think we kinda do this for dependencies already so it might not be a huge leap.

Regarding the flags that are available on some versions but not others: we do similar things already:
https://github.com/ApeWorX/ape-solidity/blob/main/ape_solidity/compiler.py#L461-L462

@mikeshultz
Copy link
Member

Regarding the flags that are available on some versions but not others: we do similar things already: https://github.com/ApeWorX/ape-solidity/blob/main/ape_solidity/compiler.py#L461-L462

If we did go that route, it would be just "throw error if config option is used and compiler < version" which isn't ideal, and probably not necessary.

@antazoey
Copy link
Member

antazoey commented Feb 2, 2024

We could add those configs separately to the ape-solidity config so the user could sometimes do this:

compile:
  cache_folder: my/custom/cache

solidity:
    import-paths:
      - my/custom/cache

@mikeshultz
Copy link
Member

We could add those configs separately to the ape-solidity config so the user could sometimes do this:

I don't think this makes much sense (for our problem). This basically requires user configuration to support some internal behavior of ape-solidity rather than something the user wanted to do. And only if they happen to not have any contracts with an older pragma.

@mikeshultz
Copy link
Member

Also, thinking about this and testing a bit more, we probably shouldn't allow absolute paths here (nor resolve anything to absolute paths) because they are likely to be different across machines. And different paths inputs would cause different metadata hashes altering the bytecode...

But, maybe we can use the project root as a base path for compilers instead of the contracts/ directory.

@mikeshultz
Copy link
Member

Need to get the docs correct with the latest revision but the code should be ready for review.

@mikeshultz
Copy link
Member

Just adding some context I ran into for anyone that needs to reference in this issue.

Was just looking around and it looks like Vyper also had some CBOR metadata appended to the bytecode. Then removed it later. Doesn't look like it was influenced by input metadata at all, so settings changes or path changes shouldn't influence it at all like with Solidity.

@fubuloubu
Copy link
Member

Doesn't look like it was influenced by input metadata at all, so settings changes or path changes shouldn't influence it at all like with Solidity.

Correct, Vyper's CBOR only contained the version of the compiler it was compiled with

@mikeshultz
Copy link
Member

This feature will be availible in the next release of the ape-solidity plugin. For some information on how to use it, see the compiler documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category: feature New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants